Advertisement
ptownhero

Untitled

Oct 25th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. // Binary Search Tree Code
  2. // Take a sorted Arrray increasing value
  3. // Divide it in the middle, thats the initial root.
  4. // Ignoring the middle root node, take the left and right half and make that the next roots.
  5. // Put the left root in the left node and the right root in the right node.
  6. // Continue this process until there is nothing left.
  7.  
  8.  
  9. function node(number) {
  10. return {
  11. left: null,
  12. right: null,
  13. value: number,
  14. }
  15. }
  16.  
  17. const merge = (left, right) => {
  18. let array = [];
  19. while (left.length && right.length) {
  20. if (left[0] < right[0]) {
  21. array.push(left.shift());
  22. }
  23. else {
  24. array.push(right.shift());
  25. }
  26. }
  27. return [...array, ...left, ...right];
  28. }
  29.  
  30. const mergeSort = (array) => {
  31. let mid = array.length / 2;
  32. if (array.length < 2) return array;
  33. let left = array.splice(0, mid);
  34. return merge(mergeSort(left), mergeSort(array));
  35. }
  36.  
  37. const prettyPrint = (node, prefix = '', isLeft = true) => {
  38. if (node.right !== null) {
  39. prettyPrint(node.right, `${prefix}${isLeft ? '│ ' : ' '}`, false);
  40. }
  41. console.log(`${prefix}${isLeft ? '└── ' : '┌── '}${node.value}`);
  42. if (node.left !== null) {
  43. prettyPrint(node.left, `${prefix}${isLeft ? ' ' : '│ '}`, true);
  44. }
  45. }
  46.  
  47. const createBST = value => {
  48. let count = 0;
  49.  
  50. const createNode = (value) => {
  51. return {
  52. value,
  53. left: null,
  54. right: null,
  55. }
  56. }
  57.  
  58. const insert = value => {
  59. count++;
  60.  
  61. let newNode = createNode(value);
  62.  
  63. const searchTree = node => {
  64. // If value < node.value, go left
  65. if (value < node.value) {
  66. // If there's no left child, append the new node
  67. if (!node.left) {
  68. node.left = newNode;
  69. }
  70. // Run the search again
  71. else {
  72. searchTree(node.left);
  73. }
  74. }
  75. // If value > node.value, go right
  76. else if (value > node.value) {
  77. // If node.right
  78. if (!node.right) {
  79. node.right = newNode;
  80. }
  81. else {
  82. searchTree(node.right);
  83. }
  84. }
  85. }
  86. searchTree(newNode);
  87. }
  88.  
  89. const size = () => {
  90. return this.count;
  91. }
  92.  
  93. // takes in the root node
  94. const min = () => {
  95. let currentNode = this.root;
  96. while (currentNode.left) {
  97. currentNode = currentNode.left
  98. }
  99. return currentNode.value;
  100. }
  101.  
  102. const max = () => {
  103. let currentNode = this.root;
  104. while (currentNode.right) {
  105. currentNode = currentNode.right;
  106. }
  107. return currentNode.value;
  108. }
  109.  
  110. const contains = value => {
  111. let currentNode = this.root;
  112.  
  113. while (currentNode) {
  114. if (value === currentNode.value) {
  115. return true;
  116. }
  117. if (value < currentNode.value) {
  118. currentNode = currentNode.left;
  119. }
  120. else {
  121. currentNode = currentNode.right;
  122. }
  123. }
  124.  
  125. return false;
  126. }
  127.  
  128. // Array 2, 3, 12, 15, 28, 36, 39
  129.  
  130. // Tree 15
  131. // 3 36
  132. // 2 12 28 39
  133.  
  134. // depth first search
  135.  
  136. // In-Order
  137. // left, root, right
  138. // 2, 3, 12, 15, 28, 36, 39
  139. const dfsInOrder = () => {
  140. let result = [];
  141.  
  142. const traverse = node => {
  143. if (node.left) traverse(node.left);
  144. result.push(node.value);
  145. if (node.right) traverse(node.right);
  146. }
  147. return result;
  148. }
  149.  
  150. // Pre-Order
  151. // root, left, right
  152. // 15, 3, 2, 12, 36, 28, 39
  153. const dfsPreOrder = () => {
  154. let result = [];
  155.  
  156. const traverse = node => {
  157. result.push(node.value);
  158. if (node.right) traverse(node.right);
  159. if (node.left) traverse(node.left);
  160. }
  161. return result;
  162. }
  163.  
  164. // Post-Order
  165. // right, root, left
  166. // 39, 36, 28, 15, 12, 3, 2
  167. const dfsPostOrder = () => {
  168. let result = [];
  169.  
  170. const traverse = node => {
  171. if (node.right) traverse(node.right);
  172. result.push(node.value);
  173. if (node.left) traverse(node.left);
  174.  
  175. }
  176. return result;
  177. }
  178.  
  179. // breadth first search
  180. const bfs = () => {
  181. let result = [];
  182. let queue = [];
  183.  
  184. queue.push(this.root);
  185. while (queue.length) {
  186. let currentNode = queue.shift();
  187. result.push(currentNode);
  188.  
  189. if (currentNode.left) {
  190. queue.push(currentNode.left);
  191. }
  192. if (currentNode.right) {
  193. queue.push(currentNode.right);
  194. }
  195. }
  196. return result;
  197. }
  198.  
  199. return {
  200. root: value,
  201. count: 0,
  202. insert,
  203. min,
  204. max,
  205. contains,
  206. dfsInOrder,
  207. dfsPostOrder,
  208. dfsPreOrder,
  209. bfs
  210. }
  211. }
  212.  
  213. // Array 2, 3, 12, 15, 28, 36, 39
  214.  
  215. // Tree 15
  216. // 3 36
  217. // 2 12 28 39
  218. const badArray = [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]
  219. const purgedDupes = [...new Set(badArray)];
  220. const sorted = mergeSort(purgedDupes);
  221. const bst = createBST(15);
  222. bst.insert(2);
  223. bst.insert(3);
  224. bst.insert(12);
  225. bst.insert(28);
  226. bst.insert(36);
  227. bst.insert(39);
  228. bst
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement