Advertisement
Adeeb

Old Code

Apr 13th, 2013
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1.     /*
  2.     Method for deleting a particular node
  3.     */
  4.     Node parent;
  5.     int WeHaveSubtree=0;
  6.     public void rm(int ID,Node currentNode){
  7.         if ((currentNode.getId()<ID)&&(currentNode.getRightLink()!=null)){
  8.             //if the item is in the right substree
  9.             parent=currentNode;
  10.             WeHaveSubtree++;
  11.             rm(ID,currentNode.getRightLink());
  12.         }
  13.         else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()==null)&&(getRoot().getLeftLink()==null)){
  14.             //if we are deleting a tree with only a root
  15.             root = null;
  16.             System.out.println("Deleted");
  17.         }
  18.         else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()==null)&&(getRoot().getLeftLink()!=null)){
  19.             //if you are deleting the root and it only has a left child
  20.             root=getRoot().getLeftLink();
  21.             System.out.println("Deleted");
  22.         }
  23.         else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()!=null)){
  24.             //if you are deleting the root and it has a right child
  25.             Replace(getRoot(),getMin(getRoot().getRightLink()));
  26.             reallyRemoveMin(getRoot().getRightLink());
  27.             System.out.println("Deleted");
  28.         }
  29.         else if ((currentNode.getId()>ID)&&(currentNode.getLeftLink()!=null)){
  30.             //if the item is the left substree
  31.             parent=currentNode;
  32.             WeHaveSubtree++;
  33.             rm(ID,currentNode.getLeftLink());
  34.         }
  35.         else if ((currentNode.getId()>ID)&&(currentNode.getLeftLink()==null)){
  36.             //if the item is supposed to be the left child but the node has no left child
  37.             System.out.println("There is no bigram with id "+ID+" in the tree");
  38.             }
  39.         else if ((currentNode.getId()<ID)&&(currentNode.getRightLink()==null)){
  40.             //if the item was supposed to be the right child but node has no right children
  41.             System.out.println("There is no bigram with id "+ID+" in the tree");
  42.         }
  43.         else if (currentNode.getId()==ID){
  44.             //if the node is found
  45.             if (WeHaveSubtree==0){
  46.                 parent = getRoot();
  47.             }
  48.             //first condition---if item has a left child
  49.             if ((currentNode.getLeftLink()!=null)&&(currentNode.getRightLink()==null)){
  50.                 if (parent.getLeftLink()==currentNode){
  51.                     //if the node we are deleting is in the left subtree of its parent
  52.                     WeHaveSubtree=0;
  53.                     parent.setLeftLink(currentNode.getLeftLink());
  54.                     System.out.println("Deleted");
  55.                 }
  56.                 else{
  57.                     WeHaveSubtree=0;
  58.                     parent.setRightLink(currentNode.getLeftLink());
  59.                     System.out.println("Deleted");
  60.                 }
  61.             }
  62.             //second condition--if item has only a right child
  63.             else if ((currentNode.getRightLink()!=null)&&(currentNode.getLeftLink()==null)){
  64.                 if (parent.getLeftLink()==currentNode){
  65.                     WeHaveSubtree=0;
  66.                     parent.setLeftLink(currentNode.getRightLink());
  67.                     System.out.println("Deleted");
  68.                 }
  69.                 else{
  70.                     WeHaveSubtree=0;
  71.                     parent.setRightLink(currentNode.getRightLink());
  72.                     System.out.println("Deleted");
  73.                 }
  74.             }
  75.             //third condition--if item has no children at all
  76.             else if ((currentNode.getLeftLink()==null)&&(currentNode.getRightLink()==null)){
  77.                 if (parent.getLeftLink()==currentNode){
  78.                     //if item was the left child of the parent
  79.                     parent.setLeftLink(null);
  80.                     WeHaveSubtree=0;
  81.                     System.out.println("Deleted");
  82.                 }
  83.                 else{
  84.                     //it was a right child to its parent
  85.                     WeHaveSubtree=0;
  86.                     parent.setRightLink(null);
  87.                     System.out.println("Deleted");
  88.                 }
  89.             }
  90.             //forth condition--if item has two children
  91.             else if ((currentNode.getLeftLink()!=null)&&(currentNode.getRightLink()!=null)){
  92.                 Replace(currentNode,getMin(currentNode.getRightLink()));
  93.                 reallyRemoveMin(currentNode.getRightLink());
  94.                 System.out.println("Deleted");
  95.                
  96.             }
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement