-1

I could not find people to explain me this java code properly so finally I am posting this question.please explain the process how that particular statement is affecting the tree.the problems are stated in the comments.I have problems in the BST class.

 import java.util.Scanner;

 class BSTNode
 {
     BSTNode left, right;
     int data;

     public BSTNode()
     {
         left = null;
         right = null;
         data = 0;
     }

     public BSTNode(int n)
     {
         left = null;
         right = null;
         data = n;
     }

     public void setLeft(BSTNode n)
     {
         left = n;
     }

     public void setRight(BSTNode n)
     {
         right = n;
     }

     public BSTNode getLeft()
     {
         return left;
     }

     public BSTNode getRight()
     {
         return right;
     }

     public void setData(int d)
     {
         data = d;
     }

     public int getData()
     {
         return data;
     }     
 }

 class BST
 {
     private BSTNode root;

     public BST()
     {
         root = null;
     }

     public boolean isEmpty()
     {
         return root == null;
     }

Why is the insert function written like root=insert(...... Is it returning root = actual root element each time?

     public void insert(int data)
     {
         root = insert(root, data);
     }

I understand how the inserting process is going on but what is the insert function returning? I know that it returns some node but how the is the process going on during iteration?

     private BSTNode insert(BSTNode node, int data)
     {
         if (node == null)
             node = new BSTNode(data);
         else
         {
             if (data <= node.getData())
                 node.left = insert(node.left, data);
             else
                 node.right = insert(node.right, data);
         }

         return node;
     }

     public void delete(int k)
     {
         if (isEmpty())
             System.out.println("Tree Empty");
         else if (search(k) == false)
             System.out.println("Sorry "+ k +" is not present");
         else
         {
             root = delete(root, k);

Again, why is the delete function written like root=delete(.....? Is it returning root =actual root element each time?

             System.out.println(k+ " deleted from the tree");
         }
     }

     private BSTNode delete(BSTNode root, int k)
     {
         BSTNode p, p2, n;

         if (root.getData() == k)
         {
             BSTNode lt, rt;
             lt = root.getLeft();
             rt = root.getRight();

             if (lt == null && rt == null)
                 return null;
             else if (lt == null)
             {
                 p = rt;
                 return p;
             }
             else if (rt == null)
             {
                 p = lt;
                 return p;
             }
             else
             {
                 //case when we delete node having both children.
                 p2 = rt;
                 p = rt;

                 //getting the min of the right child subtree in p variable .
                 while (p.getLeft() != null)
                     p = p.getLeft();

                 p.setLeft(lt);

Please explain what is happening here and why is p2 i.e rt being returned.

                 return p2;
             }
         }

         if (k < root.getData())
         {
             n = delete(root.getLeft(), k);
             root.setLeft(n);
         }
         else
         {
             n = delete(root.getRight(), k);
             root.setRight(n);             
         }

         return root;
     }

     public int countNodes()
     {
         return countNodes(root);
     }
John Kugelman
  • 307,513
  • 65
  • 473
  • 519
untilted
  • 54
  • 6

1 Answers1

0

In the delete portion of your code, what you are doing is checking to see if the node (called root) has its data value equal to the value you want to delete (k). If that is true, then you examine both children which you seem to grasp. So then we get down to your question when you have both children of the node not null. In this case you want to delete the current node (root) of the subtree you are on, but you need to choose a node (either the left or right) to promote to the position of this node. So (assuming this tree is not balanced) you simply make a choice (left or right) of the subtree to promote to the know root. Keep in mind here that you are only calling the current node root because it is the root of some subtree within the larger tree (this does not mean the actual root of the tree unless that is the value that was passed in as root). Knowing that all of the values in the right child subtree are going to be greater than those in the left child subtree, you simply take the current node's left subtree then recurse as far down the left children of the right subtree as you can go until you're to the end. You then set this node's left child to the entire left subtree.

//case when we delete node having both children.
p2 = rt;
p = rt;

//getting the min of the right child subtree in p variable .
while (p.getLeft() != null)
p = p.getLeft();

p.setLeft(lt);

Notice in the statement

p = rt;

You are setting P to be the root of the of the right subtree. Then p is now the right child of the current root passed in.

while (p.getLeft() != null)
    p = p.getLeft();

p.setLeft(lt);

This is basically saying that for that right subtree, If the root node has a left child then set p to that and keep doing so until that value is null. This will keep going down the left children of that right subtree. Finally once that value is null

p.setLeft(lt);

Will set the left child of that left most leaf in the right subtree to the entire left subtree that you started with. And return the node that you said was the right subtree of the original (now with the left subtree of the original appended to its leftmost leaf).

brandenesmith
  • 136
  • 1
  • 8
  • So, you want to delete 12 but you notice that twelve has both a left and right child so you save those as `lt` and `rt`respectively then you set `p` to be the right which is 21. Now while 21 has a left child you set `p` to that child so now 19 but now the left child of 19 is null so you set its left child to be 9 and you will return 21 which you saved as `p2` this will be set to the right child of 5. So, 12 is now gone and 21 is in its place and 9 has moved down to be the left child of 19. – brandenesmith Oct 11 '15 at 14:09
  • I thought it to be exactly like what you told but this website explains deletion of node otherwise.If what this site explains is true then this program's deletion process is wrong.please check this [link](http://www.algolist.net/Data_structures/Binary_search_tree/Removal) and verify if this is the proper way to delete a node. – untilted Oct 11 '15 at 18:50
  • The link you provided is also correct and, in fact, slightly better because it is keeping the depth of the tree as low as possible. The way explained it is the way your code is doing it, but the way the that your link describes it is also correct (and better). You just need to step through the code slowly to see what it is doing. – brandenesmith Oct 11 '15 at 19:28