-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_trees.java
More file actions
147 lines (120 loc) · 3.93 KB
/
class_trees.java
File metadata and controls
147 lines (120 loc) · 3.93 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
//Write a program for Traversal over pre-order, in-order and post order, level-order. Find height of the tree and sum of all nodes in the tree.
import java.util.*;
class node {
int data;
node left, right;
node(int data) {
this.data = data;
left = right = null;
}
}
//Node part end here.
public class class_trees {
static node root;
class_trees() {
root = null;
}
static void preOrder(node root) {
if (root == null)
return;
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
static void inOrder(node root) {
if (root == null)
return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
static void postOrder(node root) {
if (root == null)
return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
static void levelOrder(node root) {
if (root == null)
return;
Queue<node> qt = new LinkedList<>();
qt.add(root);
while (!qt.isEmpty()) {
node current = qt.poll();
System.out.print(current.data + " ");
if (current.left != null)
qt.add(current.left);
if (current.right != null)
qt.add(current.right);
}
}
static void spiralOrder(node root) {
if (root == null) return;
Queue<node> queue = new LinkedList<>();
boolean leftToRight = true;
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> level = new ArrayList<>();
for (int i = 0; i < size; i++) {
node current = queue.poll();
level.add(current.data);
if (current.left != null) queue.add(current.left);
if (current.right != null) queue.add(current.right);
}
if (!leftToRight) {
Collections.reverse(level);
}
for (int val : level) {
System.out.print(val + " ");
}
leftToRight = !leftToRight;
}
}
static int height(node root) {
if (root == null)
return -1;
int leftHeight = height(root.left);
int rightHeight = height(root.right);
return 1 + Math.max(leftHeight, rightHeight);
}
static int sumOfNodes(node root) {
if (root == null)
return 0;
return root.data + sumOfNodes(root.left) + sumOfNodes(root.right);
}
public static void main(String[] args) {
root = new node(10);
root.left = new node(11);
root.right = new node(12);
root.left.left = new node(13);
root.left.right = new node(14);
root.right.left = new node(15);
root.right.right = new node(16);
root.left.left.left = new node(17);
root.left.left.right = new node(18);
root.left.right.left = new node(19);
root.left.right.right = new node(20);
root.right.left.left = new node(21);
root.right.left.right = new node(22);
root.right.right.left = new node(23);
root.right.right.right = new node(24);
System.out.println("Traversal using Pre-Order:");
preOrder(root);
System.out.println();
System.out.println("Traversal using In-Order:");
inOrder(root);
System.out.println();
System.out.println("Traversal using Post-Order:");
postOrder(root);
System.out.println();
System.out.println("Traversal using Level-Order:");
levelOrder(root);
System.out.println();
System.out.println("Height of the tree: ");
System.out.println(height(root));
System.out.println("Sum of all nodes in the tree: ");
System.out.println(sumOfNodes(root));
}
}