-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericBinaryTree.java
More file actions
242 lines (207 loc) · 7.45 KB
/
GenericBinaryTree.java
File metadata and controls
242 lines (207 loc) · 7.45 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
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
package com.example.data;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class GenericBinaryTree<T extends Comparable<? super T>> {
public enum RecursionDirection {
LEFT,
RIGHT
}
public class Element {
public T data;
public Element leftChild;
public Element rightChild;
public Element(T data, Element leftChild, Element rightChild) {
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public Element(T data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
}
public GenericBinaryTree() {
this.size = 0;
this.root = null;
this.elementWithMaxData = null;
this.elementWithMinData = null;
}
private int size;
public Element root;
private Element elementWithMinData;
private Element elementWithMaxData;
public Element getElementWithMinData() {
return this.elementWithMinData;
}
public Element getElementWithMaxData() {
return this.elementWithMaxData;
}
public T delete(T data) {
// TODO: If deletion succeeds, decrement size.
// this.size--;
return null;
}
public void insert(T data) {
Element element = new Element(data);
if (this.elementWithMaxData.data.compareTo(data) < 0)
this.elementWithMaxData = element;
if (this.elementWithMinData.data.compareTo(data) > 0)
this.elementWithMinData = element;
// TODO: Add the new element to the tree.
// If insertion succeeds, increment size.
// this.size++;
return;
}
public int getNumberOfLeaves(Element localRoot) {
if (localRoot.leftChild == null && localRoot.rightChild == null)
return 1;
int numberOfLeaves = 0;
if (localRoot.leftChild != null)
numberOfLeaves += getNumberOfLeaves(localRoot.leftChild);
if (localRoot.rightChild != null)
numberOfLeaves += getNumberOfLeaves(localRoot.rightChild);
return numberOfLeaves;
}
public int getTotalNumberOfInternalElements(Element localRoot) {
if (localRoot == null ||
(localRoot.leftChild == null && localRoot.rightChild == null))
return 0;
else return 1 +
getTotalNumberOfInternalElements(localRoot.leftChild) +
getTotalNumberOfInternalElements(localRoot.rightChild);
}
public List<T> inOrderTraversal() {
List<T> inOrderTraversal = new ArrayList<T>();
inOrderTraversal(this.root, inOrderTraversal);
return inOrderTraversal;
}
private void inOrderTraversal(Element root, final List<T> inOrderTraversal) {
if (root == null) return;
if (root.leftChild != null) inOrderTraversal(root.leftChild, inOrderTraversal);
inOrderTraversal.add(root.data);
if (root.rightChild != null) inOrderTraversal(root.rightChild, inOrderTraversal);
}
public List<T> preOrderTraversal() {
List<T> preOrderTraversal = new ArrayList<T>();
preOrderTraversal(this.root, preOrderTraversal);
return preOrderTraversal;
}
private void preOrderTraversal(Element root, final List<T> preOrderTraversal) {
if (root == null) return;
preOrderTraversal.add(root.data);
if (root.leftChild != null) preOrderTraversal(root.leftChild, preOrderTraversal);
if (root.rightChild != null) preOrderTraversal(root.rightChild, preOrderTraversal);
}
public List<T> postOrderTraversal() {
List<T> postOrderTraversal = new ArrayList<T>();
postOrderTraversal(this.root, postOrderTraversal);
return postOrderTraversal;
}
private void postOrderTraversal(Element root, final List<T> postOrderTraversal) {
if (root == null) return;
if (root.leftChild != null) postOrderTraversal(root.leftChild, postOrderTraversal);
if (root.rightChild != null) postOrderTraversal(root.rightChild, postOrderTraversal);
postOrderTraversal.add(root.data);
}
public void convertBinaryTreeToLinkedListInOrderTraversal() {
Element previous = convertBinaryTreeToLinkedList(this.root.leftChild, RecursionDirection.LEFT);
Element next = convertBinaryTreeToLinkedList(this.root.rightChild, RecursionDirection.RIGHT);
this.root.leftChild = previous;
if (previous != null) previous.rightChild = this.root;
this.root.rightChild = next;
if (next != null) next.leftChild = this.root;
}
private Element convertBinaryTreeToLinkedList(Element root,
RecursionDirection recursionDirection) {
if (root == null) return null;
if (root.leftChild == null && root.rightChild == null) return root;
Element previous = convertBinaryTreeToLinkedList(root.leftChild,
RecursionDirection.LEFT);
Element next = convertBinaryTreeToLinkedList(root.rightChild,
RecursionDirection.RIGHT);
if (recursionDirection == RecursionDirection.LEFT) {
if (next == null) {
root.leftChild = previous;
if (previous != null) previous.rightChild = root;
return root;
} else {
root.leftChild = previous;
if (previous != null) previous.rightChild = root;
root.rightChild = next;
next.leftChild = root;
return next;
}
} else {
if (previous == null) {
root.rightChild = next;
if (next != null) next.leftChild = root;
return root;
} else {
root.leftChild = previous;
previous.rightChild = root;
root.rightChild = next;
if (next != null) next.leftChild = root;
return previous;
}
}
}
public void findPathSumToValue(int value) {
if (this.root == null) return;
findPathSumToValue(value, (Number) this.root.data, this.root.leftChild);
findPathSumToValue(value, (Number) this.root.data, this.root.rightChild);
}
private void findPathSumToValue(int value, Number sum, Element child) {
sum = sum.intValue() + ((Number) child.data).intValue();
if (sum.intValue() == value) {
System.out.println("Sum of values from root to node " +
child.data.toString() + " equls to " + value);
} else if (sum.intValue() < value) {
if (child.leftChild != null)
findPathSumToValue(value, sum, child.leftChild);
if (child.rightChild != null)
findPathSumToValue(value, sum, child.rightChild);
}
}
@SuppressWarnings("unchecked")
public T[] serialize() {
if (this.root == null) return null;
T[] serializedTree = (T[]) new Object();
final List<T> serializedTreeAsList = new ArrayList<T>();
serializeTree(this.root, serializedTreeAsList);
for (int i = 0; i < serializedTreeAsList.size(); i++)
serializedTree[i] = serializedTreeAsList.get(i);
return serializedTree;
}
private void serializeTree(final Element rootOfTree, final List<T> serializedTreeAsList) {
if (rootOfTree != null) {
serializedTreeAsList.add(rootOfTree.data);
if (rootOfTree.leftChild != null) serializeTree(rootOfTree.leftChild, serializedTreeAsList);
if (rootOfTree.rightChild != null) serializeTree(rootOfTree.rightChild, serializedTreeAsList);
serializedTreeAsList.add(null); // Add a signature to indicate moving one level up in the tree.
} else return;
}
public GenericBinaryTree<T> deserialize(final T[] serializedTree) {
if (serializedTree == null || serializedTree.length == 0) return null;
Element child = null;
Element parent = null;
GenericBinaryTree<T> genericBinaryTree = new GenericBinaryTree<T>();
final Stack<Element> preOrderTraversalStack = new Stack<Element>();
for (int i = 0; i < serializedTree.length; i++) {
if (serializedTree[i] != null) {
preOrderTraversalStack.push(new Element(serializedTree[i]));
} else {
child = preOrderTraversalStack.pop();
if (preOrderTraversalStack.peek() != null) {
parent = preOrderTraversalStack.peek();
if (parent.leftChild == null) parent.leftChild = child;
else if (parent.rightChild == null) parent.rightChild = child;
} else {
genericBinaryTree.root = child;
}
}
}
return genericBinaryTree;
}
}