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