tree js script two

Solutions on MaxInterview for tree js script two by the best coders in the world

showing results for - "tree js script two"
Ned
27 Oct 2020
1
2BinarySearchTree.prototype.push = function(val){
3   var root = this.root;
4
5   if(!root){
6      this.root = new Node(val);
7      return;
8   }
9
10   var currentNode = root;
11   var newNode = new Node(val); 
12
13   while(currentNode){
14      if(val < currentNode.value){
15          if(!currentNode.left){
16             currentNode.left = newNode;
17             break;
18          }
19          else{
20             currentNode = currentNode.left;
21        }
22     }
23     else{
24         if(!currentNode.right){
25            currentNode.right = newNode;
26            break;
27         }
28         else{
29            currentNode = currentNode.right;
30         }
31     }
32  }
33
34}
35