-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.js
210 lines (195 loc) · 4.65 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var BinaryTree = function() {
var head=null,traverseInO= new Array(),count=0,index=1,width=0;
var BinaryTreeNode = function(data) {
var left=null,right=null,data=data;
return {
left : left,
right : right,
data : data
};
};
var constructLevelOrder = function(data) {
var that = this;
var temp = new BinaryTreeNode(data);
if(that.head==null) {
that.head = temp;
return;
}
var temp1 = that.traverseToIncomplete();
temp1.left==null ? temp1.left=temp : temp1.right = temp;
return;
};
var constructZigZag = function(data) {
var that = this;
var temp = new BinaryTreeNode(data);
if(that.head==null) {
that.head = temp;
that.count++;
return;
}
if(Math.floor(Math.log(that.count+1)/Math.log(2))%2==0) {
var temp1 = that.traverseToIncomplete();
temp1.left==null ? temp1.left=temp : temp1.right = temp;
}
else {
var temp1 = that.traverseToIncompleteRight();
temp1.right==null ? temp1.right = temp : temp1.left =temp;
}
that.count++;
return;
};
var traverseToIncomplete = function() {
var that = this;
if(that.head==null)
return null;
var que = new Queue();
que.enQueue(that.head);
while(!que.isEmpty()) {
var temp = que.deQueue();
if(temp.left==null||temp.right==null) {
return temp;
}
else {
que.enQueue(temp.left);
que.enQueue(temp.right);
}
}
};
var traverseToIncompleteRight = function() {
var that =this;
if(that.head==null)
return null;
var que = new Queue();
que.enQueue(that.head);
while(!que.isEmpty()) {
var temp = que.deQueue();
if(temp.left==null||temp.right==null) {
return temp;
}
else {
que.enQueue(temp.right);
que.enQueue(temp.left);
}
}
};
var traverseInOrder = function(root) {
var that = this;
if(root==null)
return;
that.traverseInOrder(root.left);
that.traverseInO.push(root.data);
that.traverseInOrder(root.right);
};
var join = function(a,b) {
a.right = b;
b.left = a;
};
var append = function(a,b) {
if(a==null) return b ;
if(b==null) return a;
var x = a.left;
var y = b.left;
join(x,b);
join(y,a);
return a;
};
var BinaryTreeToDLL = function(root) {
if(root==null)
return null;
var temp = BinaryTreeToDLL(root.left);
var temp1 = BinaryTreeToDLL(root.right);
root.left =root;
root.right =root;
temp = append(temp,root);
return append(temp,temp1);
};
var traverseDLLtoList = function(root) {
var that =this;
var temp =root;
while(temp.right!=root) {
that.traverseInO.push(temp.data);
temp =temp.right;
}
that.traverseInO.push(temp.data);
};
var isBST = function(root) {
if(root==null)
return true;
if((root.left!=null&&root.data<findMax(root.left))||(root.right!=null&&root.data>findMin(root.right)))
return false;
if(!isBST(root.left)||!isBST(root.right))
return false;
return true;
};
var findMax = function(root) {
var que = new Queue();
var temp =root;
que.enQueue(root);
while(!que.isEmpty()) {
var temp1 = que.deQueue();
if(temp1.data>temp.data) {
temp = temp1;
}
que.enQueue(root.left);
que.enQueue(root.right);
}
return temp;
};
var LCA = function(root,node1,node2) {
if(root==null) {
return null;
}
if(root.data==node1||root.data==node2)
return root;
var left = LCA(root.left,node1,node2);
var right = LCA(root.right,node1,node2);
if(left==null&&right==null)
return null;
if(left==null)
return right;
if(right==null)
return left;
return root;
};
var findMin = function(root) {
var que = new Queue();
var temp =root;
que.enQueue(root);
while(!que.isEmpty()) {
var temp1 = que.deQueue();
if(temp1.data<temp.data) {
temp = temp1;
}
que.enQueue(root.left);
que.enQueue(root.right);
}
return temp;
};
var widthCal =function(root) {
if(root==null)
return 0;
var left = widthCal(root.left);
var right = widthCal(root.right);
return left+right+1;
}
return {
BinaryTreeNode : BinaryTreeNode,
constructLevelOrder : constructLevelOrder,
traverseToIncomplete : traverseToIncomplete,
traverseToIncompleteRight : traverseToIncompleteRight,
traverseInOrder : traverseInOrder,
traverseInO : traverseInO,
constructZigZag : constructZigZag,
head : head,
count : count,
BinaryTreeToDLL : BinaryTreeToDLL,
traverseDLLtoList : traverseDLLtoList,
LCA : LCA
};
};
var a = [1,3,2,4,5,6,7,15,14,13,12,11,10,9,8,16,17,18];
var abc = new BinaryTree();
for(var i=0;i<a.length;i++) {
abc.constructZigZag(a[i]);
}
abc.LCA(abc.head,12,15);