title: Recursively Traverse a Binary Search Tree And Print Out Data In Columns
body: <p>I have constructed a binary search tree using a text file that is read in by the main function. The resulting tree contains the words of the text file, with a count so that the same word is not inserted twice. The problem is not with constructing the tree, but getting the information to display properly. The data is required to be printed out in columns of 4, as to keep it readable.</p>
  
  <p>Example:</p>
  
  <pre><code>|BTNode1|BTNode2|BTNode3|BTNode4|  
  |BTNode5|BTNode6|BTNode7|BTNode8|
  </code></pre>
  
  <p>The <code>BTNode</code> class has a <code>toString()</code> method that prints out the data of the individual nodes. But, whenever I call this code below with the root node, and a count of 0 I get the node information properly, but in weird numbers of nodes per column. Any ideas how to get this to work? I can post additional code if necessary.</p>
  
  <p>EDIT: Added entire class to reflect changes, and added sample current output. Might be a problem with constructing the tree.</p>
  
  <p>EDIT2: Changed <code>printcount = 1</code>, fixes the display problems. Code now works properly.</p>
  
  <pre><code>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) &lt; 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) &gt; 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++;
  }
  }    
  </code></pre>