Guest User

Untitled

a guest
Oct 15th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. //implementing BST
  2. //condition - parent node has two child nodes
  3. //left - smaller value
  4. //right - larger value
  5. // No duplicates assume
  6.  
  7. //max -depth of BST
  8. //find if a value is present or not
  9. //insert
  10. //update
  11. //delete
  12.  
  13. //Tree Travesal for everything - it is just visiting all nodes
  14. //Bread first search - BFS
  15. //Depth first search - DFS
  16. //pre-order
  17. //post-order
  18. //in-order
  19.  
  20. class Node {
  21. constructor(val) {
  22. this.val = val;
  23. this.child = [];
  24. }
  25. }
  26.  
  27. class BST {
  28. constructor() {
  29. this.root = null;
  30. }
  31.  
  32. insert(val) {
  33. const newNode = new Node(parent, val);
  34. if(this.root === null) {
  35. this.root = newNode;
  36. return this;
  37. }
  38. this.insertHelper(this.root, newNode);
  39. return this;
  40. }
  41.  
  42. bfs() {
  43. let tempQ = [];
  44. if(this.root === null) return;
  45. tempQ.push(this.root);
  46. while(tempQ.length !== 0) {
  47. let current = tempQ[0];
  48. if(current.left !== null) tempQ.push(current.left);
  49. if(current.right !== null) tempQ.push(current.right);
  50. console.log(current.val);
  51. tempQ.shift();
  52. console.log(tempQ);
  53. }
  54. }
  55.  
  56. preOrder() {
  57. if(this.root === null) return;
  58. let current = this.root;
  59. this.preOrderHelper(current);
  60.  
  61. }
  62. preOrderHelper(current) {
  63. console.log(current.val);
  64. if(current.left) this.preOrderHelper(current.left);
  65. if(current.right) this.preOrderHelper(current.right);
  66. }
  67.  
  68. postOrder() {
  69. if(this.root === null) return;
  70. let current = this.root;
  71. this.postOrderHelper(current);
  72.  
  73. }
  74. postOrderHelper(current) {
  75. if(current.left) this.postOrderHelper(current.left);
  76. console.log(current.val);
  77. if(current.right) this.postOrderHelper(current.right);
  78. }
  79.  
  80. inOrder() {
  81. if(this.root === null) return;
  82. let current = this.root;
  83. this.inOrderHelper(current);
  84.  
  85. }
  86. inOrderHelper(current) {
  87. if(current.left) this.inOrderHelper(current.left);
  88.  
  89. if(current.right) this.inOrderHelper(current.right);
  90. console.log(current.val);
  91. }
  92.  
  93. insertHelper(current, newNode) {
  94. if(newNode.val < current.val) {
  95. if(current.left === null) {
  96. current.left = newNode;
  97. return;
  98. }
  99. else {
  100. current = current.left;
  101. this.insertHelper(current, newNode);
  102. }
  103. }
  104. else if(newNode.val > current.val) {
  105. if(current.right === null) {
  106. current.right = newNode;
  107. return;
  108. }
  109. else {
  110. current = current.right;
  111. this.insertHelper(current, newNode);
  112. }
  113. }
  114. else {
  115. return;
  116. }
  117. } //END
  118.  
  119. }
  120. const myBST = new BST();
  121. myBST.insert(50);
  122. myBST.insert(40);
  123. myBST.insert(60);
  124. myBST.insert(45);
  125. myBST.insert(30);
  126. myBST.insert(10);
  127. myBST.insert(75);
  128.  
  129. console.log(myBST);
Add Comment
Please, Sign In to add comment