1

I'm working on code for insertion into a binary search tree. It works for the first node I insert, making it the root, but after that it doesn't seem to insert any nodes. I'm sure it's a problem with setting left/right references, but I can't quite figure it out. Please help!

    //params: key of node to be inserted, parent node
    public void insert(int newKey, TreeNode parent){

    //if the root of the tree is empty, insert at root
    if(this.getRoot() == null){
        this.root = new TreeNode(newKey, null, null);
    }

    //if the node is null, insert at this node
    else if(parent == null)
        parent = new TreeNode(newKey, null, null);


    else{
        //if the value is less than the value of the current node, call insert on the node's left child
        if(newKey < parent.getKey()) {
                insert(newKey, parent.getLeft());
        }
        //greater than value of current node, call insert on node's right child
        else if(newKey > parent.getKey()){
                insert(newKey, parent.getRight());
        }
        //same as value of current node, increment iteration field by one
        else if(newKey == parent.getKey())
            parent.incrementIterations();
    }

}

My treenodes have key, left, right, and iteration fields, as well as getter/setter functions. Thank you in advance!

2
  • One problem I can see is that parent is passed by value. Changes to parent will be to a local copy. You probably need to pass by reference. Commented Oct 9, 2014 at 4:20
  • Yes, thank you! I noticed that as well as added extra if loops to solve it. I believe this fixes the problem: if(newKey < parent.getKey()) { if(parent.getLeft() == null) parent.setLeft(new TreeNode(newKey, null, null)); else insert(newKey, parent.getLeft()); } I edited the code to check the parent node's children before recursively calling insert again and this allowed the parent node's left/right references to be set correctly. Commented Oct 9, 2014 at 4:29

3 Answers 3

10
public Node insertNode(Node head, int data) {

        if(head == null){
            head = new Node();
            head.data = data;
            return head;
        }
        if(head.data < data) {
            head.right = insertNode(head.right,data);
        } else {
            head.left = insertNode(head.left, data);
        }
        return head;
    }
Sign up to request clarification or add additional context in comments.

Comments

2

If (parent==null) you are creating a node, but you are not associating it to tree back. Its just created and garbage collected.

You should be using insert (newkey, parent) then u still have handle to tree

Comments

1
private AVLNode insert(AVLNode root, int value){
    if (root == null) return new AVLNode(value);

    if(root.value > value)
        root.leftChild = insert(root.rightChild, value);
    else
        root.rightChild = insert(root.leftChild, value);

    return root;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.