-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
385 lines (369 loc) · 14.2 KB
/
BinaryTree.java
File metadata and controls
385 lines (369 loc) · 14.2 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import javax.management.Query;
public class BinaryTree {
static class Node{
int data;
Node left;
Node right;
Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
public static class Binarytree{
static int indx = -1;
public static Node buildtree(int arr[]){
indx++;
if(arr[indx]==-1){
return null;
}
Node newNode = new Node(arr[indx]);
newNode.left=buildtree(arr);
newNode.right=buildtree(arr);
return newNode;
}
public static void PrintTree(Node root){
if(root == null){
System.out.println(-1);
return;
}
System.out.println(root.data);
PrintTree(root.left);
PrintTree(root.right);
}
public static void InorderTree(Node root){
if(root == null){
// System.out.println(-1);
return;
}InorderTree(root.left); //left NOde
System.out.print(root.data+" "); //root Node
InorderTree(root.right); // right Node
}
public static void postOrderTree(Node root){
if(root==null){
return;
}
postOrderTree(root.left); //left NOde
postOrderTree(root.right); // right Node
System.out.print(root.data); //root Node
}
public static void levelTree(Node root){
if(root == null){
return;
}
Queue<Node>q = new LinkedList<>();
q.add(root);
q.add(null);
while(!q.isEmpty()){
Node curr = q.remove();
if(curr== null){
System.out.println();
if(q.isEmpty()){
break;
}
else{
q.add(null);
}
}else{
System.out.print(curr.data+" ");
if(curr.left!= null){
q.add(curr.left);
}
if(curr.right!= null){
q.add(curr.right);
}
}
}
}
public static int HightOfTree(Node root){
if(root==null){
return 0;
}
int lh = HightOfTree(root.left);
int rh = HightOfTree(root.right);
return Math.max(lh,rh)+1;
}
public static int CountOfTree(Node root){
if(root==null){
return 0;
}
int cl = CountOfTree(root.left);
int cr = CountOfTree(root.right);
return cl+cr+1;
}
public static int SumOfTree(Node root){
if(root==null){
return 0;
}
int sl = SumOfTree(root.left);
int sr = SumOfTree(root.right);
return sl+sr+root.data ;
}
public static int DiameterOfTree(Node root){
if(root==null){
return 0;
}
int leftDiameter = DiameterOfTree(root.left);
int leftHight = HightOfTree(root.left);
int RightDiameter = DiameterOfTree(root.right);
int rightHight = HightOfTree(root.right);
int Self = leftHight+rightHight+1;
return Math.max(Self,Math.max(RightDiameter,leftDiameter));
}
}
public static class Info{
int Diameter;
int hight;
Info(int d , int h){
this.Diameter=d;
this.hight = h;
}
}
public static Info DiameterOfTree2(Node root){
if(root==null){
return new Info(0,0);
}
Info leftInfo = DiameterOfTree2(root.left);
Info rightInfo = DiameterOfTree2(root.right);
int self = leftInfo.hight+rightInfo.hight+1;
int maxDia = Math.max(self,Math.max(leftInfo.Diameter,rightInfo.Diameter));
int maxHig = Math.max(leftInfo.hight,rightInfo.hight)+1;
return new Info(maxDia,maxHig);
}
public static boolean isIdentical(Node root, Node subroot){
if(root==null && subroot==null){ // if both are null which means they are same
return true;
}
else if(subroot==null|| root==null|| root.data!= subroot.data){ // we will check whether subroot is empty or main root is empty or the data in subroot is not equal in main root
return false;
}
if(!isIdentical(root.left, subroot.left)){ // checking in left subroot and main root
return false;
}
if(!isIdentical(root.right, subroot.right)){ // checking in right subroot and main root
return false;
}
return true;
}
public static boolean isSame(Node root, Node subroot){
if(root==null){
return false;
}
if(root.data==subroot.data){ // findint the root node which is same in root as well as Subnode
if(isIdentical(root,subroot)){
return true;
}
}
return isSame(root.left, subroot)||isSame(root.right, subroot); // checking in left & right Node
}
public static class MapInfo{
//creating a class which store all the Horizontal distance and Node info
Node node;
int Hd;
public MapInfo(Node node , int Hd){
this.node = node;
this.Hd = Hd;
}
}
public static void topViewOfTree(Node root){
if(root==null){ //checking if node is not empty
return;
}
Queue<MapInfo>q = new LinkedList<>(); // using concept of level travacel to check level by level and adding its Horizontal value (-x,+x)
HashMap<Integer,Node> map = new HashMap<>(); // using some concepts of hashmaps to store value with key value pair to check and call specific value
int min = 0;int max = 0; // min and max will help us to iterate in horizontal distance as the left element lay on -x coodinates and right elements lays on +x coodinates
q.add(new MapInfo(root, 0)); // add root node value and its Horizontal distance
q.add(null);
while(!q.isEmpty()){ // check if queue is not empty
MapInfo curr = q.remove(); // removiing front value to check wheather it null or its Horizontal Distance is present in Hashmap
if(curr==null){
if(q.isEmpty()){
break;
}
else{
q.add(null);
}
}else{ // checking if it exists in Hashmap or not if not then add its Node value and Its Horizontal Distance
if(!map.containsKey(curr.Hd)){
map.put(curr.Hd,curr.node);
}
if(curr.node.left!=null){ // checking if it left Node exists in Hashmap or not if not then add its Node value and Its Horizontal Distance and change the min value from prev Min value and Horizontal Distance of left Node
q.add(new MapInfo(curr.node.left, curr.Hd-1));
min = Math.min(min,curr.Hd-1);
}
if(curr.node.right!=null){ // checking if it RightNode exists in Hashmap or not if not then add its Node value and Its Horizontal Distance and change the max value from prev Max value and Horizontal Distance of right Node
q.add(new MapInfo(curr.node.right, curr.Hd+1));
max = Math.max(max,curr.Hd+1);
}
}
}
for(int i = min ; i<=max;i++){ // printing all the Node value which is store in HashMap
System.out.print(map.get(i).data);
}
}
public static void KthNodes(Node root , int level , int k ){
// Using recursive method
if(root==null){
return;
}
if(level==k){
System.out.print(root.data+" ");
return;
}
KthNodes(root.left, level+1, k);
KthNodes(root.right, level+1, k);
}
public static void KthNode(Node root,int k){
// Using level travecal method
if(root == null){
return ;
}
int level = 1;
Queue<Node>q = new LinkedList<>();
q.add(root);
q.add(null);
while(!q.isEmpty()){
Node curr = q.remove();
if(curr==null){
level++;
if(q.isEmpty()){
break;
}
else{
q.add(null);
}
}else{
if(level == k){
System.out.print(curr.data+" ");
}
if(curr.left!=null){
q.add(curr.left);
}
if(curr.right!=null){
q.add(curr.right);
}
}
}
}
public static boolean getNode(Node root , int n , ArrayList<Node> AL){
if(root == null){ // if we reached to the end of the node and there is no value then we will return false
return false;
}
AL.add(root); // first we will add Root node then by the recursion all the left and right node will be added
if(root.data==n){ // if the data at root is equal or not
return true;
}
boolean NodeLeft = getNode(root.left, n, AL); // checking for left half if we get true then we have found the node we want and add all its parents in the arrayList or if we get false then it may be in right half or don't exist
boolean NodeRight = getNode(root.right, n, AL); //checking for right half if we get true then we have found the node we want and add all its parents in the arrayList or if we get false then it don't exist
if(NodeLeft||NodeRight){ // if any of the left or right half return true then we have found the node we want and add all its parents in the arrayList
return true;
} // if the Node we are isn't the Node we want then we need to delete that node and need to check in other half or return back
AL.remove(AL.size()-1);
return false;
}
public static Node LcN(Node root , int n1 , int n2){
ArrayList<Node> al1 = new ArrayList<>();
ArrayList<Node> al2 = new ArrayList<>();
getNode(root,n1,al1);
getNode(root,n2,al2);
int i = 0;
for(;i<al1.size()&&i<al2.size();i++){
if(al1.get(i)!=al2.get(i)){
break;
}
}
return al1.get(i-1);
}
public static Node lca2(Node root , int n1,int n2){
if(root==null||root.data==n1||root.data==n2){ // base case for where we check whether the node is null or if it's equal to N1 or N2 value;
return root; // if it get true then we need to return the Node
}
Node LeftHalf = lca2(root.left, n1, n2); // checking for the left half and storing the Node value
Node RightHalf = lca2(root.right, n1, n2); // checking for the Right half and storing the Node value
if(LeftHalf==null){ // if the left part is null then it mean that the value is present in the right half
return RightHalf;
}
if(RightHalf==null){ // if the right part is null then it mean that the value is present in the left half
return LeftHalf;
}
// if left half and right half of any parent node is not null that mean that its the least common Ancestor
return root;
}
//----------------------------------------------------------------
public static int distanceHelp(Node root , int n ){
if(root == null){
return -1;
} // if node is null then we need to return -1
if(root.data == n){
return 0;
} // if we fond the node then we need to return the 0 as the distance is overlaping so start counting from 0
int leftHalf = distanceHelp(root.left, n); // checking for left and right half of the tree
int rightHalf = distanceHelp(root.right, n);
if(leftHalf==-1&&rightHalf==-1){ // if both left and right are -1 then return -1
return -1;
}
else if(leftHalf==-1){ // if left half is -1 then it means that value exist in right hali and vice versa
return rightHalf+1;
}else{
return leftHalf+1;
}
}
public static int minDist(Node root , int n1 , int n2){
Node lca = lca2(root, n1, n2);
int dist1 = distanceHelp(lca,n1);
int dist2 = distanceHelp(lca,n2);
return dist1+dist2;
}
public static int KthAncester(Node root ,int n , int k ){
if(root == null){
return -1;
}
if(root.data == n){
return 0;
}
int leftHalf = KthAncester(root.left, n, k);
int rightHalf = KthAncester(root.right, n, k);
if(leftHalf==-1&&rightHalf==-1){
return -1;
}
int max = Math.max(leftHalf,rightHalf);
if(max+1 == k){
System.out.println(root.data+" ");
}
return max+1;
}
public static int transform(Node root){
if(root == null){
return 0;
}
int leftChild = transform(root.left);
int rightChild = transform(root.right);
int data = root.data;
int newleft = root.left== null?0:root.left.data;
int newright = root.right== null?0:root.right.data;
root.data = newleft+leftChild+newright+rightChild;
return data;
}
public static void preOrder(Node root){
if(root == null){
return ;
}
System.out.print(root.data+" ");
preOrder(root.left);
preOrder(root.right);
}
public static void main(String arg[]){
int arr[]={1,2,4,-1,-1,5,-1,-1,3,7,-1,-1,6,-1,-1,};
Binarytree tree = new Binarytree();
//Main node
Node root = tree.buildtree(arr);
transform(root);
preOrder(root);
}
}