1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>二叉查找树计数</title> 6 </head> 7 <body> 8 <script> 9 function Node(data,left,right){ 10 this.data = data; 11 this.count = 1; 12 this.left = left; 13 this.right = right; 14 this.show = show; 15 } 16 17 function show(){ 18 return this.data; 19 } 20 21 function BST(){ 22 this.update = update; 23 this.root = null; 24 this.insert = insert; 25 this.inOrder = inOrder; 26 this.preOrder = preOrder; 27 this.postOrder = postOrder; 28 this.getMin = getMin; 29 this.getMax = getMax; 30 this.find = find; 31 this.remove = remove; 32 } 33 34 //不存在存进去 存在计数加1 35 function insert(data){ 36 if(this.find(data)){ 37 this.find(data).count++; 38 return 2; 39 } 40 var n = new Node(data,null,null); 41 if(this.root == null ){ 42 this.root = n; 43 console.log(this.root,"insert"); 44 }else{ 45 var current = this.root; 46 var parent; 47 while(true){ 48 parent = current; 49 if(data < current.data){ 50 current = current.left; 51 52 if(current == null ){ 53 parent.left = n; 54 break; 55 } 56 }else{ 57 current = current.right; 58 if(current == null ){ 59 parent.right = n; 60 break; 61 } 62 } 63 } 64 } 65 } 66 //中序遍历 67 function inOrder(node){ 68 if(node !=null){ 69 inOrder(node.left); 70 console.log(node.show(),"zhongxu") 71 inOrder(node.right); 72 } 73 } 74 //先序遍历 75 function preOrder(node){ 76 if(node !=null){ 77 console.log(node.show()); 78 preOrder(node.left); 79 preOrder(node.right); 80 } 81 } 82 83 //后序遍历 84 function postOrder(node){ 85 if(node != null){ 86 postOrder(node.left); 87 postOrder(node.right); 88 console.log(node.show()); 89 } 90 } 91 // 查找最小值 92 function getMin(){ 93 var current = this.root; 94 while (current.left != null){ 95 current = current.left; 96 } 97 return current.data; 98 } 99 //查找最大值 100 function getMax(){ 101 var current = this.root; 102 while(current.right != null){ 103 current = current.right; 104 } 105 return current.data; 106 } 107 108 //查找给定值 109 function find(data){ 110 var current = this.root; 111 while(current != null){ 112 if(data == current.data){ 113 return current; 114 }else if(data>current.data){ 115 current = current.right; 116 }else{ 117 current = current.left; 118 } 119 } 120 return null; 121 } 122 123 124 //删除节点 125 126 function remove(data){ 127 root = removeNode(this.root,data); 128 } 129 function removeNode(node,data){ 130 if(node == null){ 131 return null; 132 } 133 if(data == node.data){ 134 //没有子节点的节点 135 if(node.left == null && node.right == null){ 136 return null; 137 } 138 //没有左子节点 139 if(node.left == null){ 140 return node.right; 141 } 142 //没有右子节点 143 if(node.right == null){ 144 return node.left; 145 } 146 //有两个子节点的节点 147 var temp = getSmall(node.right); 148 node.data = temp.data; 149 node.right = removeNode(node.right,temp.data); 150 return node; 151 }else if(data < node.data){ 152 node.left = removeNode(node.left,data); 153 return node; 154 }else{ 155 node.right = removeNode(node.right,data); 156 return node; 157 } 158 } 159 //获取最小值 160 function getSmall(node){ 161 console.log("123456") 162 if(node.left ==null && node.right == null ){ 163 return node; 164 } 165 while(node.left !=null ){ 166 node = node.left; 167 } 168 return node; 169 } 170 171 //更新计数 172 function update(data){ 173 var grade = this.find(data); 174 grade.count++; 175 return grade; 176 } 177 var obj = new BST(); 178 obj.insert(15); 179 obj.insert(22); 180 console.log(obj.insert(22),"计数"); 181 obj.insert(9); 182 obj.insert(30); 183 obj.inOrder(obj.root); 184 obj.preOrder(obj.root); 185 obj.postOrder(obj.root); 186 console.log(obj.getMin(),"最小值"); 187 console.log(obj.getMax(),"最大值"); 188 189 console.log(obj.find(22)); 190 191 192 obj.remove(15); 193 obj.inOrder(obj.root); 194 </script> 195 </body> 196 </html>