-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFlattenTernaryTree.java
221 lines (173 loc) · 4.88 KB
/
FlattenTernaryTree.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
/*
given a tree as a parameter, will return an inorder traversal of that tree.Your implementation should throw an
IllegalArgumentException if the tree is null.Your implementation must implement the FlattenTree interface For example a tree like:
/|\
1 | 6
/|\
5 4 9
would result in the list [1,5,4,9,6].
*/
import java.util.*;
import java.lang.*;
import java.util.List;
/**
* A type that stores three values of the same type.
*/
class Triple<V> {
private final V l, m, r;
public Triple(V l, V m, V r) {
this.l = l;
this.m = m;
this.r = r;
}
public V left() {
return l;
}
public V middle() {
return m;
}
public V right() {
return r;
}
}
/**
* A type which stores one of either of two types of value, but not both.
*
*/
class Either<A,B> {
/**
* Constructs a left-type Either
*/
public static <A> Either left(A a) {
if (a == null) throw new IllegalArgumentException();
return new Either(a, null);
}
/**
* Constructs a right-type Either
*/
public static <B> Either right(B b) {
if (b == null) throw new IllegalArgumentException();
return new Either(null, b);
}
private final A a;
private final B b;
private Either(A a, B b) {
this.a = a;
this.b = b;
}
/**
* Applies function f to the contained value if it is a left-type and returns the result. Throws an IllegalStateException if this is a right-type Either.
*/
public<T> T ifLeft(Function<A,T> f) {
if (!this.isLeft()) {
throw new IllegalStateException();
}
return f.apply(a);
}
/**
* Applies function f to the contained value if it is a right-type and returns the result. Throws an IllegalStateException if this is a left-type Either.
*/
public<T> T ifRight(Function<B,T> f) {
if (this.isLeft()) {
throw new IllegalStateException();
}
return f.apply(b);
}
/**
* @return true if this is a left, false if it is a right
*/
public boolean isLeft() {
return b == null;
}
}
interface FlattenTree<T> {
/**
*
* @param tree the Tree to flatten
* @return a list containing all the leaf values in t, in left-to-right order
* @throws IllegalArgumentException if t is null
*/
public List<T> flattenInOrder(Tree<T> tree);
}
interface Function<P, R> {
public R apply(P p);
}
class GetFunction<P, R> implements Function<P, R> {
public R apply(P p) {
return (R) p;
}
}
interface Tree<T> {
Either<T, Triple<Tree<T>>> get();
static final class Leaf<T> implements Tree<T> {
public static <T> Leaf<T> leaf (T value) {
return new Leaf<T>(value);
}
private final T t;
public Leaf(T t) {
this.t = t;
}
@Override
public Either<T, Triple<Tree<T>>> get() {
return Either.left(t);
}
}
static final class Node<T> implements Tree<T> {
public static <T> Tree<T> tree (T left, T middle, T right) {
return new Node<T>(Leaf.leaf(left), Leaf.leaf(middle), Leaf.leaf(right));
}
private final Triple<Tree<T>> branches;
public Node(Tree<T> left, Tree<T> middle, Tree<T> right) {
this.branches = new Triple<Tree<T>>(left, middle, right);
}
@Override
public Either<T, Triple<Tree<T>>> get() {
return Either.right(branches);
}
}
}
class MyFlattenTree<T> implements FlattenTree<T> {
public List<T> flattenInOrder(Tree<T> tree) {
if (tree == null) {
throw new IllegalArgumentException();
}
LinkedList<T> result = new LinkedList<T>();
inorder(tree, result);
return result;
}
private void inorder(Tree<T> current, LinkedList<T> result) {
GetFunction<T,T> leaf_get_function = new GetFunction<T,T>();
GetFunction<Triple<Tree<T>>,Triple<Tree<T>>> triple_get_function = new GetFunction<Triple<Tree<T>>,Triple<Tree<T>>>();
if (current.get().isLeft()) {
/* A leaf */
result.add(current.get().ifLeft(leaf_get_function));
} else {
/* traverse left */
inorder(current.get().ifRight(triple_get_function).left(), result);
/* traverse middle */
inorder(current.get().ifRight(triple_get_function).middle(), result);
/* traverse right */
inorder(current.get().ifRight(triple_get_function).right(), result);
}
}
}
class FlattenTernaryTree
{
public static void main (String[] args) throws java.lang.Exception
{
Tree<Integer> node_one = new Tree.Leaf<Integer>(1);
Tree<Integer> node_five = new Tree.Leaf<Integer>(5);
Tree<Integer> node_four = new Tree.Leaf<Integer>(4);
Tree<Integer> node_nine = new Tree.Leaf<Integer>(9);
Tree<Integer> node_six = new Tree.Leaf<Integer>(6);
Tree<Integer> node_five_four_nine = new Tree.Node<Integer>(node_five,node_four,node_nine);
Tree<Integer> root = new Tree.Node<Integer>(node_one,node_five_four_nine,node_six);
MyFlattenTree<Integer> flatten = new MyFlattenTree<Integer>();
System.out.println(flatten.flattenInOrder(root));
}
}
/*********************************/
Time Complexity O(3^n)
Space Complexity O(n) , where n is number of nodes in tree.
Run Here http://ideone.com/trzycV
/*********************************/