Java二进制树。打印InOrder遍历

时间:2010-04-24 21:17:32

标签: java binary-tree tree-traversal inorder

我在打印二叉树的inOrder遍历时遇到一些问题。即使将多个项目插入树中,它也只打印3个项目。

public class BinaryTree {

    private TreeNode root;
    private int size;

    public BinaryTree(){
        this.size = 0;
    }

    public boolean insert(TreeNode node){

        if( root == null)
            root = node;

        else{
            TreeNode parent = null;
            TreeNode current = root;
            while( current != null){
                if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
                    parent = current;
                    current = current.getLeft();
                }
                else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
                    parent = current;
                    current = current.getRight();
                }
                else
                    return false;

                if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
                    parent.setLeft(node);
                else
                    parent.setRight(node);
                }
            }
            size++;
            return true;
        }


    /**
     * 
     */
    public void inOrder(){
        inOrder(root);
    }

    private void inOrder(TreeNode root){
        if( root.getLeft() !=null)
            this.inOrder(root.getLeft());
        System.out.println(root.getData().getValue());

        if( root.getRight() != null)
            this.inOrder(root.getRight());
    }



}

3 个答案:

答案 0 :(得分:1)

您插入方法有问题。它找到了将新元素附加到的正确父节点,但是在它弄乱整个树的过程中。您应该将插入代码移出while循环:

public boolean insert(TreeNode node){

    if( root == null)
        root = node;

    else{
        TreeNode parent = null;
        TreeNode current = root;
        while( current != null){
            if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
                parent = current;
                current = current.getLeft();
            }
            else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
                parent = current;
                current = current.getRight();
            }
            else
                return false;
        }

        if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
            parent.setLeft(node);
        else
            parent.setRight(node);
        }

        size++;
        return true;
    }
}

答案 1 :(得分:1)

您似乎没有在插入时正确遍历树,以便为新节点找到正确的位置。现在你总是插入根的一个子节点,因为插入代码在while循环内 - 它应该在之后

public boolean insert(TreeNode node){

    if( root == null)
        root = node;

    else{
        TreeNode parent = null;
        TreeNode current = root;
        while( current != null){
            if( node.getData().getValue().compareTo(current.getData().getValue()) <0){
                parent = current;
                current = current.getLeft();
            }
            else if( node.getData().getValue().compareTo(current.getData().getValue()) >0){
                parent = current;
                current = current.getRight();
            }
            else
                return false;
        }
        if(node.getData().getValue().compareTo(parent.getData().getValue()) < 0)
            parent.setLeft(node);
        else
            parent.setRight(node);
        }

        size++;
        return true;
    }

答案 2 :(得分:0)

嘿,这里的人很简单......尝试一下......这对我很有用......

public void levelOrderTraversal(Node root){
    Queue<Node> queue = new ArrayDeque<Node>();
    if(root == null) {
        return;
    }
    Node tempNode = root;
    while(tempNode != null) {
        System.out.print(tempNode.data + " ");

        if(tempNode.left != null)   queue.add(tempNode.left);
        if(tempNode.right != null)   queue.add(tempNode.right);

        tempNode = queue.poll();
    }
}