Repository navigation
Expand file tree
/
Copy pathBinaryTreeDemo.java
More file actions
161 lines (145 loc) · 4.76 KB
/
Copy pathBinaryTreeDemo.java
File metadata and controls
161 lines (145 loc) · 4.76 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
import java.util.*;
// Node class
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
Node(int i) {
throw new UnsupportedOperationException("Not supported yet.");
}
Node(int i) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
class BinaryTreeViews {
Node root;
BinaryTreeViews() {
root = null;
}
// Preorder
void preorder(Node node) {
if (node == null) return;
System.out.print(node.data + " ");
preorder(node.left);
preorder(node.right);
}
// Inorder
void inorder(Node node) {
if (node == null) return;
inorder(node.left);
System.out.print(node.data + " ");
inorder(node.right);
}
// Postorder
void postorder(Node node) {
if (node == null) return;
postorder(node.left);
postorder(node.right);
System.out.print(node.data + " ");
}
// Level-order
void levelOrder() {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
Node temp = q.poll();
System.out.print(temp.data + " ");
if (temp.left != null) q.add(temp.left);
if (temp.right != null) q.add(temp.right);
}
}
// Left View
void leftView() {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int n = q.size();
for (int i = 1; i <= n; i++) {
Node temp = q.poll();
if (i == 1) System.out.print(temp.data + " ");
if (temp.left != null) q.add(temp.left);
if (temp.right != null) q.add(temp.right);
}
}
}
// Right View
void rightView() {
if (root == null) return;
Queue<Node> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int n = q.size();
for (int i = 1; i <= n; i++) {
Node temp = q.poll();
if (i == n) System.out.print(temp.data + " ");
if (temp.left != null) q.add(temp.left);
if (temp.right != null) q.add(temp.right);
}
}
}
// Top View
void topView() {
if (root == null) return;
class Pair {
Node node;
int hd;
Pair(Node n, int h) { node = n; hd = h; }
}
Queue<Pair> q = new LinkedList<>();
Map<Integer, Integer> map = new TreeMap<>();
q.add(new Pair(root, 0));
while (!q.isEmpty()) {
Pair p = q.poll();
if (!map.containsKey(p.hd)) map.put(p.hd, p.node.data);
if (p.node.left != null) q.add(new Pair(p.node.left, p.hd - 1));
if (p.node.right != null) q.add(new Pair(p.node.right, p.hd + 1));
}
for (int val : map.values()) System.out.print(val + " ");
}
// Bottom View
void bottomView() {
if (root == null) return;
class Pair {
Node node;
int hd;
Pair(Node n, int h) { node = n; hd = h; }
}
Queue<Pair> q = new LinkedList<>();
Map<Integer, Integer> map = new TreeMap<>();
q.add(new Pair(root, 0));
while (!q.isEmpty()) {
Pair p = q.poll();
map.put(p.hd, p.node.data);
if (p.node.left != null) q.add(new Pair(p.node.left, p.hd - 1));
if (p.node.right != null) q.add(new Pair(p.node.right, p.hd + 1));
}
for (int val : map.values()) System.out.print(val + " ");
}
}
// Demo
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTreeViews bt = new BinaryTreeViews();
bt.root = new Node(1);
bt.root.left = new Node(2);
bt.root.right = new Node(3);
bt.root.left.left = new Node(4);
bt.root.left.right = new Node(5);
bt.root.right.right = new Node(6);
bt.root.right.left = new Node(7);
System.out.println("Binary Tree Views:");
System.out.print("Preorder: "); bt.preorder(bt.root); System.out.println();
System.out.print("Inorder: "); bt.inorder(bt.root); System.out.println();
System.out.print("Postorder: "); bt.postorder(bt.root); System.out.println();
System.out.print("Level-order: "); bt.levelOrder(); System.out.println();
System.out.print("Left View: "); bt.leftView(); System.out.println();
System.out.print("Right View: "); bt.rightView(); System.out.println();
System.out.print("Top View: "); bt.topView(); System.out.println();
System.out.print("Bottom View: "); bt.bottomView(); System.out.println();
}
}