【问题标题】:Why does my print method fail to print out the binary search tree inorder?为什么我的打印方法无法按顺序打印出二叉搜索树?
【发布时间】:2016-11-29 01:18:40
【问题描述】:

当我在 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);
    }
}

【问题讨论】:

    标签: java linked-list tree binary-search-tree


    【解决方案1】:

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

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

    【讨论】:

    • 我的第一个 if 语句不是已经说明了树为空的可能性吗? bstree 是根。当 bstree 为 null 时,树为空。
    • 如果整棵树的根为空,那么你必须设置根,稍后你用它来调用打印方法。您当前拥有的 if 语句仅检查给定子树的根是否为空,并且您仅返回一个新节点。
    • “设置根目录”是什么意思?
    • binarySearchTree 类中的根实例变量,除了 null 之外,它永远不会被设置为任何值
    • 哦等等,根总是空的?我虽然树的第一个顶部节点是根。
    【解决方案2】:

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

    1. 创建新的构造函数:

      公共二进制搜索树(节点根){ this.root = 根; }

    2. 我改变主要功能的样子:

      BinarySearchTree 树 = new BinarySearchTree(new Node("apple"));

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2020-08-18
      相关资源
      最近更新 更多