JavaScript数据结构-15.二叉树

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

 

posted @ 2017-07-16 21:30  橙云生  阅读(186)  评论(0编辑  收藏  举报