中序遍历二叉搜索树,在末尾打印”%”.

huangapple 未分类评论45阅读模式
英文:

Inorder traversal of BST printing "%" at the end

问题

我正在尝试打印二叉搜索树的元素。代码在大部分情况下都正常运行,但每次执行结束时都会打印出"%"符号。我正在使用中序遍历。我尝试使用调试器,但似乎该符号是在程序从主函数退出之前打印出来的。

如果您能指出发生这种情况的原因和时间,将会很有帮助。

**输出**

    (base) ➜  Trees java Tree
    3 10 11 12 %

**代码**

import java.util.*;

class Node{
int key;
Node left, right;

Node(int key){
    this.key = key;
    left = null;
    right = null;
}

}

class BinaryTree{
Node root;

BinaryTree(){
    root = null;
}

BinaryTree(int key){
    root = new Node(key);
}

//Wrapper over recursive
void push(int num) {
    push(num, this.root);
}

Node search(int query) {
    return search(query, this.root);
}

void inOrder() {
    inOrder(this.root);
}

Node search(int query, Node root) {
    if(root == null) {
        return null;
    }
    if(root.key == query){
        return root;
    }else if(root.key > query){
        return search(query, root.left);
    }else{
        return search(query, root.right);
    }
}

void push(int num, Node root){
    if(this.root == null) {
        this.root = new Node(num);
    }else{
        if(num < root.key){
            if(root.left == null)
                root.left = new Node(num);
            else
                push(num, root.left);
        }else{
            if(root.right == null)
                root.right = new Node(num);
            else
                push(num, root.right);
        }
    }
}

void inOrder(Node node) {
    if(node == null)
        return;

    inOrder(node.left);
    System.out.print(node.key + " ");
    inOrder(node.right);
}

}

class Tree {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

    tree.push(10);
    tree.push(3);
    tree.push(12);
    tree.push(11);
    tree.inOrder();
}

}



<details>
<summary>英文:</summary>

I am trying to print the elements of binary search tree. The code runs normally for most of the part but it is printing &quot;%&quot; symbol at the end of execution every time. I am using inorder traversal. I tried using debugger but it seems the symbol is printed just before the program exits from the main function.

It would be helpful if you can point out why and when this happens.

**Output**

    (base) ➜  Trees java Tree
    3 10 11 12 % 

**Code**

import java.util.*;

class Node{
int key;
Node left, right;

Node(int key){
    this.key = key;
    left = null;
    right = null;
}

}

class BinaryTree{
Node root;

BinaryTree(){
    root = null;
}

BinaryTree(int key){
    root = new Node(key);
}

//Wrapper over recursive
void push(int num) {
    push(num, this.root);
}

Node search(int query) {
    return search(query, this.root);
}

void inOrder() {
    inOrder(this.root);
}

Node search(int query, Node root) {
    if(root == null) {
        return null;
    }
    if(root.key == query){
        return root;
    }else if(root.key &gt; query){
        return search(query, root.left);
    }else{
        return search(query, root.right);
    }
}

void push(int num, Node root){
    if(this.root == null) {
        this.root = new Node(num);
    }else{
        if(num &lt; root.key){
            if(root.left == null)
                root.left = new Node(num);
            else
                push(num, root.left);
        }else{
            if(root.right == null)
                root.right = new Node(num);
            else
                push(num, root.right);
        }
    }
}

void inOrder(Node node) {
    if(node == null)
        return;

    inOrder(node.left);
    System.out.print(node.key + &quot; &quot;);
    inOrder(node.right);
}

}

class Tree {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();

    tree.push(10);
    tree.push(3);
    tree.push(12);
    tree.push(11);
    tree.inOrder();
}

}


</details>


huangapple
  • 本文由 发表于 2020年4月7日 12:03:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/61072632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定