hjalpmig
hjalpmig

Reputation: 702

Java Binary Search Tree Not Printing at run time

I have created a Binary Search Tree class which I followed some youtube videos to make. The problem is that, from what I can tell, my code is exactly the same as is in the guide I followed. The issue is that when I run my main() method nothing happens. There is no compile or runtime errors, the program simply does nothing. What should happen is that it should use the toString() method to print out some keys and values of those keys if I use the traverse methods, or it should provide a value of a node if I use the findNode() method. I was hoping someone could check my code to see if they can spot any logic errors and such, as I have spent a long time checking and rechecking my code and I cant seem to spot it.

Here is the code for my main method:

public class BinarySearchTreeAss2 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BinarySearchTree myTree = new BinarySearchTree();

        myTree.addNode(50, 1);
        myTree.addNode(25, 2);
        myTree.addNode(15, 3);
        myTree.addNode(30, 4);
        myTree.addNode(75, 5);
        myTree.addNode(80, 6);

        myTree.inOrderTraversal(myTree.root);
        System.out.println("\n\n");

        myTree.preOrderTraversal(myTree.root);
        System.out.println("\n\n");

        myTree.postOrderTraversal(myTree.root);
        System.out.println(myTree.findNode(30));
    }
}

And the code for the Binary Search Tree class:

public class BinarySearchTree {
    Node root;

    public void addNode(int key, int value) {
        Node newNode = new Node(key, value);
        if(root == null) {
            root = newNode;
        } else {
            Node focusNode = root;
            Node parent;
            while(true) {
                parent = focusNode;
                if(key < focusNode.key) {
                    focusNode = focusNode.leftChild;
                    if(focusNode == null) {
                        parent.leftChild = newNode;
                        return;
                    } else {
                        focusNode = focusNode.rightChild;
                        if(focusNode == null) {
                            parent.rightChild = newNode;
                            return;
                        }
                    }
                }
            }
        }
    }

    public void inOrderTraversal(Node focusNode) {
        if(focusNode != null) {
            inOrderTraversal(focusNode.leftChild);
            System.out.println(focusNode);
            inOrderTraversal(focusNode.rightChild);
        }
    }

    public void preOrderTraversal(Node focusNode) {
        if(focusNode != null) {
            System.out.println(focusNode);
            preOrderTraversal(focusNode.leftChild);
            preOrderTraversal(focusNode.rightChild);
        }
    }

    public void postOrderTraversal(Node focusNode) {
        if(focusNode != null) {
            postOrderTraversal(focusNode.leftChild);
            postOrderTraversal(focusNode.rightChild);
            System.out.println(focusNode);
        }
    }

    public Node findNode(int key) {
        Node focusNode = root;
        while(focusNode.key != key) {
            if(key < focusNode.key) {
                focusNode = focusNode.leftChild;
            } else {
                focusNode = focusNode.rightChild;
            }
            if(focusNode == null) {
                return null;
            }
        }
        return focusNode;
    }
}

class Node {
    int key;
    int value;

    Node leftChild;
    Node rightChild;

    Node(int key, int value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return value + " has the key " + key;
    }
}

Upvotes: 0

Views: 95

Answers (1)

yate
yate

Reputation: 804

You messed up on the if statement brace which causes the program to go into an infinite loop.

if (key < focusNode.key) {
    focusNode = focusNode.leftChild;

    if (focusNode == null) {
        parent.leftChild = newNode;
        return;
    }

    else {
        focusNode = focusNode.rightChild;

        if (focusNode == null) {
            parent.rightChild = newNode;
            return;
        }
    }
}

If the key is less than the focusNode.key then go left, ELSE go to the right, so it should really be

if (key < focusNode.key) {
    focusNode = focusNode.leftChild;

    if (focusNode == null) {
        parent.leftChild = newNode;
        return;
    }
} else {
    focusNode = focusNode.rightChild;

    if (focusNode == null) {
        parent.rightChild = newNode;
        return;
    }
}

Upvotes: 1

Related Questions