Cross a binary search tree recursively and print data in columns

I built a binary search tree using a text file that is read by the main function. The resulting tree contains the words of a text file with a counter, so that the same word is not inserted twice. The problem is not in creating a tree, but in the correct display of information. Data should be printed in columns 4 to keep them readable.

Example:

|BTNode1|BTNode2|BTNode3|BTNode4| |BTNode5|BTNode6|BTNode7|BTNode8| 

The BTNode class has a toString() method that outputs data from individual nodes. But whenever I call this code below with the root of the node and the number 0, I get the node information correctly, but in strange numbers of nodes per column. Any ideas how to make this work? If necessary, I can add additional code.

EDIT: Added a whole class to reflect changes and added sample output current. There may be a problem with building a tree.

EDIT2: Changed printcount = 1 , fixes display issues. The code now works correctly.

 package speech; public class BSTree { private BTNode root; private final String DISPLAY_FORMAT_CAPS = "*****************************************************************"; private StringBuilder buffer = new StringBuilder(); private int printcount = 1; public BSTree (){ root = null; } public BTNode insert(String indata, boolean lowercase){ if(lowercase){ if(root != null){ return insertRecursive(root,indata.toLowerCase()); } else{ root = new BTNode(indata.toLowerCase()); return root; } } else{ if(root != null){ return insertRecursive(root,indata); } else{ root = new BTNode(indata); return root; } } } private BTNode insertRecursive(BTNode node, String value) { if (value.compareTo(node.data) < 0){ if (node.left != null) { return insertRecursive(node.left, value); } else { //System.out.println(" Inserted " + value + " to left of Node " + node.data); node.left = new BTNode(value); return node.left; } } else if (value.compareTo(node.data) > 0) { if (node.right != null) { return insertRecursive(node.right, value); } else { //System.out.println(" Inserted " + value + " to right of Node " + node.data); node.right = new BTNode(value); return node.left; } } else if (value.compareTo(node.data) == 0){ node.incrementCount(); //System.out.println("Incremented count of " + value + " to: " + node.wordcount); return node; } return null; } private int wordcountRecursive(BTNode node){ if(node == null){ return 0; } else{ return wordcountRecursive(node.left) + node.wordcount + wordcountRecursive(node.right); } } public int wordcount(){ return wordcountRecursive(root); } public void display(){ System.out.println(DISPLAY_FORMAT_CAPS); displayRecursive(root); System.out.println(buffer.toString()); System.out.println(DISPLAY_FORMAT_CAPS); System.out.println("Word Count:" + wordcount()); } private void displayRecursive (BTNode node){ //System.out.println(count); if(node != null){ displayRecursive(node.left); addNodeDisplay(node); displayRecursive(node.right); } } private void addNodeDisplay(BTNode node){ if(printcount % 4 != 0){ buffer.append("|").append(node); } else{ buffer.append("|").append(node).append("|\n"); } printcount++; } } 
+4
source share
1 answer

I added some sample data, and it looks like this:

 private void displayRecursive(Node node) { displayRecursive(node, 0); System.out.println(""); } private int displayRecursive(Node node, int count) { if (node != null) { // Do left first. count = displayRecursive(node.getLeft(), count); // New line? if (count > 0 && count % 4 == 0) { // End of line. System.out.println("|"); } // Then me. System.out.print("|" + node); count += 1; // Then right. count = displayRecursive(node.getRight(), count); } return count; } private void test() { Node root = new Node("Root"); Node left = new Node("Left"); Node right = new Node("Right"); root.setLeft(left); root.setRight(right); Node leftLeft = new Node("Left.Left"); leftLeft.setLeft(new Node("LeftLeftLeft")); leftLeft.setRight(new Node("LeftLeftRight")); left.setLeft(leftLeft); left.setRight(new Node("Left.Right")); right.setLeft(new Node("Right.Left")); right.setRight(new Node("Right.Right")); displayRecursive(root); } public static void main(String[] args) throws InterruptedException { try { Test test = new Test(); test.test(); } catch (Exception e) { e.printStackTrace(); } } static class Node { final String data; private Node left = null; private Node right = null; Node(String data) { this.data = data; } @Override public String toString() { return data; } /** * @return the left */ public Node getLeft() { return left; } /** * @param left the left to set */ public void setLeft(Node left) { this.left = left; } /** * @return the right */ public Node getRight() { return right; } /** * @param right the right to set */ public void setRight(Node right) { this.right = right; } } 

he prints:

 |LeftLeftLeft|Left.Left|LeftLeftRight|Left| |Left.Right|Root|Right.Left|Right| |Right.Right 
0
source

All Articles