BinaryTree.js 5.0 KB

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