-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathGt.java
186 lines (151 loc) · 5.08 KB
/
Gt.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
import java.util.ArrayList;
import java.util.Stack;
public class Gt {
public static class Node{
int data;
ArrayList<Node> children;
public Node(int data){
this.data=data;
this.children=new ArrayList<>();
}
public String toString(Node node){
return node.data+"";
}
}
public static Node constructTree(int[] arr){
Stack<Node> st=new Stack<>();
for(int i=0; i<arr.length-1; i++){
if(arr[i]!=-1){
st.push(new Node(arr[i]));
} else {
Node node=st.pop();
st.peek().children.add(node);
}
}
return st.peek();
}
public static void display(Node root){
StringBuilder sb=new StringBuilder();
sb.append(root.data);
sb.append("->");
for(Node child:root.children) sb.append(child.data+" ");
System.out.println(sb);
for(Node child:root.children) display(child);
}
public static int height(Node root){ // in terms of edges
int h=-1;
for(Node child:root.children) h=Math.max(h, height(child)+1);
return h;
}
public static int size(Node root){
int s=0;
for(Node child:root.children)
s+=size(child);
return s+1;
}
public static ArrayList<Node> NodeToRoot(Node root, int a){
if(root.data==a){
ArrayList<Node> ba=new ArrayList<>();
ba.add(root);
return ba;
}
ArrayList<Node> ans=new ArrayList<>();
for(Node child:root.children){
ans=NodeToRoot(child, a);
if(ans.size()>0){
ans.add(child);
return ans;
}
}
return ans;
}
public static boolean NodeToRoot_bool(Node root, int a, ArrayList<Node> al){
for(Node child:root.children){
if(child.data==a){
al.add(child);
return true;
} else {
if(NodeToRoot_bool(child, a, al)){
al.add(child);
return true;
}
}
}
return false;
}
public static void lca(Node root, int a, int b){
ArrayList<Node> ntra=NodeToRoot(root,a);
ArrayList<Node> ntrb=new ArrayList<>();
NodeToRoot_bool(root,b,ntrb);
ntrb.add(root);
int i=ntra.size()-1;
int j=ntrb.size()-1;
while(i>=0 && j>=0 && ntra.get(i)==ntrb.get(j)){
i--;
j--;
}
i++;
System.out.println(ntra.get(i).data);
}
// if we can fold it form between
public static boolean isFoldable(Node node1, Node node2){
if(node1.children.size()!=node2.children.size()) return false;
for(int i=0, j=node1.children.size()-1; i<=j; i++,j--){
Node a=node1.children.get(i);
Node b=node2.children.get(j);
if(!isFoldable(a,b)) return false;
}
return true;
}
// mirror ================================================================
public static boolean isMirror(Node node1, Node node2){
if(node1.children.size()!=node2.children.size() || node1.data!=node2.data) return false;
for(int i=0, j=node1.children.size()-1; i<=j; i++,j--){
Node a=node1.children.get(i);
Node b=node2.children.get(j);
if(!isMirror(a,b)) return false;
}
return true;
}
// linearize Tree ========================================================================
public static void linearize(Node root){
for(Node child:root.children){
linearize(child);
}
for(int i=root.children.size()-2; i>=0;i--){
Node node=root.children.get(i);
Node tail=node;
while(tail.children.size()>0)
tail=tail.children.get(0);
tail.children.add(root.children.get(i+1));
root.children.remove(i+1);
}
}
// linearize TREE better ==============================================================
public static Node linearize_better(Node root){
if(root.children.size()==0){
return root;
}
int n=root.children.size();
Node gTail=linearize_better(root.children.get(n-1));
for(int i=n-2; i>=0; i--){
Node tail=linearize_better(root.children.get(i));
tail.children.add(root.children.get(i+1));
root.children.remove(i+1);
}
return gTail;
}
public static void solve(){
int[] arr={10,20,50,-1,60,-1,-1,30,70,-1,80,110,-1,120,-1,-1,90,-1,-1,40,100,-1,-1,-1}; ;
Node root=constructTree(arr);
display(root);
System.out.println(height(root));
System.out.println(size(root));
//lca(root,70,120);
linearize_better(root);
display(root);
}
public static void main(String[] args) {
solve();
}
}