Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. class BSTNode {
  2. constructor(data, left = null, right = null) {
  3. this.data = data;
  4. this.left = left;
  5. this.right = right;
  6. }
  7.  
  8. insertNode(node) {
  9. let newNode = null;
  10. if (node.data <= this.data) {
  11. if (this.left) {
  12. return this.left.insertNode(node);
  13. }
  14.  
  15. newNode = new BSTNode(node.data);
  16. this.left = newNode;
  17.  
  18. return newNode;
  19.  
  20. } else if (node.data > this.data) {
  21. if (this.right) {
  22. return this.right.insertNode(node);
  23. }
  24.  
  25. newNode = new BSTNode(node.data);
  26. this.right = newNode;
  27.  
  28. return newNode;
  29. }
  30.  
  31. return this;
  32. }
  33.  
  34. findNode(data) {
  35. if (this.data === data) {
  36. return this;
  37. } else if (data < this.data && this.left) {
  38. return this.left.findNode(data);
  39. } else if (data > this.data && this.right) {
  40. return this.right.findNode(data);
  41. }
  42.  
  43. return null;
  44. }
  45.  
  46. findMinNode() {
  47. if (!this.left) {
  48. return this;
  49. }
  50.  
  51. return this.left.findMinNode();
  52. }
  53. }
  54.  
  55. class BinarySearchTree {
  56. constructor() {
  57. this.root = null;
  58. }
  59.  
  60. insert(data) {
  61. const newNode = new BSTNode(data);
  62.  
  63. if (!this.root) {
  64. this.root = newNode;
  65. return this;
  66. }
  67.  
  68. return this.root.insertNode(newNode);
  69. }
  70.  
  71. delete(target, root = this.root) {
  72. if (!root) {
  73. return root;
  74. } else if (target < root.data) {
  75. root.left = this.delete(target, root.left);
  76. } else if (target > root.data) {
  77. root.right = this.delete(target, root.right);
  78. } else {
  79. // No child
  80. if (!root.left && !root.right) {
  81. root = null;
  82. }
  83.  
  84. // One child
  85. else if (root.left == null) {
  86. root = root.right;
  87. } else if (root.right == null) {
  88. root = root.left;
  89. }
  90.  
  91. // Two children
  92. else {
  93. const minNode = this.findMin(root.right);
  94. root.data = minNode.data;
  95. root.right = this.delete(minNode.data, root.right);
  96. }
  97. }
  98.  
  99. return root;
  100. }
  101.  
  102. find(data) {
  103. if (!this.root) {
  104. return null;
  105. }
  106.  
  107. return this.root.findNode(data);
  108. }
  109.  
  110. findMin(root = this.root) {
  111. if (!root) {
  112. return null;
  113. }
  114.  
  115. return root.findMinNode();
  116. }
  117.  
  118. /**
  119. * Find the in-order successor of a node
  120. *
  121. * @param data The data of the node to find its successor
  122. * @returns {null|BSTNode}
  123. */
  124. findSuccessor(data) {
  125. let current = this.find(data);
  126.  
  127. if(!current) {
  128. return null;
  129. }
  130.  
  131. if(current.right) {
  132. let rightNode = current.right;
  133. return this.findMin(rightNode);
  134. }
  135.  
  136. else if(!current.right) {
  137. let ancestor = this.root;
  138. let successor = null;
  139.  
  140. while(ancestor !== current) {
  141. if(current.data < ancestor.data) {
  142. successor = ancestor;
  143. ancestor = ancestor.left;
  144. }
  145. else {
  146. ancestor = ancestor.right;
  147. }
  148. }
  149.  
  150. return successor;
  151. }
  152.  
  153.  
  154.  
  155. return null;
  156. }
  157.  
  158. height(root) {
  159. if (!root) {
  160. return -1;
  161. }
  162.  
  163. return Math.max(this.height(root.left), this.height(root.right)) + 1;
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement