Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. //BST :D
  2. //METHOD: INSERT
  3. function BST(value){
  4. this.value = value;
  5. this.left =null;
  6. this.right = null;
  7. }
  8.  
  9. BST.prototype.insert = function (value) {
  10. if(value <= this.value) {
  11. if(!this.left) this.left = new BST(value);
  12. else this.left.insert(value);
  13. }
  14. else if (value > this.value) {
  15. if(!this.right) this.right = new BST(value);
  16. else this.right.insert(value);
  17. }
  18. };
  19.  
  20. //METHOD: CONTAINS
  21.  
  22. BST.prototype.contains = function(value){
  23. if(value === this.value) {
  24. return true;
  25. }
  26. else if(value < this.value){
  27. if(!this.left) return false;
  28. else return this.left.contains(value);
  29. }
  30. else if(value > this.value){
  31. if(!this.right) return false;
  32. else return this.right.contains(value);
  33. }
  34. };
  35.  
  36. //DEPTH FIRST TRAVERSAL: travels through all of the nodes from top to bottom. post, pre and order.
  37. BST.prototype.depthFirstTraversal = function(iteratorFunc, order) {
  38. if(order === "pre-order") iteratorFunc(this.value);
  39. if(this.left) this.left.depthFirstTraversal(iteratorFunc, order);
  40. if(order === "in-order") iteratorFunc(this.value);
  41. if(this.right) this.right.depthFirstTraversal(iteratorFunc, order);
  42. if(order === "post-order") iteratorFunc(this.value);
  43. };
  44.  
  45. //BREADTH FIRST TRAVERSAL: it travels level by level
  46. BST.prototype.breadthFirstTraversal = function(iteratorFunc) {
  47. var queue =[this]; //queue: first in first out
  48. while (queue.length) {
  49. var treeNode = queue.shift();
  50. iteratorFunc(treeNode);
  51. if(treeNode.left) queue.push(treeNode.left);
  52. if(treeNode.right) queue.push(treeNode.right);
  53.  
  54. }
  55. };
  56.  
  57. //MIN-MAX
  58. BST.prototype.getMinVal = function() {
  59. if(this.left) return this.left.getMinVal();
  60. else return this.value;
  61. };
  62. BST.prototype.getMaxVal = function() {
  63. if(this.right) return this.right.getMaxVal();
  64. else return this.value;
  65. };
  66.  
  67. var bst = new BST(50);
  68. bst.insert(30);
  69. bst.insert(70);
  70. bst.insert(100);
  71. bst.insert(60);
  72. bst.insert(59);
  73. bst.insert(20);
  74. bst.insert(45);
  75. bst.insert(35);
  76. bst.insert(85);
  77. bst.insert(105);
  78. bst.insert(10);
  79.  
  80. //console.log(bst.contains(50));
  81.  
  82. //bst.depthFirstTraversal(log, "post-order");
  83.  
  84. function log(/*value*/node) {
  85. console.log(/*value*/node.value);
  86. }
  87.  
  88. bst.breadthFirstTraversal(log);
  89.  
  90. console.log("MAX:" + bst.getMaxVal());
  91. console.log("MIN:" + bst.getMinVal());
  92.  
  93. //NOTES:
  94.  
  95. //BINARY SEARCH TREES: Collection of nodes all connected together in a certain way. Each parent has two children nodesL: right node and left node. Each node contains value or data. Left nodes <= parent; Right nodes >= parent. Child + parent = binary tree.
  96.  
  97. //Traveling through the tree-touching nodes; two patterns: Depth Firts Traversal, Breadth First Traversal (horizontal).
  98.  
  99. //RECURSION: when a function calls itself.
  100.  
  101. /*function f() {
  102. if(base case) {
  103. return whatever;
  104. }
  105. else { //recursive case
  106. f()
  107. }
  108. } */
  109.  
  110. //factorials (!)
  111.  
  112. function factorial(num) {
  113. if(num === 1) {
  114. return num;
  115. }
  116. else {
  117. return num * factorial(num -1);
  118. }
  119. }
  120.  
  121. //Call Stack: what happens at each step? the recursive keeps until it satisfies the base case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement