forked from mr-sh202/HelpCreateSomeSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearizeaGT.java
More file actions
180 lines (145 loc) · 4.62 KB
/
linearizeaGT.java
File metadata and controls
180 lines (145 loc) · 4.62 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
import java.io.*;
import java.util.*;
public class linearizeaGT {
private static class Node {
int data;
ArrayList < Node > children = new ArrayList < > ();
}
public static void display(Node node) {
String str = node.data + " -> ";
for (Node child: node.children) {
str += child.data + ", ";
}
str += ".";
System.out.println(str);
for (Node child: node.children) {
display(child);
}
}
public static Node construct(int[] arr) {
Node root = null;
Stack < Node > st = new Stack < > ();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == -1) {
st.pop();
} else {
Node t = new Node();
t.data = arr[i];
if (st.size() > 0) {
st.peek().children.add(t);
} else {
root = t;
}
st.push(t);
}
}
return root;
}
public static int size(Node node) {
int s = 0;
for (Node child: node.children) {
s += size(child);
}
s += 1;
return s;
}
public static int max(Node node) {
int m = Integer.MIN_VALUE;
for (Node child: node.children) {
int cm = max(child);
m = Math.max(m, cm);
}
m = Math.max(m, node.data);
return m;
}
public static int height(Node node) {
int h = -1;
for (Node child: node.children) {
int ch = height(child);
h = Math.max(h, ch);
}
h += 1;
return h;
}
public static void traversals(Node node) {
System.out.println("Node Pre " + node.data);
for (Node child: node.children) {
System.out.println("Edge Pre " + node.data + "--" + child.data);
traversals(child);
System.out.println("Edge Post " + node.data + "--" + child.data);
}
System.out.println("Node Post " + node.data);
}
public static void levelOrderLinewiseZZ(Node node) {
Stack < Node > stack = new Stack < > ();
stack.add(node);
Stack < Node > cstack = new Stack < > ();
int level = 0;
while (stack.size() > 0) {
node = stack.pop();
System.out.print(node.data + " ");
if (level % 2 == 0) {
for (int i = 0; i < node.children.size(); i++) {
Node child = node.children.get(i);
cstack.push(child);
}
} else {
for (int i = node.children.size() - 1; i >= 0; i--) {
Node child = node.children.get(i);
cstack.push(child);
}
}
if (stack.size() == 0) {
stack = cstack;
cstack = new Stack < > ();
level++;
System.out.println();
}
}
}
public static void mirror(Node node) {
for (Node child: node.children) {
mirror(child);
}
Collections.reverse(node.children);
}
public static void removeLeaves(Node node) {
for (int i = node.children.size() - 1; i >= 0; i--) {
Node child = node.children.get(i);
if (child.children.size() == 0) {
node.children.remove(i);
}
}
for (Node child: node.children) {
removeLeaves(child);
}
}
public static Node getTail(Node temp){
while(temp.children.size() > 0){
temp = temp.children.get(0); // same as temp = temp.next
}
return temp;
}
public static void linearize(Node node) {
for(int i = 0; i < node.children.size(); i++){
linearize(node.children.get(i)); //faith
}
while(node.children.size() > 1){
int size = node.children.size();
Node sltail = getTail(node.children.get(size - 2)); //size-2 gives the second last index
sltail.children.add(node.children.remove(size - 1));
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
String[] values = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(values[i]);
}
Node root = construct(arr);
linearize(root);
display(root);
}
}