简体   繁体   中英

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. 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. At the first time when you insert new element, function Node add(String element, Node bstree) was called and returned new Node() . Why you don't assign the new Node to current Btree because Btree still null ?. We have a couple of solution to fix it. It's my opinion :

  1. Create new contructor :

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

  2. I change main function look like :

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

P/s : I have created a BTree ( not the same your BTree) . You can see here : BST-Level-Order-Traversal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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