-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.js
53 lines (53 loc) · 1.34 KB
/
binarytree.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
class binarytree{
data;
left;
right;
constructor(data){
this.data=data;
this.left=null;
this.right=null;
}
postorder(){
this.left.postorder();
this.right.postorder();
console.log(this.data);
}
inorder(){
this.left.inorder();
console.log(this.data);
this.right.inorder();
}
print(){
console.log(this.data);
temp=this.left;
while(temp!=null){
temp.print();
temp=temp.right;
}
}
depth(){
if(this.left==null && this.right==null){
return 0;
}
else if(this.left==null){
return this.right.depth()+1;
}
else if(this.right==null){
return this.left.depth()+1;
}
else{
return Math.max(this.left.depth(),this.right.depth())+1;
}
}
}
module.exports={structure:binarytree,
description:"Binary Tree",
complexity:"O(log n)",
methods:{
print:"Prints the tree",
depth:"Returns the depth of the tree",
inorder:"Prints the tree in inorder",
postorder:"Prints the tree in postorder",
preorder:"Prints the tree in preorder"
}
};