java - Bst tree delete() method doesnt work -


i implementing binary search tree , wondering, why delete() method doesn't work... findmin() method work, have tested out far. type in key, doesn't exist in 3 , right exception, whenever type in key, existing, doesn't remove node three... here code far:

import java.util.nosuchelementexception;  public class bst {  node root; node head; node tail;   public bst(){     root = null; }  public void insert (node root, int key){     node newnode=new node(key);      if(root==null){         root=newnode;     }      if(key<=root.getkey()){         if (root.getleft()!=null){             insert(root.getleft(), key);         }         else{             root.setleft(newnode);         }     }      if (key>=root.getkey()){         if (root.getright()!=null){             insert(root.getright(),key);         }          else{             root.setright(newnode);         }      }       }  public void printtree(node root){     if (root==null) return;     printtree(root.getleft());     system.out.print(root.getkey() + " ");     printtree(root.getright()); }  public node treetocdll(node root){     if (root == null){         return null;     }      node lefttree=treetocdll(root.getleft());     node righttree=treetocdll(root.getright());       if (lefttree == null){         head=root;     }      else {         head=lefttree;         lefttree.getleft().setright(root);         root.setleft(lefttree.getleft());     }      if (righttree==null){         head.setleft(root);         root.setright(head);         tail=root;     }      else{         tail=righttree.getleft();         head.setleft(tail);         tail.setright(head);         root.setright(righttree);         righttree.setleft(root);     }      return head; }  public boolean find(node root, int key){     node current=root;      while(current!=null){          if(current.getkey()==key){             return true;         }         else if(current.getkey()>key){             current=current.getleft();         }          else             current=current.getright();         }     return false; }  public void printlist(node head){     node current = head;      while(current!=null){         system.out.print(current.getkey() + " ");         current=current.getright();         if(current==head) break;     } }  public node findmin(node root){     node current=root;     if(root==null) return null;     else{     if(current.getleft()!=null){         return findmin(current.getleft());     }     }     return current;  }  public void delete(node root, int key){     node current=root;      if(root==null){         throw new nosuchelementexception("baum ist leer");     }      else{         if(current.getkey()>key){             delete(current.getleft(), key);         }          else if(current.getkey()<key){             delete(current.getright(),key);         }          else{             if(current.getleft()==null && root.getright()==null){                 current=null;             }              else if(current.getleft()==null){                 node tmp=current;                 current=current.getright();                 tmp=null;             }              else if(current.getright()==null){                 node tmp=current;                 current=current.getleft();                 tmp=null;             }              else {                 node min=findmin(current.getright());                 node tmp=current;                 current=min;                 tmp=null;              }         }     } } public static void main (string[]args){      bst bst=new bst();      node root=new node(4);     bst.insert(root, 2);     bst.insert(root, 10);     bst.insert(root, 3);     bst.insert(root, 5);     bst.insert(root, 6);     bst.insert(root, 0);      bst.delete(root, 2);      system.out.print("in-order traversal: ");     bst.printtree(root);     system.out.println();     system.out.print("der gesuchte knoten : " + bst.find(root,7));     system.out.println();     system.out.print("der kleinste knoten : " + bst.findmin(root));     system.out.println();        system.out.print("circular doubly linked list: ");     node head= bst.treetocdll(root);     bst.printlist(head);        }  } 

node class:

public class node {      private node left;     private node right;     private int key;      public node(int key){         this.key=key;         left = null;         right = null;     }      public void setleft(node left){         this.left=left;     }      public node getleft(){         return left;     }      public void setright(node right){         this.right=right;     }      public node getright(){         return right;     }      public int getkey(){         return key;     }  } 

i glad if me... tried out find solution whole day, seems won't find on own

here, doing current=min not changing node in tree, replacing local pointer (current). either rewrite values of node setters, of update getleft on parent of node.

 else {      node min=findmin(current.getright());      node tmp=current;      current=min;      tmp=null;  } 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -