user2916886
user2916886

Reputation: 845

Updating parent node in Binary search tree in JAVA

I am working on Binary search tree and I am having some problems in updating/adding the parent node of current node.Parent node are not getting updated correctly.This is what Iam trying to do:

public class binarytree {

    private Node root;

static class Node{

    Node left;
    Node right;
    int value;
    Node parent;

    public Node(Node parent,int value)
        {
        this.value = value;
        this.left = null;
        this.right = null;
        this.parent = parent;
        }
    }

public void creatBST()
    {
    root = new Node(null,4);
    System.out.println("Binary Search tree with root = "+ root.value);
    insert(root,1);
    insert(root,2);
    insert(root,3);
    insert(root,6);
    insert(root,5);
    insert(root,7);
}

public void insert(Node node, int value)
    {
    Node prevnode = node;
    if(value < node.value)
        {
        if(node.left != null)
            {


            insert(node.left, value);   
            System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);
            }
        else
            {

            node.left = new Node(prevnode,value);
            }
        }
    else if(value > node.value)
        {
        if(node.right != null)
            {
            //prevnode = node;

            insert(node.right, value);
            System.out.println("Parent node of node"+ node.right.value+"is:"+prevnode.value);
            }
        else
            {

            node.right = new Node(prevnode,value);
            }
        }
    }
public static void main(String args [])
    {
    binarytree obj = new binarytree();
    obj.creatBST();
    //obj.printInOrder();
    //obj.check();
    }
}

The output which I am getting is:

    Binary Search tree with root = 4
Parent node of node1is:4
Parent node of node2is:1
Parent node of node1is:4
Parent node of node6is:4
Parent node of node6is:4

which is not the correct output.

Need help me in determining how can I do it correctly.

Upvotes: 0

Views: 3363

Answers (2)

pokemans
pokemans

Reputation: 98

It looks like the insertion of nodes is working properly. The print statements are wrong because they are called after the recursive call to insert the next Node. For example, after you do insert(root,1) your function works and you have the following structure:

          4
         /
        1

Now when you call insert(root, 2) you end up at this point in the code:

if(node.left != null)
        {


        insert(node.left, value);   
        System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);
        }

insert(node.left, value) is called and then the print statement happens. So while your new tree looks like this:

          4
         /
        1
         \
          2

The next print statement

        System.out.println("Parent node of node"+ node.left.value+"is:"+prevnode.value);

will print out node.left.value of 1 and prevnode.value of 4. You may want to change where your print statements are or write a new method that prints the nodes and parent nodes.

Edit: I would run this method in your main method to test the tree structure:

    public void printParents(Node n){
    if(n.left!= null){
        printParents(n.left);
    }
    if(n.right!=null){
        printParents(n.right);
    }
    if(n.parent != null){
        System.out.println("Parent of " + n.value + " is " + n.parent.value);
    }
    else{
        System.out.println("The root node is" + n.value);
    }
}

I called it here:

public static void main(String args [])
{
binarytree obj = new binarytree();
obj.creatBST();
Node n = obj.root;
obj.printParents(n);
//obj.printInOrder();
//obj.check();
}

Upvotes: 3

La-comadreja
La-comadreja

Reputation: 5755

For starters, you might want to fix the insert method:

Node currentNode;
public void setCurrentNode(Node node) {
    currentNode = node;
}

public boolean insert(int value) {
    //You should probably insert from an instance variable, not an explicit parameter
    Node n = new Node(currentNode, value);
    if (currentNode.left == null) currentNode.left = n;
    else if (currentNode.right == null) currentNode.right = n;
    else return false;    //If insert failed because currentNode was full
    return true;
}

Upvotes: 1

Related Questions