java - Delete a node in Binary Search Tree -
i have test code bst. bst created, node deletion not working properly. suggest if below delete code correct or modification in delete method helpful.
public class binarysearchtree { public binarysearchtree() { super(); } private class binarysearchtreenode{ private int data; private binarysearchtreenode left; private binarysearchtreenode right; public binarysearchtreenode(){ } public binarysearchtreenode(int data){ this.data = data; } public void setdata(int data) { this.data = data; } public int getdata() { return data; } public void setleft(binarysearchtree.binarysearchtreenode left) { this.left = left; } public binarysearchtree.binarysearchtreenode getleft() { return left; } public void setright(binarysearchtree.binarysearchtreenode right) { this.right = right; } public binarysearchtree.binarysearchtreenode getright() { return right; } } public binarysearchtreenode insertrec(binarysearchtreenode root,int data){ if(root == null){ root = new binarysearchtreenode(); root.setdata(data); root.setleft(null); root.setright(null); }else{ if(data < root.getdata()) root.setleft(insertrec(root.getleft(), data)); else if(data > root.getdata()) root.setright(insertrec(root.getright(), data)); } return root; } public void insertnonrec(binarysearchtreenode root,int data){ if(root == null){ root = new binarysearchtreenode(data); root.setleft(null); root.setright(null); }else{ if(data < root.getdata()){ if(root.getleft() != null){ insertnonrec(root.getleft(),data); }else{ root.setleft(new binarysearchtreenode(data)); } }else if(data > root.getdata()){ if(root.getright() != null){ insertnonrec(root.getright(), data); }else{ root.setright(new binarysearchtreenode(data)); } } } } public void delete(binarysearchtreenode root,int data){ binarysearchtreenode temp; if(root == null){ system.out.println("no elemets in bst."); }else if(data < root.getdata()){ delete(root.getleft(),data); }else if(data > root.getdata()){ delete(root.getright(),data); }else{ if((root.getleft() != null) && (root.getright() != null)){ // replace largest in left subtree temp = findmax(root.getleft()); root.setdata(temp.getdata()); delete(root.getleft(),temp.getdata()); }else if(root.getleft() != null || root.getright() != null){ // 1 child if(root.getleft() == null){ //root = root.getright(); root.setdata(root.getright().getdata()); root.setright(null); }else if(root.getright() == null){ //root = root.getleft(); root.setdata(root.getleft().getdata()); root.setleft(null); } }else{ root = null; } } } public binarysearchtreenode findmax(binarysearchtreenode root){ if(root == null) return null; while(root.getright() != null) root = root.getleft(); return root; } public binarysearchtreenode findmin(binarysearchtreenode root){ if(root == null) return null; while(root.getleft() != null) root = root.getleft(); return root; } public void inorderrec(binarysearchtreenode root){ if(root != null){ inorderrec(root.getleft()); system.out.print(root.getdata() + " "); inorderrec(root.getright()); } } public static void main(string args[]){ binarysearchtree tree = new binarysearchtree(); binarysearchtreenode root = tree.insertrec(null, 10); tree.insertnonrec(root, 5); tree.insertnonrec(root, 20); tree.insertnonrec(root, 4); tree.insertnonrec(root, 8); tree.insertnonrec(root, 12); tree.insertnonrec(root, 30); tree.insertnonrec(root, 11); tree.insertnonrec(root, 13); system.out.println("inorder traversal :"); tree.inorderrec(root); tree.delete(root, 20); system.out.println(""); system.out.println("inorder traversal :"); tree.inorderrec(root); } }
output :
inorder traversal : 4 5 8 10 11 12 13 20 30 inorder traversal : 4 5 8 10 11 12 13 11 30
root = null;
doesn't think does. changes value of local variable only. not change tree. brief metaphor:
think of classroom of students. students nodes in tree. point @ each other, defines parenthood in tree. if student (say john, i.e. parameter of function) come along , point @ 1 of students in 'tree' (say sarah), saying
root = null;
equivalent john pointing nowhere, not change sarah nor other students pointing at.
sure there holes in metaphor, hope basic idea.
you instead need node.setleft(null);
or node.setright(null);
change tree.
this require quite few changes, i'll leave figure out (this or this may of help), note you'll have check left , right children instead of (just) root.
i suggest take @ red-black trees (or similar) provide more efficient means of deleting nodes , keeps tree balanced.
Comments
Post a Comment