Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. class BinarySearchTree {
  2. constructor(key=null, value=null, parent=null) {
  3. this.key = key;
  4. this.value = value;
  5. this.parent = parent;
  6. this.left = null;
  7. this.right = null;
  8. }
  9.  
  10. insert(key, value) {
  11. if (this.key === null) {
  12. this.key = key;
  13. this.value = value;
  14. }
  15. else if (key < this.key) {
  16. if (this.left === null) {
  17. this.left = new BinarySearchTree(key, value, this);
  18. }
  19. else {
  20. this.left.insert(key, value);
  21. }
  22. }
  23. else {
  24. if (this.right === null) {
  25. this.right = new BinarySearchTree(key, value, this);
  26. }
  27. else {
  28. this.right.insert(key, value);
  29. }
  30. }
  31. }
  32.  
  33. get(key) {
  34. if (this.key === key) {
  35. return this.value;
  36. }
  37. else if (key < this.key && this.left) {
  38. return this.left.get(key);
  39. }
  40. else if (key > this.key && this.right) {
  41. return this.right.get(key);
  42. }
  43. else {
  44. throw new Error('Key Error');
  45. }
  46. }
  47.  
  48. remove(key) {
  49. if (this.key === key) {
  50. if (this.left && this.right) {
  51. const successor = this.right._findMin();
  52. this.key = successor.key;
  53. this.value = successor.value;
  54. successor.remove(successor.key);
  55. }
  56. else if (this.left) {
  57. this._replaceWith(this.left);
  58. }
  59. else if (this.right) {
  60. this._replaceWith(this.right);
  61. }
  62. else {
  63. this._replaceWith(null);
  64. }
  65. }
  66. else if (key < this.key && this.left) {
  67. this.left.remove(key);
  68. }
  69. else if (key > this.key && this.right) {
  70. this.right.remove(key);
  71. }
  72. else {
  73. throw new Error('Key Error');
  74. }
  75. }
  76. // IF 15 has a parent
  77. // If 15 is to the left of the parent, then point 15's parent left pointer to node,
  78. // If 15 is to the right of the parent, then point 15's parent right pointer to node.
  79.  
  80. // Make the Parent of the node, the parent of the one we replaced.
  81.  
  82. // else its the root node.
  83.  
  84. _replaceWith(node) {
  85. if (this.parent) {
  86. if (this == this.parent.left) {
  87. this.parent.left = node;
  88. }
  89. else if (this == this.parent.right) {
  90. this.parent.right = node;
  91. }
  92.  
  93. if (node) {
  94. node.parent = this.parent;
  95. }
  96. }
  97. else {
  98. if (node) {
  99. this.key = node.key;
  100. this.value = node.value;
  101. this.left = node.left;
  102. this.right = node.right;
  103. }
  104. else {
  105. this.key = null;
  106. this.value = null;
  107. this.left = null;
  108. this.right = null;
  109. }
  110. }
  111. }
  112.  
  113. _findMin() {
  114. if (!this.left) {
  115. return this;
  116. }
  117. return this.left._findMin();
  118. }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. let myTree = new BinarySearchTree;
  125.  
  126. //E A S Y Q U E S T I O N
  127. // E
  128. // S A
  129. // Y S E
  130. // Q I
  131. // U O
  132. // T N
  133.  
  134. myTree.insert('E');
  135. myTree.insert('A');
  136. myTree.insert('S');
  137. myTree.insert('Y');
  138. myTree.insert('Q');
  139. myTree.insert('U');
  140. myTree.insert('E');
  141. myTree.insert('S');
  142. myTree.insert('T');
  143. myTree.insert('I');
  144. myTree.insert('O');
  145. myTree.insert('N');
  146.  
  147. // console.log(myTree);
  148.  
  149. //Write an algorithm to find the height of a binary search tree
  150.  
  151. let myTree2 = new BinarySearchTree;
  152.  
  153. myTree2.insert(10);
  154. myTree2.insert(5);
  155. myTree2.insert(13);
  156. myTree2.insert(7);
  157. myTree2.insert(14);
  158. myTree2.insert(12);
  159. myTree2.insert(3);
  160. myTree2.insert(15);
  161.  
  162.  
  163.  
  164.  
  165. function findHeight(tree) {
  166.  
  167.  
  168. if(tree.left && tree.right) {
  169.  
  170. return Math.max(findHeight(tree.left), findHeight(tree.right)) + 1;
  171. }
  172. if(tree.left) {
  173.  
  174. return findHeight(tree.left) + 1;
  175. }
  176. if(tree.right) {
  177.  
  178. return findHeight(tree.right) + 1;
  179. }
  180.  
  181. return 1;
  182. }
  183.  
  184.  
  185.  
  186. // console.log(findHeight(myTree2));
  187.  
  188. // take 2
  189. function getHeight(tree){
  190. return Math.max(tree.left && getHeight(tree.left), tree.right && getHeight(tree.right)) + 1;
  191. }
  192.  
  193. // console.log(getHeight(myTree2));
  194. // take 3
  195. function bst_height(tree) {
  196. if(tree.left && tree.right) return Math.max(bst_height(tree.left), bst_height(tree.right)) + 1;
  197. if(tree.left) return bst_height(tree.left) + 1;
  198. if(tree.right) return bst_height(tree.right) + 1;
  199.  
  200. return 1;
  201. }
  202.  
  203. // console.log(bst_height(myTree2));
  204. // Write an algorithm to check whether an arbitrary binary tree is a binary search tree, assuming the tree does not contain duplicates
  205.  
  206. //if this.left > current key then return false
  207. ////if this.right < current key then return false
  208. //
  209.  
  210.  
  211. // take 2 is_bst?
  212. function is_bst(tree) {
  213.  
  214. if((tree.left === undefined) || (tree.right === undefined)) return false;
  215.  
  216. if(tree.children > 2) {
  217. return false;
  218. }
  219.  
  220. if(tree.left) {
  221. if(tree.left.key > tree.key) return false;
  222. if(!is_bst(tree.left)) return false;
  223. }
  224.  
  225. if(tree.right) {
  226. if(tree.right.key < tree.key) return false;
  227. if(!is_bst(tree.right)) return false;
  228. }
  229.  
  230. return true;
  231. }
  232.  
  233. console.log(is_bst(myTree2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement