简体   繁体   English

为什么我的print方法无法按顺序打印出二进制搜索树?

[英]Why does my print method fail to print out the binary search tree inorder?

When I call print method in the main method, it prints nothing on the console. 当我在main方法中调用print方法时,它在控制台上什么都不打印。 I am trying to make a binary search tree in alphabetical order. 我试图按字母顺序制作二叉搜索树。 Why is it that? 为什么会这样? Are my insert method and add method correct? 我的插入方法和添加方法是否正确? Or, is it something wrong with the print method? 或者,打印方法有问题吗?

public class Node
{
    String value;
    Node leftChild;
    Node rightChild;

    Node(String val,Node left, Node right)
    {
        value = val;
        leftChild = left;
        rightChild = right;
    } 

    Node(String val)
    {
        value = val;
        leftChild = null;
        rightChild = null;

    }
}

public class binarySearchTree
{
    Node root;

    binarySearchTree()
    {
        root = null;
    }

    public Node search(String element)
    {
        Node current = root;
        while (element.compareTo(current.value) != 0 )
        {
            if(current == null)
                return null;
            else
            {
                if(element.compareTo(current.value) < 0)
                {
                    current = current.leftChild;
                }
                else
                current = current.rightChild;
            }
        }
        return current;
    }

    public Node add(String element, Node bstree)
    { 

        if(bstree == null)
        {
            return new Node(element);
        }
        else if(element.compareTo(bstree.value) < 0)
        {
            bstree.leftChild = add(element, bstree.leftChild);
        }
        else
        {
            bstree.rightChild = add(element, bstree.rightChild);
        }

        return bstree;
    }

    public void insert(String element)
    {
        add(element,root);
    }


    public void print(Node bstree)
    {
        if(bstree != null)
        {
            print(bstree.leftChild);
            System.out.print(bstree.value + " ");
            print(bstree.rightChild);
        }
    }    
}     

public class testing
{
    public static void main(String[] agrs)
    {
        binarySearchTree tree = new binarySearchTree();
        tree.insert("apple");
        tree.insert("banana");
        tree.insert("kiwi");
        tree.print(tree.root);
    }
}

You are not accounting for the possibility that you might be adding to an empty tree, in which case you need to set the root node specifically: 您没有考虑可能添加到空树的可能性,在这种情况下,您需要专门设置根节点:

public Node add(String element, Node bstree)
{ 
    if (root == null)
    {
        root = new Node(element);
        return root;
    }

    if (bstree == null)
    {
        return new Node(element);
    }
    else if (element.compareTo(bstree.value) < 0)
    {
        bstree.leftChild = add(element, bstree.leftChild);
    }
    else
    {
        bstree.rightChild = add(element, bstree.rightChild);
    }

    return bstree;
}

I'm not sure this is solution you need, but I know why you can't print all elements. 我不确定这是你需要的解决方案,但我知道为什么你不能打印所有元素。 Look at your contructor, you have created a BinaryTree with root element always is null. 查看你的构造函数,你创建了一个带有根元素的BinaryTree,总是为null。 At the first time when you insert new element, function Node add(String element, Node bstree) was called and returned new Node() . 在第一次插入新元素时,调用函数Node add(String element,Node bstree)并返回新的Node() Why you don't assign the new Node to current Btree because Btree still null ?. 为什么不将新节点分配给当前Btree,因为Btree仍然为空? We have a couple of solution to fix it. 我们有几个解决方案来解决它。 It's my opinion : 这是我的意见:

  1. Create new contructor : 创建新的构造函数:

    public BinarySearchTree(Node root) { this.root = root; public BinarySearchTree(Node root){this.root = root; } }

  2. I change main function look like : 我改变主要功能看起来像:

    BinarySearchTree tree = new BinarySearchTree(new Node("apple")); BinarySearchTree tree = new BinarySearchTree(new Node(“apple”));

P/s : I have created a BTree ( not the same your BTree) . P / s:我创建了一个BTree(与你的BTree不同)。 You can see here : BST-Level-Order-Traversal 你可以在这里看到: BST-Level-Order-Traversal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM