-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree_levelwiseInput.java
249 lines (211 loc) · 5.18 KB
/
Tree_levelwiseInput.java
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
public class Node<T> {
T data;
Node<T> next;
Node(T data){
this.data = data;
next = null;
}
}
public static void replaceWithDepthValue(TreeNode<Integer> root){
if(root == null){
return ;
}
replaceWithDepthValue(root,0);
}
public static void replaceWithDepthValue(TreeNode<Integer> root, int k) {
root.data = k;
for (int i = 0; i < root.children.size(); i++) {
replaceWithDepthValue(root.children.get(i), k + 1);
}
}
public static int getHeight(TreeNode<Integer> root){
int height = 0;
if (root == null ) {
return height;
}
if (root.children == null) {
return 1;
}
for (TreeNode<Integer> child : root.children) {
height = Math.max(height, getHeight(child));
}
return height + 1;
}
public static int countLeafNodes(TreeNode<Integer> root){
int s = 0;
if(root == null){
return 0;
}
if(root.children.size() == 0){
return 1;
}
for (int i = 0; i < root.children.size(); i++) {
s = s + countLeafNodes(root.children.get(i));
}
return s;
}
public static void preorder(TreeNode<Integer> root) {
if (root == null) {
return;
}
System.out.print(root.data + " ");
for (int i = 0; i < root.children.size(); i++) {
preorder(root.children.get(i));
}
}
public static void printAtK(TreeNode<Integer> root, int k) {
if (k < 0) {
return;
}
if (k == 0) {
System.out.println(root.data);
return;
}
for (int i = 0; i < root.children.size(); i++) {
printAtK(root.children.get(i), k - 1);
}
}
class QueueUsingLL<T> {
private Node<T> front;
private Node<T> rear;
private int size;
public QueueUsingLL() {
front = null;
rear = null;
size = 0;
}
int size(){
return size;
}
boolean isEmpty(){
return size == 0;
}
T front() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
return front.data;
}
void enqueue(T element){
Node<T> newNode = new Node<>(element);
if(rear == null){
front = newNode;
rear = newNode;
}else{
rear.next = newNode;
rear = newNode;
}
size++;
}
T dequeue() throws QueueEmptyException{
if(size == 0){
throw new QueueEmptyException();
}
T temp = front.data;
front = front.next;
if(size == 1){
rear = null;
}
size--;
return temp;
}
}
class TreeNode<T> {
public T data;
public ArrayList<TreeNode<T>> children;
public TreeNode(T data) {
this.data = data;
children = new ArrayList<>();
}
}
class TreeUse {
public static TreeNode<Integer> takeInput(Scanner s) {
int n;
System.out.println("Enter next node data");
n = s.nextInt();
TreeNode<Integer> root = new TreeNode<Integer>(n);
System.out.println("Enter number of children for " + n);
int childCount = s.nextInt();
for (int i = 0; i < childCount; i++) {
TreeNode<Integer> child = takeInput(s);
root.children.add(child);
}
return root;
}
public static void print(TreeNode<Integer> root) {
String s = root.data + ":";
for (int i = 0; i < root.children.size(); i++) {
s = s + root.children.get(i).data + ",";
}
System.out.println(s);
for (int i = 0; i < root.children.size(); i++) {
print(root.children.get(i));
}
}
public static TreeNode<Integer> takeInputLevelWise() {
Scanner s= new Scanner(System.in);
System.out.println("Enter root data");
int rootData = s.nextInt();
QueueUsingLL<TreeNode<Integer>> pendingNodes = new QueueUsingLL<>();
TreeNode<Integer> root = new TreeNode<Integer>(rootData);
pendingNodes.enqueue(root);
while (!pendingNodes.isEmpty()) {
try {
TreeNode<Integer> frontNode = pendingNodes.dequeue();
System.out.println("Enter num of children of " + frontNode.data);
int numChildren = s.nextInt();
for (int i = 0; i < numChildren; i++) {
System.out.println("Enter " + (i + 1) + "th child of " + frontNode.data);
int child = s.nextInt();
TreeNode<Integer> childNode = new TreeNode<Integer>(child);
frontNode.children.add(childNode);
pendingNodes.enqueue(childNode);
}
} catch (QueueEmptyException e) {
// Shouldn't come here
return null;
}
}
return root;
}
public static int numNodes(TreeNode<Integer> root) {
if (root == null) {
return 0;
}
int count = 1;
for (int i = 0; i < root.children.size(); i++) {
count += numNodes(root.children.get(i));
}
return count;
}
public static int sumOfAllNode(TreeNode<Integer> root){
if (root == null) {
return 0;
}
int count = root.data;
for (int i = 0; i < root.children.size(); i++) {
count += sumOfAllNode(root.children.get(i));
}
return count;
}
public static int largest(TreeNode<Integer> root) {
if (root == null) {
return Integer.MIN_VALUE;
}
int ans = root.data;
for (int i = 0; i < root.children.size(); i++) {
int childLargest = largest(root.children.get(i));
if (childLargest > ans) {
ans = childLargest;
}
}
return ans;
}
public static void main(String[] args) {
// Scanner s= new Scanner(System.in);
//
// TreeNode<Integer> root = takeInput(s);
TreeNode<Integer> root = takeInputLevelWise();
print(root);
}
}