-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree.js
More file actions
169 lines (138 loc) · 2.89 KB
/
Copy pathbinary-tree.js
File metadata and controls
169 lines (138 loc) · 2.89 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function Node(val){
this.value = val;
this.left = null;
this.right = null;
}
function BinarySearchTree(){
this.root = null;
}
BinarySearchTree.prototype.push = function(val){
var root = this.root;
if(!root){
this.root = new Node(val);
return;
}
var currentNode = root;
var newNode = new Node(val);
while(currentNode){
if(val < currentNode.value){
if(!currentNode.left){
currentNode.left = newNode;
break;
}
else{
currentNode = currentNode.left;
}
}
else{
if(!currentNode.right){
currentNode.right = newNode;
break;
}
else{
currentNode = currentNode.right;
}
}
}
}
var bst = new BinarySearchTree();
bst.push(3);
bst.push(2);
bst.push(4);
bst.push(1);
bst.push(5);
//Depth first search
function dfs(node){
if(node){
console.log(node.value);
dfs(node.left);
dfs(node.right);
}
}
//In order Traversal
function inorder(node){
if(node){
inorder(node.left);
console.log(node.value);
inorder(node.right);
}
}
//min and max value
function minNode(node){
if(!node){
return 0;
}
if(node.left){
return minNode(node.left)
}
return node.value
}
unction maxNode(node){
if(!node){
return 0;
}
if(node.right){
return maxNode(node.right)
}
return node.value;
}
//Balanced and Unbalanced Tree
function isBST(node){
if(!node){
return true;
}
if(node.left != null && node.left.value > node.value){
return false;
}
if(node.right !=null && node.right.value < node.value) {
return false;
}
if(!isBST(node.left) || !isBST(node.right)) {
return false;
}
return true;
}
//Height of a tree
function height(node){
if(!node) return 0;
var leftHeight = height(node.left);
var rightHeight = height(node.right);
return Math.max(leftHeight, rightHeight) + 1;
}
//print ancestor
function printAncestor(node, target){
if(!node) return false
if(node.value == target) return true;
if(printAncestor(node.left, target) || printAncestor(node.rigth, target)){
console.log(node.value);
return true;
}
return false
}
//common ancestor
function commonAncestor(node, n1, n2){
if(!node) return;
var val = node.value;
if(n1 < val && n2<val){
return commonAncestor(node.left, n1, n2);
}
if(n1<val && n2<val){
return commonAncestor(node.right, n1, n2);
}
console.log('lowest common ancestor value: ', val);
return node;
}
//common ancestor for binary tree
function commonAncestorBT(node, n1, n2){
if(!node) return;
var val = node.value;
if(n1 == val || n2 ==val){
return node;
}
var left = commonAncestorBT(node.left, n1, n2);
var right = commonAncestorBT(node.right, n1, n2);
if(left && right){
return node;
}
return (left) ? left : right;
}