1 | define(["dojo/_base/kernel", "dojo/_base/array", "./_base"], function(dojo, darray, dxc){ |
---|
2 | /*===== |
---|
3 | var dxc = dojox.collections; |
---|
4 | =====*/ |
---|
5 | dxc.BinaryTree=function(data){ |
---|
6 | function node(data, rnode, lnode){ |
---|
7 | this.value=data||null; |
---|
8 | this.right=rnode||null; |
---|
9 | this.left=lnode||null; |
---|
10 | this.clone=function(){ |
---|
11 | var c=new node(); |
---|
12 | if(this.value.value){ |
---|
13 | c.value=this.value.clone(); |
---|
14 | }else{ |
---|
15 | c.value=this.value; |
---|
16 | } |
---|
17 | if(this.left!=null){ |
---|
18 | c.left=this.left.clone(); |
---|
19 | } |
---|
20 | if(this.right!=null){ |
---|
21 | c.right=this.right.clone(); |
---|
22 | } |
---|
23 | return c; |
---|
24 | } |
---|
25 | this.compare=function(n){ |
---|
26 | if(this.value>n.value){ return 1; } |
---|
27 | if(this.value<n.value){ return -1; } |
---|
28 | return 0; |
---|
29 | } |
---|
30 | this.compareData=function(d){ |
---|
31 | if(this.value>d){ return 1; } |
---|
32 | if(this.value<d){ return -1; } |
---|
33 | return 0; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | function inorderTraversalBuildup(current, a){ |
---|
38 | if(current){ |
---|
39 | inorderTraversalBuildup(current.left, a); |
---|
40 | a.push(current.value); |
---|
41 | inorderTraversalBuildup(current.right, a); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | function preorderTraversal(current, sep){ |
---|
46 | var s=""; |
---|
47 | if (current){ |
---|
48 | s=current.value.toString() + sep; |
---|
49 | s+=preorderTraversal(current.left, sep); |
---|
50 | s+=preorderTraversal(current.right, sep); |
---|
51 | } |
---|
52 | return s; |
---|
53 | } |
---|
54 | function inorderTraversal(current, sep){ |
---|
55 | var s=""; |
---|
56 | if (current){ |
---|
57 | s=inorderTraversal(current.left, sep); |
---|
58 | s+=current.value.toString() + sep; |
---|
59 | s+=inorderTraversal(current.right, sep); |
---|
60 | } |
---|
61 | return s; |
---|
62 | } |
---|
63 | function postorderTraversal(current, sep){ |
---|
64 | var s=""; |
---|
65 | if (current){ |
---|
66 | s=postorderTraversal(current.left, sep); |
---|
67 | s+=postorderTraversal(current.right, sep); |
---|
68 | s+=current.value.toString() + sep; |
---|
69 | } |
---|
70 | return s; |
---|
71 | } |
---|
72 | |
---|
73 | function searchHelper(current, data){ |
---|
74 | if(!current){ return null; } |
---|
75 | var i=current.compareData(data); |
---|
76 | if(i==0){ return current; } |
---|
77 | if(i>0){ return searchHelper(current.left, data); } |
---|
78 | else{ return searchHelper(current.right, data); } |
---|
79 | } |
---|
80 | |
---|
81 | this.add=function(data){ |
---|
82 | var n=new node(data); |
---|
83 | var i; |
---|
84 | var current=root; |
---|
85 | var parent=null; |
---|
86 | while(current){ |
---|
87 | i=current.compare(n); |
---|
88 | if(i==0){ return; } |
---|
89 | parent=current; |
---|
90 | if(i>0){ current=current.left; } |
---|
91 | else{ current=current.right; } |
---|
92 | } |
---|
93 | this.count++; |
---|
94 | if(!parent){ |
---|
95 | root=n; |
---|
96 | }else{ |
---|
97 | i=parent.compare(n); |
---|
98 | if(i>0){ |
---|
99 | parent.left=n; |
---|
100 | }else{ |
---|
101 | parent.right=n; |
---|
102 | } |
---|
103 | } |
---|
104 | }; |
---|
105 | this.clear=function(){ |
---|
106 | root=null; |
---|
107 | this.count=0; |
---|
108 | }; |
---|
109 | this.clone=function(){ |
---|
110 | var c=new dxc.BinaryTree(); |
---|
111 | var itr=this.getIterator(); |
---|
112 | while(!itr.atEnd()){ |
---|
113 | c.add(itr.get()); |
---|
114 | } |
---|
115 | return c; |
---|
116 | }; |
---|
117 | this.contains=function(data){ |
---|
118 | return this.search(data) != null; |
---|
119 | }; |
---|
120 | this.deleteData=function(data){ |
---|
121 | var current=root; |
---|
122 | var parent=null; |
---|
123 | var i=current.compareData(data); |
---|
124 | while(i!=0&¤t!=null){ |
---|
125 | if(i>0){ |
---|
126 | parent=current; |
---|
127 | current=current.left; |
---|
128 | }else if(i<0){ |
---|
129 | parent=current; |
---|
130 | current=current.right; |
---|
131 | } |
---|
132 | i=current.compareData(data); |
---|
133 | } |
---|
134 | if(!current){ return; } |
---|
135 | this.count--; |
---|
136 | if(!current.right){ |
---|
137 | if(!parent){ |
---|
138 | root=current.left; |
---|
139 | }else{ |
---|
140 | i=parent.compare(current); |
---|
141 | if(i>0){ parent.left=current.left; } |
---|
142 | else if(i<0){ parent.right=current.left; } |
---|
143 | } |
---|
144 | } |
---|
145 | else if(!current.right.left){ |
---|
146 | if(!parent){ |
---|
147 | root=current.right; |
---|
148 | }else{ |
---|
149 | i=parent.compare(current); |
---|
150 | if(i>0){ parent.left=current.right; } |
---|
151 | else if(i<0){ parent.right=current.right; } |
---|
152 | } |
---|
153 | } |
---|
154 | else{ |
---|
155 | var leftmost=current.right.left; |
---|
156 | var lmParent=current.right; |
---|
157 | while(leftmost.left!=null){ |
---|
158 | lmParent=leftmost; |
---|
159 | leftmost=leftmost.left; |
---|
160 | } |
---|
161 | lmParent.left=leftmost.right; |
---|
162 | leftmost.left=current.left; |
---|
163 | leftmost.right=current.right; |
---|
164 | if(!parent){ |
---|
165 | root=leftmost; |
---|
166 | }else{ |
---|
167 | i=parent.compare(current); |
---|
168 | if(i>0){ parent.left=leftmost; } |
---|
169 | else if(i<0){ parent.right=leftmost; } |
---|
170 | } |
---|
171 | } |
---|
172 | }; |
---|
173 | this.getIterator=function(){ |
---|
174 | var a=[]; |
---|
175 | inorderTraversalBuildup(root, a); |
---|
176 | return new dxc.Iterator(a); |
---|
177 | }; |
---|
178 | this.search=function(data){ |
---|
179 | return searchHelper(root, data); |
---|
180 | }; |
---|
181 | this.toString=function(order, sep){ |
---|
182 | if(!order){ order=dxc.BinaryTree.TraversalMethods.Inorder; } |
---|
183 | if(!sep){ sep=","; } |
---|
184 | var s=""; |
---|
185 | switch(order){ |
---|
186 | case dxc.BinaryTree.TraversalMethods.Preorder: |
---|
187 | s=preorderTraversal(root, sep); |
---|
188 | break; |
---|
189 | case dxc.BinaryTree.TraversalMethods.Inorder: |
---|
190 | s=inorderTraversal(root, sep); |
---|
191 | break; |
---|
192 | case dxc.BinaryTree.TraversalMethods.Postorder: |
---|
193 | s=postorderTraversal(root, sep); |
---|
194 | break; |
---|
195 | }; |
---|
196 | if(s.length==0){ return ""; } |
---|
197 | else{ return s.substring(0, s.length - sep.length); } |
---|
198 | }; |
---|
199 | |
---|
200 | this.count=0; |
---|
201 | var root=this.root=null; |
---|
202 | if(data){ |
---|
203 | this.add(data); |
---|
204 | } |
---|
205 | } |
---|
206 | dxc.BinaryTree.TraversalMethods={ |
---|
207 | Preorder: 1, Inorder: 2, Postorder: 3 |
---|
208 | }; |
---|
209 | return dxc.BinaryTree; |
---|
210 | }); |
---|