Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归遍历二叉搜索树并按列打印数据_Java_Recursion_Binary Search Tree - Fatal编程技术网

Java 递归遍历二叉搜索树并按列打印数据

Java 递归遍历二叉搜索树并按列打印数据,java,recursion,binary-search-tree,Java,Recursion,Binary Search Tree,我使用一个文本文件构建了一个二叉搜索树,该文本文件由main函数读入。生成的树包含文本文件中的单词,并带有一个计数,以便同一单词不会插入两次。问题不在于构建树,而在于正确显示信息。数据需要以4列打印出来,以保持可读性 例如: |BTNode1|BTNode2|BTNode3|BTNode4| |BTNode5|BTNode6|BTNode7|BTNode8| BTNode类有一个toString()方法,用于打印各个节点的数据。但是,每当我使用根节点调用下面的代码,并且计数为0时,我都会正

我使用一个文本文件构建了一个二叉搜索树,该文本文件由main函数读入。生成的树包含文本文件中的单词,并带有一个计数,以便同一单词不会插入两次。问题不在于构建树,而在于正确显示信息。数据需要以4列打印出来,以保持可读性

例如:

|BTNode1|BTNode2|BTNode3|BTNode4|  
|BTNode5|BTNode6|BTNode7|BTNode8|
BTNode
类有一个
toString()
方法,用于打印各个节点的数据。但是,每当我使用根节点调用下面的代码,并且计数为0时,我都会正确地获得节点信息,但每列的节点数都很奇怪。你有什么办法让它工作吗?如有必要,我可以发布附加代码

编辑:添加整个类以反映更改,并添加示例当前输出。可能是构造树时出现问题

EDIT2:Changed
printcount=1
,修复了显示问题。代码现在可以正常工作了

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++;
}
}    
package-speech;
公共类BSTree{
私有节点根;
专用最终字符串显示\u格式\u大写=
"*****************************************************************";
私有StringBuilder缓冲区=新StringBuilder();
私有int printcount=1;
公共BSTree(){
root=null;
}
公共BTNode插入(字符串indata,布尔小写){
if(小写){
if(root!=null){
返回insertRecursive(root,indata.toLowerCase());
}
否则{
root=新的BTNode(indata.toLowerCase());
返回根;
}
}
否则{
if(root!=null){
返回insertRecursive(root,indata);
}
否则{
root=新的BTNode(indata);
返回根;
}
}
}
专用BTNode insertRecursive(BTNode节点,字符串值){
if(value.compareTo(node.data)<0){
if(node.left!=null){
返回insertRecursive(node.left,value);
}否则{
//System.out.println(“在节点“+节点数据”左侧插入“+值+”);
node.left=新的BTNode(值);
返回node.left;
}
}else if(value.compareTo(node.data)>0){
if(node.right!=null){
返回insertRecursive(node.right,value);
}否则{
//System.out.println(“在节点“+节点数据”右侧插入“+值+”);
node.right=新的BTNode(值);
返回node.left;
}
}else if(value.compareTo(node.data)==0){
node.incrementCount();
//System.out.println(“将“+value+”的计数增加到“+node.wordcount”);
返回节点;
}
返回null;
}
private int wordcountRecursive(BTNode节点){
if(node==null){
返回0;
}
否则{
返回wordcountRecursive(node.left)+node.wordcount+wordcountRecursive(node.right);
}
}
公共int字数(){
返回wordcountRecursive(root);
}
公共空间显示(){
System.out.println(显示格式\u大写);
显示递归(根);
System.out.println(buffer.toString());
System.out.println(显示格式\u大写);
System.out.println(“字数:+wordcount());
}
私有void displayRecursive(BTNode节点){
//系统输出打印项次(计数);
如果(节点!=null){
显示递归(node.left);
addNodeDisplay(节点);
displayRecursive(node.right);
}
}
专用void addNodeDisplay(BTNode节点){
如果(打印计数%4!=0){
append(“|”).append(节点);
}
否则{
buffer.append(“|”).append(节点).append(“|\n”);
}
printcount++;
}
}    

我添加了一些示例数据,看起来效果不错:

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;
  }
}
它打印:

|LeftLeftLeft|Left.Left|LeftLeftRight|Left|
|Left.Right|Root|Right.Left|Right|
|Right.Right

为什么不将遍历和4列构造拆分为2个方法?@AdamArold不确定作为赋值的一部分是否可以接受,但我可以遍历它并将其存储在arraylist中,然后使用标准循环输出数据。不过他确实希望它是递归的,至少遍历是递归的。您的
count++
应该是
count+1
。甚至不接近:)不过我非常感谢您的帮助,我通过分离列构造和遍历方法使代码正常工作,使事情变得更容易。