实现BinarySearchTree,感觉比前面的2,3,4Tree要简单很多。
part1实现find:
private BinaryTreeNode findHelper(Comparable key, BinaryTreeNode node) { BinaryTreeNode newnode=node; if(key.compareTo(node.entry.key)==0) return node; else if(key.compareTo(node.entry.key)<0){ if(node.leftChild==null) return null; else return(findHelper(key,node.leftChild)); } else if(key.compareTo(node.entry.key)>0){ if(node.rightChild==null) return null; else return(findHelper(key,node.rightChild)); } else return null; }
part2:实现remove,分为无child,有一个child,有两个child的情况,注意root的特殊性,root无parent,要特殊处理,之前也碰到过因为没考虑root原因产生的bug。
代码:
1 public Entry remove(Object key) { 2 BinaryTreeNode node=findHelper((Comparable)key,root); 3 if(node==null) 4 return null; 5 else{ 6 Entry e=new Entry(node.entry.key,node.entry.value); 7 if(node.leftChild==null&&node.rightChild==null){ 8 if(node.parent==null) 9 root=null; 10 else{if(node==node.parent.leftChild) 11 node.parent.leftChild=null; 12 else if(node==node.parent.rightChild) 13 node.parent.rightChild=null; 14 } 15 } 16 else if(node.leftChild!=null&&node.rightChild==null){ 17 if(node.parent==null){ 18 node.leftChild.parent=null; 19 root=node.leftChild; 20 } 21 else{if(node.parent.leftChild==node){ 22 node.parent.leftChild=node.leftChild; 23 node.leftChild.parent=node.parent;} 24 else if(node.parent.rightChild==node){ 25 node.parent.rightChild=node.leftChild; 26 node.leftChild.parent=node.parent; 27 } 28 } 29 } 30 else if(node.leftChild==null&&node.rightChild!=null){ 31 if(node.parent==null){ 32 node.rightChild.parent=null; 33 root=node.rightChild; 34 } 35 else{ 36 if(node==node.parent.rightChild) 37 node.parent.rightChild=node.rightChild; 38 else if(node==node.parent.leftChild) 39 node.parent.leftChild=node.rightChild; 40 node.rightChild.parent=node.parent; 41 } 42 } 43 else if(node.leftChild!=null&&node.rightChild!=null){ 44 BinaryTreeNode newnode=node.rightChild; 45 while(newnode.leftChild!=null) 46 newnode=newnode.leftChild; 47 node.entry=new Entry(newnode.entry.key,newnode.entry.value); 48 if(newnode.rightChild==null){ 49 if(newnode==newnode.parent.leftChild) 50 newnode.parent.leftChild=null; 51 else if(newnode==newnode.parent.rightChild) 52 newnode.parent.rightChild=null; 53 } 54 else if(newnode.rightChild!=null){ 55 if(newnode==newnode.parent.leftChild) 56 newnode.parent.leftChild=newnode.rightChild; 57 else if(newnode==newnode.parent.rightChild) 58 newnode.parent.rightChild=newnode.rightChild; 59 newnode.rightChild.parent=newnode.parent; 60 61 } 62 } 63 size--; 64 return e; 65 } 66 }
运行结果:
Inserting 1A, 6V, 3K, 2Z, 5L, 9L: The tree is: 1A(((2Z)3K(5L))6V(9L)) Size: 6 Testing find() ... Calling find() on 1 returned A. Calling find() on 9 returned L. Calling find() on 5 returned L. Calling find() on 4 returned null. Calling find() on 6 returned V. Calling find() on 3 returned K. Testing remove() (for nodes with < 2 children) ... After remove(5): 1A(((2Z)3K)6V(9L)) After remove(3): 1A((2Z)6V(9L)) After remove(1): (2Z)6V(9L) After inserting 7S, 8X, 10B: (2Z)6V((7S(8X))9L(10B)) Size: 6 Testing remove() (for nodes with 2 children) ... After remove(6): (2Z)7S((8X)9L(10B)) After remove(9): (2Z)7S((8X)10B) Size: 4
时间: 2024-10-12 17:57:34