Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Method for deleting a particular node
- */
- Node parent;
- int WeHaveSubtree=0;
- public void rm(int ID,Node currentNode){
- if ((currentNode.getId()<ID)&&(currentNode.getRightLink()!=null)){
- //if the item is in the right substree
- parent=currentNode;
- WeHaveSubtree++;
- rm(ID,currentNode.getRightLink());
- }
- else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()==null)&&(getRoot().getLeftLink()==null)){
- //if we are deleting a tree with only a root
- root = null;
- System.out.println("Deleted");
- }
- else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()==null)&&(getRoot().getLeftLink()!=null)){
- //if you are deleting the root and it only has a left child
- root=getRoot().getLeftLink();
- System.out.println("Deleted");
- }
- else if ((getRoot().getId()==ID)&&(getRoot().getRightLink()!=null)){
- //if you are deleting the root and it has a right child
- Replace(getRoot(),getMin(getRoot().getRightLink()));
- reallyRemoveMin(getRoot().getRightLink());
- System.out.println("Deleted");
- }
- else if ((currentNode.getId()>ID)&&(currentNode.getLeftLink()!=null)){
- //if the item is the left substree
- parent=currentNode;
- WeHaveSubtree++;
- rm(ID,currentNode.getLeftLink());
- }
- else if ((currentNode.getId()>ID)&&(currentNode.getLeftLink()==null)){
- //if the item is supposed to be the left child but the node has no left child
- System.out.println("There is no bigram with id "+ID+" in the tree");
- }
- else if ((currentNode.getId()<ID)&&(currentNode.getRightLink()==null)){
- //if the item was supposed to be the right child but node has no right children
- System.out.println("There is no bigram with id "+ID+" in the tree");
- }
- else if (currentNode.getId()==ID){
- //if the node is found
- if (WeHaveSubtree==0){
- parent = getRoot();
- }
- //first condition---if item has a left child
- if ((currentNode.getLeftLink()!=null)&&(currentNode.getRightLink()==null)){
- if (parent.getLeftLink()==currentNode){
- //if the node we are deleting is in the left subtree of its parent
- WeHaveSubtree=0;
- parent.setLeftLink(currentNode.getLeftLink());
- System.out.println("Deleted");
- }
- else{
- WeHaveSubtree=0;
- parent.setRightLink(currentNode.getLeftLink());
- System.out.println("Deleted");
- }
- }
- //second condition--if item has only a right child
- else if ((currentNode.getRightLink()!=null)&&(currentNode.getLeftLink()==null)){
- if (parent.getLeftLink()==currentNode){
- WeHaveSubtree=0;
- parent.setLeftLink(currentNode.getRightLink());
- System.out.println("Deleted");
- }
- else{
- WeHaveSubtree=0;
- parent.setRightLink(currentNode.getRightLink());
- System.out.println("Deleted");
- }
- }
- //third condition--if item has no children at all
- else if ((currentNode.getLeftLink()==null)&&(currentNode.getRightLink()==null)){
- if (parent.getLeftLink()==currentNode){
- //if item was the left child of the parent
- parent.setLeftLink(null);
- WeHaveSubtree=0;
- System.out.println("Deleted");
- }
- else{
- //it was a right child to its parent
- WeHaveSubtree=0;
- parent.setRightLink(null);
- System.out.println("Deleted");
- }
- }
- //forth condition--if item has two children
- else if ((currentNode.getLeftLink()!=null)&&(currentNode.getRightLink()!=null)){
- Replace(currentNode,getMin(currentNode.getRightLink()));
- reallyRemoveMin(currentNode.getRightLink());
- System.out.println("Deleted");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement