-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeHelper.java
More file actions
287 lines (233 loc) · 8.92 KB
/
Copy pathBinaryTreeHelper.java
File metadata and controls
287 lines (233 loc) · 8.92 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
import java.util.Scanner;
import java.util.Stack;
import java.util.LinkedList;
//import java.util.Queue;
public class BinaryTreeHelper {
public static void BT(){
Scanner s = new Scanner(System.in);
BinaryTreeNode<Integer> root = takeInput() ;
printAllKthNodes(root,6,1);
// printUsingQ(root);
// System.out.println("PreOrder: ");
// preOrder(root);
// System.out.println();
// System.out.println("PostOrder: ");
// postOrder(root);
// System.out.println();
// System.out.println("inOrder: ");
// inOrder(root);
//
// int[] preOrder = new int[]{4, 2, 1, 3, 6, 5, 7};
// int[] inOrder = new int[]{1, 2, 3, 4, 5, 6, 7};
// int[] postOrder = new int[]{1, 3, 2, 5, 7, 6, 4};
// BinaryTreeNode<Integer> node = buildTree(preOrder, inOrder);
// BinaryTreeNode<Integer> node = makeTree(inOrder, postOrder);
// System.out.println("make tree ko print krne wale h");
// printUsingQ(node);
}
public static BinaryTreeNode<Integer> getData(Scanner s){
int rootData;
System.out.println("Enter data ");
rootData = s.nextInt();
if (rootData == -1) return null;
BinaryTreeNode<Integer> node = new BinaryTreeNode<>(rootData);
node.left = getData(s);
node.right = getData(s);
return node;
}
public static void print(BinaryTreeNode<Integer> root){
if (root == null) return;
String toBePrinted = root.data + ": ";
if (root.left != null){
toBePrinted += "L: " + root.left.data + ",";
}
if (root.right != null){
toBePrinted += "R: " + root.right.data;
}
System.out.println(toBePrinted);
print(root.left);
print(root.right);
}
public static void printUsingQ(BinaryTreeNode<Integer> root){
if (root == null){
System.out.println("tree empty ho chuka h");
return;
};
Queue<BinaryTreeNode<Integer>> Q = new Queue<>();
Q.enqueue(root);
while (!Q.isEmpty()){
BinaryTreeNode<Integer> temp = Q.dequeue();
String s = temp.data + ": ";
if (temp.left != null) {
s += temp.left.data +", ";
Q.enqueue(temp.left);
}
else{
s += " ";
}
if (temp.right != null) {
s += temp.right.data +", ";
Q.enqueue(temp.right);
}
System.out.println(s);
}
}
public static BinaryTreeNode<Integer> takeInput(){
Scanner s = new Scanner(System.in);
System.out.println("Write data for the root node ");
int data = s.nextInt();
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(data);
Queue<BinaryTreeNode<Integer>> Q = new Queue<>();
Q.enqueue(root);
while (!Q.isEmpty()){
BinaryTreeNode<Integer> temp = Q.dequeue();
System.out.println("Left of "+temp.data);
data = s.nextInt();
if (data != -1){
temp.left = new BinaryTreeNode<>(data);
Q.enqueue(temp.left);
}
System.out.println("Right of "+temp.data);
data = s.nextInt();
if (data != -1){
temp.right = new BinaryTreeNode<>(data);
Q.enqueue(temp.right);
}
}
return root;
}
public static void preOrder(BinaryTreeNode<Integer> root){
if (root == null) return;
System.out.print(root.data+", ");
preOrder(root.left);
preOrder(root.right);
}
public static void inOrder(BinaryTreeNode<Integer> root){
if (root == null){
return;
}
inOrder(root.left);
System.out.print(root.data+", ");
inOrder(root.right);
}
public static void postOrder(BinaryTreeNode<Integer> root){
if (root == null) return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data+", ");
}
public static BinaryTreeNode<Integer> buildTree(int[] preOrder, int[] inOrder){
return buildTree(preOrder, inOrder,0, preOrder.length-1, 0, inOrder.length -1 );
}
public static BinaryTreeNode<Integer> buildTree(int[] preOrder, int[] inOrder, int preStart, int preEnd, int inStart, int inEnd){
if (inStart > inEnd) return null;
int data = preOrder[preStart];
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(data);
int index = -1;
for (int i = inStart; i<= inEnd ; i++){
if (data == inOrder[i]){
index = i;
break;
}
}
if (index == -1) return null;
int leftInStart = inStart;
int leftInEnd = index - 1;
int leftPreStart = preStart + 1;
int leftPreEnd = leftInEnd - leftInStart + leftPreStart;
int rightInStart = index + 1;
int rightInEnd = inEnd;
int rightPreStart = leftPreEnd + 1;
int rightPreEnd = preEnd ;
root.left = buildTree(preOrder, inOrder, leftPreStart, leftPreEnd, leftInStart, leftInEnd);
root.right = buildTree(preOrder, inOrder, rightPreStart, rightPreEnd, rightInStart, rightInEnd);
return root;
}
public static int sumOfDepth (BinaryTreeNode<Integer> root, int depth){
if (root == null) return 0;
int sum = sumOfDepth(root.left, depth + 1) + sumOfDepth(root.right, depth + 1);
if (root != null ) sum += depth;
return sum;
}
public static BinaryTreeNode<Integer> makeTree (int[] inOrder, int[] postOrder){
int n = postOrder.length;
for (int i = 0; i < n/2; i++){
int temp = postOrder[i];
postOrder[i] = postOrder[n-1-i];
postOrder[n-i-1] = temp;
}
//
// for (int i = 0; i < n; i++){
// System.out.println("reverse array : "+postOrder[i]+" ");
// }
return makeTree(inOrder, postOrder, 0, n-1, 0, n-1);
}
public static BinaryTreeNode<Integer> makeTree(int[] inOrder, int[] postOrder, int inStart, int inEnd, int postStart, int postEnd){
if (inStart > inEnd){
return null;
}
int data = postOrder[postStart];
BinaryTreeNode<Integer> root = new BinaryTreeNode<>(data);
System.out.println("root ka data: " + root.data);
int index = -1;
for (int i = inStart; i <= inEnd; i++){
if (inOrder[i] == root.data){
index = i;
break;
}
}
if (index == -1) return null;
int leftInStart = inStart;
int leftInEnd = index - 1;
int rightInStart = index + 1;
int rightInEnd = inEnd;
int rightPostStart = postStart + 1;
int rightPostEnd = rightPostStart + (rightInEnd - rightInStart);
int leftPostStart = rightPostEnd + 1;
int leftPostEnd = postEnd;
root.right = makeTree(inOrder, postOrder, rightInStart, rightInEnd, rightPostStart, rightPostEnd);
root.left = makeTree(inOrder, postOrder, leftInStart, leftInEnd, leftPostStart, leftPostEnd);
return root;
}
public static void printKthElement(BinaryTreeNode<Integer> root, int k){
if (root == null) return;
if (k == 0){
System.out.println(root.data);
return;
}
printKthElement(root.left, k-1);
printKthElement(root.right, k-1);
}
public static void printAllKthNodes(BinaryTreeNode<Integer> root, int element, int k){
if (root == null) return;
Queue<BinaryTreeNode<Integer>> Q = path(root, element);
BinaryTreeNode<Integer> tempOne = Q.dequeue();
printKthElement(tempOne,k);
while(!Q.isEmpty() && k > 0){
k--;
BinaryTreeNode<Integer> tempTwo = Q.dequeue();
if (k == 0){
printKthElement(tempTwo, 0);
}
else if (tempTwo.left == tempOne && k > 0){
printKthElement(tempTwo.right, k-1);
}
else{
printKthElement(tempTwo.right,k-1);
}
tempOne = tempTwo;
}
}
public static Queue<BinaryTreeNode<Integer>> path(BinaryTreeNode<Integer> root, int element){
if (root == null) return null;
if (root.data == element){
Queue<BinaryTreeNode<Integer>> Q = new Queue<>();
Q.enqueue(root);
return Q;
}
Queue<BinaryTreeNode<Integer>> ans = path(root.left, element);
if (ans == null) ans = path(root.right, element);
if (ans != null) ans.enqueue(root);
return ans;
}
}