-
Notifications
You must be signed in to change notification settings - Fork 77
/
generictree.java
145 lines (137 loc) · 3.94 KB
/
generictree.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
import java.util.*;
public class generictree{
public static class node{
int data=0;
ArrayList<node> children =new ArrayList<>();
node(int data){
this.data=data;
}
}
static node constructor(int[]arr){
LinkedList<node> st=new LinkedList<>();
node root=null;
for(int i=0;i<arr.length;i++){
if(arr[i]==-1){
st.removeFirst();
}else{
node nn = new node(arr[i]);
if(st.size()==0){
root =nn;
}else{
node parent = st.getFirst();
parent.children.add(nn);
}
st.addFirst(nn);
}
}
return root;
}
static void display(node root){
System.out.print(root.data+ "->");
for(node child:root.children){
System.out.print(child.data+" ");
}
System.out.println();
for(node child:root.children){
display(child);
}
}
static boolean find(node root,int data){
if(root.data==data){
return true;
}
for(node child:root.children){
boolean res=find(child,data);
if(res==true){
return true;
}
}
return false;
}
static int min (node root){
int mino=root.data;
for(node child: root.children){
int recmin=min(child);
mino=Math.min(mino,recmin);
}
return mino;
}
public static ArrayList<node> nodetorootpath(node root,int data){
if(root.data==data){
ArrayList<node> base=new ArrayList<>();
base.add(root);
return base;
}
for(node child:root.children){
ArrayList<node> res=nodetorootpath(child, data);
if(res.size()>0){
res.add(root);
return res;
}
}
return new ArrayList<>();
}
public static int lca(int a,int b,node root){
if(find(root,a)&&find(root,b)){
ArrayList<node> res1=nodetorootpath(root,a);
ArrayList<node> res2=nodetorootpath(root,b);
for(int i=res1.size()-1,j=res2.size()-1;i>=0&&j>=0;i--,j--){
if(res1.get(i).data!=res2.get(j).data){
return res1.get(i+1).data;
}
else if(i==0){
return res1.get(i).data;
}else if(j==0){
return res2.get(j).data;
}
}
}
return -1;
}
public static void zigzag(node root){
LinkedList<node> st1 = new LinkedList<>();
LinkedList<node> st2 = new LinkedList<>();
st1.addFirst(root);
boolean flag=true;
while(st1.size()>0){
node rnode=st1.removeFirst();
System.out.print(rnode.data+" ");
if(flag==true){
for(int i=rnode.children.size()-1;i>=0;i--){
st2.addFirst(rnode.children.get(i));
}
}else{
for(node child:rnode.children){
st2.addFirst(child);
}
}
if(st1.size()==0){
System.out.println();
LinkedList<node> temp=st1;
st1=st2;
st2=temp;
flag=!flag;
}
}
}
/////17th august class
public static class pair{
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
boolean find =false;
}
static void multisolver(node root){
if(pair.find==false){
}
}
public static void main(String []args){
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=constructor(arr);
display(root);
boolean res=find(root,40);
System.out.println(res);
System.out.println(min(root));
System.out.println(lca(100,120,root));
zigzag(root);
}
}