daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node {
  2.     constructor(data) {
  3.         this.data = data;
  4.         this.left = null;
  5.         this.right = null;
  6.         this.parent = null;
  7.     }
  8. }
  9.  
  10. class BST {
  11.     constructor(data) {
  12.         this._root = new Node(data);
  13.     }
  14.  
  15. recurseAdd(data, currNode=this._root) {
  16.     let newNode = new Node(data);
  17.  
  18.     if (data <= currNode.data) {
  19.         // leftNode - empty
  20.         if(!currNode.left) {
  21.             currNode.left = newNode;
  22.             newNode.parent = currNode;
  23.             // leftNode - exists
  24.         } else {
  25.             currNode = currNode.left;
  26.             this.recurseAdd(data, currNode);
  27.         }
  28.     } else {
  29.         // leftNode - empty
  30.         if(!currNode.right) {
  31.             newNode.parent = currNode;
  32.             currNode.right = newNode;
  33.             // leftNode - exists
  34.         } else {
  35.             currNode = currNode.right;
  36.             this.recurseAdd(data, currNode);
  37.         }
  38.     }
  39. }
  40.  
  41. add(data, currNode=this._root) {
  42.     let newNode = new Node(data);
  43.  
  44.     while(currNode) {
  45.         if (data <=currNode.data) {
  46.         // leftNode - empty
  47.         if (!currNode.left) {
  48.             newNode.parent = currNode;
  49.             currNode.left = newNode;
  50.             break;
  51.             // leftNode - exists
  52.         } else {
  53.             currNode = currNode.left;
  54.         }
  55.     } else {
  56.         // leftNode - empty
  57.         if (!currNode.right) {
  58.           newNode.parent = currNode;
  59.             currNode.right = newNode;
  60.             break;
  61.             // leftNode - exists
  62.         } else {
  63.             currNode = currNode.right;
  64.         }
  65.     }   // end of outer-most else
  66.     } // end of while
  67.  
  68. }   // end of iterative ADD
  69.  
  70. /* PRINT OUT VALUES - BFS Traversal */
  71. BFS(currNode=this._root, queue=[], results=[]) {
  72.     if (currNode === null) {return;}
  73.  
  74.     // push rootNode to stack
  75.     queue.push(currNode);
  76.     let len = queue.length;
  77.  
  78.     while (queue.length !== 0) {
  79.         // check if left child exists
  80.         if (currNode.left) {
  81.             queue.push(currNode.left);
  82.         }
  83.         // check if right child exists
  84.         if (currNode.right) {
  85.             queue.push(currNode.right);
  86.         }
  87.  
  88.     // remove 'first element' in [queue]
  89.     let node = queue.shift();
  90.     let nodeVal = node.data;
  91.     //console.log(`nodeVal: ${nodeVal}`);
  92.         results.push(nodeVal);
  93.         currNode = queue[0];
  94.     } // end of while-loop
  95.  
  96.     return results;
  97. } // end of BFS
  98. }
  99.  
  100. let hi = new BST('f');
  101. hi.add('d');
  102. hi.add('k');
  103.  
  104. hi.add('b');
  105. hi.add('e');
  106. hi.add('g');
  107. hi.add('l');
  108.  
  109. hi.add('a');
  110. hi.add('c');
  111. hi.add('h');
  112.  
  113. hi.add('j');
  114. /* console.log(hi._root); */
  115.  
  116. // BFS
  117. let result = hi.BFS();
  118. console.log(`Printed Values in BFS: [${result}]`);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top