繁体   English   中英

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

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

当我在main方法中调用print方法时,它在控制台上什么都不打印。 我试图按字母顺序制作二叉搜索树。 为什么会这样? 我的插入方法和添加方法是否正确? 或者,打印方法有问题吗?

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);
    }
}

您没有考虑可能添加到空树的可能性,在这种情况下,您需要专门设置根节点:

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;
}

我不确定这是你需要的解决方案,但我知道为什么你不能打印所有元素。 查看你的构造函数,你创建了一个带有根元素的BinaryTree,总是为null。 在第一次插入新元素时,调用函数Node add(String element,Node bstree)并返回新的Node() 为什么不将新节点分配给当前Btree,因为Btree仍然为空? 我们有几个解决方案来解决它。 这是我的意见:

  1. 创建新的构造函数:

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

  2. 我改变主要功能看起来像:

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

P / s:我创建了一个BTree(与你的BTree不同)。 你可以在这里看到: BST-Level-Order-Traversal

暂无
暂无

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

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