-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbstImplementation.js
62 lines (50 loc) · 1.14 KB
/
bstImplementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class binaryTree{
constructor(value){
this.value=value
this.left=null
this.right=null
}
}
class binary{
constructor(){
this.root=null
}
insert(value){
let newNode= new binaryTree(value)
if(!this.root){
this.root= newNode
}
else{
let current=this.root
while(true){
if(value<current.value){
if(!current.left){
current.left=newNode
return
}current=current.left
}else if(value>current.value){
if(!current.right){
current.right=newNode
return
}current=current.right
}
}
}
}
inorder(current){
if(current){
this.inorder(current.left)
console.log(current.value);
this.inorder(current.right)
}
}
}
let bt=new binary
bt.insert(20)
bt.insert(15)
bt.insert(30)
bt.insert(18)
bt.insert(25)
bt.insert(35)
bt.insert(28)
bt.inorder(bt.root)