簡體   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