Repository navigation
Expand file tree
/
Copy pathBinaryTree.java
More file actions
402 lines (340 loc) · 14.5 KB
/
Copy pathBinaryTree.java
File metadata and controls
402 lines (340 loc) · 14.5 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import java.util.*;
import java.util.LinkedList;
import java.util.Queue;
/**
* BinaryTree class: Implements generic Binary Tree operations, including
* standard traversals and utility methods for computing different tree views.
*/
public class BinaryTree {
// --- 1. Standard Tree Node Definition ---
public static class Node {
int data;
Node left;
Node right;
public Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
// Root of the tree
public Node root;
public BinaryTree() {
this.root = null;
}
// --- 2. Traversal Methods ---
public void inorder(Node node) {
if (node != null) {
inorder(node.left);
System.out.print(node.data + " ");
inorder(node.right);
}
}
public void preorder(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preorder(node.left);
preorder(node.right);
}
}
public void postorder(Node node) {
if (node != null) {
postorder(node.left);
postorder(node.right);
/* Represents a single node in a binary tree.
*/
class Node {
int data;
Node left;
Node right;
/**
* Constructor to create a new node.
*
* @param data The data to be stored in the node.
*/
public Node(int data) {
this.data = data;
left = null;
right = null;
}
}
/**
* Implements a Binary Tree and includes methods for traversal and different views.
*/
public class BinaryTree {
Node root;
public void BinaryTree(Node root) {
root = null;
}
/**
* Performs an in-order traversal of the binary tree.
*
* @param node The current node in the traversal.
*/
public void inOrder(Node node) {
if (node != null) {
inOrder(node.left);
System.out.print(node.data + " ");
inOrder(node.right);
}
}
/**
* Performs a pre-order traversal of the binary tree.
*
* @param node The current node in the traversal.
*/
public void preOrder(Node node) {
if (node != null) {
System.out.print(node.data + " ");
preOrder(node.left);
preOrder(node.right);
}
}
/**
* Performs a post-order traversal of the binary tree.
*
* @param node The current node in the traversal.
*/
public void postOrder(Node node) {
if (node != null) {
postOrder(node.left);
postOrder(node.right);
System.out.print(node.data + " ");
}
}
// --- 3. View Methods (Utility) ---
// Top View: Nodes visible when looking from the top (first node encountered at each Horizontal Distance)
public List<Integer> topView() {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
// Map stores <Horizontal Distance, Node Data>
Map<Integer, Integer> map = new TreeMap<>();
// Queue stores <Node, Horizontal Distance>
Queue<Pair<Node, Integer>> queue = new LinkedList<>();
queue.add(new Pair<>(root, 0));
while (!queue.isEmpty()) {
Pair<Node, Integer> current = queue.poll();
Node node = current.key;
int hd = current.value;
// Only update if this HD is encountered for the first time (this is the 'top' node)
if (!map.containsKey(hd)) {
map.put(hd, node.data);
}
if (node.left != null) {
queue.add(new Pair<>(node.left, hd - 1));
}
if (node.right != null) {
queue.add(new Pair<>(node.right, hd + 1));
}
}
// Add all values from the sorted map (TreeMap ensures HD is sorted) to the result list
result.addAll(map.values());
return result;
}
// Bottom View: Nodes visible when looking from the bottom (last node encountered at each Horizontal Distance)
public List<Integer> bottomView() {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
// Map stores <Horizontal Distance, Node Data>
Map<Integer, Integer> map = new TreeMap<>();
// Queue stores <Node, Horizontal Distance>
Queue<Pair<Node, Integer>> queue = new LinkedList<>();
queue.add(new Pair<>(root, 0));
while (!queue.isEmpty()) {
Pair<Node, Integer> current = queue.poll();
Node node = current.key;
int hd = current.value;
// Always update the map. The last node processed for a given HD in BFS
// will be the lowest/closest to the bottom at that HD.
map.put(hd, node.data);
if (node.left != null) {
queue.add(new Pair<>(node.left, hd - 1));
}
if (node.right != null) {
queue.add(new Pair<>(node.right, hd + 1));
}
}
result.addAll(map.values());
return result;
}
// Left View: The first node of every level
public List<Integer> leftView() {
List<Integer> result = new ArrayList<>();
leftView(root, 1, new int[]{0}, result);
return result;
}
// Recursive helper for Left View
private void leftView(Node node, int level, int[] maxLevel, List<Integer> result) {
if (node == null) return;
// If this is the first node at the current level, add it to the view
if (maxLevel[0] < level) {
result.add(node.data);
maxLevel[0] = level;
}
// Recurse left first to ensure the leftmost node is processed first
leftView(node.left, level + 1, maxLevel, result);
leftView(node.right, level + 1, maxLevel, result);
}
// Right View: The last node of every level
public List<Integer> rightView() {
List<Integer> result = new ArrayList<>();
rightView(root, 1, new int[]{0}, result);
return result;
}
// Recursive helper for Right View
private void rightView(Node node, int level, int[] maxLevel, List<Integer> result) {
if (node == null) return;
// If this is the first node at the current level, add it to the view
if (maxLevel[0] < level) {
result.add(node.data);
maxLevel[0] = level;
}
// Recurse right first to ensure the rightmost node is processed first
rightView(node.right, level + 1, maxLevel, result);
rightView(node.left, level + 1, maxLevel, result);
}
// Helper class for combining Node and Horizontal Distance in the Queue (for Top/Bottom View)
private static class Pair<K, V> {
K key;
V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
/**
* Computes and prints the top view of the binary tree.
* The top view consists of the nodes that are visible from the top.
*/
public void topView() {
if (root == null) {
return;
}
}
class QueueObj {
Node node;
int hd; // Horizontal Distance
QueueObj(Node node, int hd) {
this.node = node;
this.hd = hd;
}
}
Queue<QueueObj> q = new LinkedList<>();
Map<Integer, Node> topViewMap = new TreeMap<>();
q.add(new
QueueObj(root, 0));
while(!q.isEmpty())
{
QueueObj tmpNode = q.poll();
if (!topViewMap.containsKey(tmpNode.hd)) {
topViewMap.put(tmpNode.hd, tmpNode.node);
}
if (tmpNode.node.left != null) {
q.add(new QueueObj(tmpNode.node.left, tmpNode.hd - 1));
}
if (tmpNode.node.right != null) {
q.add(new QueueObj(tmpNode.node.right, tmpNode.hd + 1));
}
}
System.out.println("Top View:");
for(
Map.Entry<Integer, Node> entry :topViewMap.entrySet())
{
System.out.print(entry.getValue().data + " ");
}
System.out.println();
}
/**
* Computes and prints the bottom view of the binary tree.
* The bottom view consists of the nodes that are visible from the bottom.
*/
public void bottomView() {
if (root == null) {
return;
}
class QueueObj {
Node node;
int hd; // Horizontal Distance
QueueObj(Node node, int hd) {
this.node = node;
this.hd = hd;
}
}
Queue<QueueObj> q = new LinkedList<>();
Map<Integer, Node> bottomViewMap = new TreeMap<>();
q.add(new QueueObj(root, 0));
while (!q.isEmpty()) {
QueueObj tmpNode = q.poll();
bottomViewMap.put(tmpNode.hd, tmpNode.node);
if (tmpNode.node.left != null) {
q.add(new QueueObj(tmpNode.node.left, tmpNode.hd - 1));
}
if (tmpNode.node.right != null) {
q.add(new QueueObj(tmpNode.node.right, tmpNode.hd + 1));
}
}
System.out.println("Bottom View:");
for (Map.Entry<Integer, Node> entry : bottomViewMap.entrySet()) {
System.out.print(entry.getValue().data + " ");
}
System.out.println();
}
/**
* Computes and prints the left view of the binary tree.
* The left view consists of the first node at each level.
*/
public void leftView() {
if (root == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
System.out.println("Left View:");
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 1; i <= n; i++) {
Node temp = queue.poll();
if (i == 1) {
System.out.print(temp.data + " ");
}
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
}
System.out.println();
}
/**
* Computes and prints the right view of the binary tree.
* The right view consists of the last node at each level.
*/
public void rightView() {
if (root == null) {
return;
}
Queue<Node> queue = new LinkedList<>();
queue.add(root);
System.out.println("Right View:");
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 1; i <= n; i++) {
Node temp = queue.poll();
if (i == n) {
System.out.print(temp.data + " ");
}
if (temp.left != null) {
queue.add(temp.left);
}
if (temp.right != null) {
queue.add(temp.right);
}
}
}
System.out.println();
}
}
}
}
}