-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
258 lines (232 loc) · 6.85 KB
/
BinaryTree.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package assignment2016UniPeopleManagementQUESTION;
/**
* BinaryTree - An example of a Binary Tree built using Node objects.
*/
public class BinaryTree {
/**
* Node root variable to store the root of the tree size to store the number
* of nodes in the tree
*/
private Node root;
private int size = 0;
/**
* Returns the number of nodes in the tree.
*
* @return Number of nodes in the tree.
*/
public int size() {
return size;
}
/**
* Empties the tree
*/
public void clear() {
this.root = null;
size = 0;
}
/**
* Determines if the tree is empty or not.
*
* @return true if the tree is empty, false otherwise
*/
public boolean isEmpty() {
if (root != null) {
return true;
} else {
return false;
}
}
/**
* Adds a Node containing a reference to the people v to the tree. if the
* tree is empty make that node the root, else reference to the recursive
* addingNode method
*
* @param person
* The content of the node that will be added
*/
public void addNode(Node person) {
if (root == null) {
root = person;
size = 1;
} else {
addNode(person, root);
size = size + 1;
}
}
/**
* Internal recursive method to add a node If the first result is negative
* going to the left child and if there is something checks again in the end
* assign a node in the left if the last compare result was negative If the
* first result is positive - analog with the right child,in the end assign
* a node in the right if the last compare result was positive
*
* @param nodeToInsert
* The content to place in the tree
* @param currentNode
* The current node to consider, will not be null
*/
private void addNode(Node nodeToInsert, Node currentNode) {
if (nodeToInsert.compareTo(currentNode) < 0) {
if (currentNode.hasLeft())
addNode(nodeToInsert, currentNode.getLeft());
else {
currentNode.setLeft(nodeToInsert);
return;
}
} else {
if (currentNode.hasRight())
addNode(nodeToInsert, currentNode.getRight());
else {
currentNode.setRight(nodeToInsert);
return;
}
}
}
/**
* Public call to start the recursive pre-order traversal using the root of
* the tree. Pre-order traversal is also known as pre-fix traversal.
*
* @return A reference to a comma separated String containing the trees
* contents as determined by a preOrder Traversal.
*/
public String preOrderTraversal() {
StringBuffer stringBuffer = new StringBuffer();
if (root == null)
return "Empty Tree!";
else
preOrder(root, stringBuffer);
return stringBuffer.toString();
}
/**
* Private method used to make a recursive call from a particular node
*
* @param node
* The node to perform a pre-order traversal from.
*/
private void preOrder(Node node, StringBuffer stringBuffer) {
// visit the Nodes and add contents to our String
stringBuffer.append(node.getPersonName().toString() + ", ");
// 1 Visit Node
if (node.hasLeft())
preOrder(node.getLeft(), stringBuffer);
// 2 Visit left
if (node.hasRight())
preOrder(node.getRight(), stringBuffer);
// 3 Visit right
}
/**
* Public call to start a recursive in-order traversal using the root of the
* tree. In-order traversal is often called in-fix traversal.
*
* @return A reference to a comma separated String containing the trees
* contents as determined by an in-order traversal.
*/
public String inOrderTraversal() {
StringBuffer stringBuffer = new StringBuffer();
if (root == null)
return "Empty Tree!";
else
inOrder(root, stringBuffer);
return stringBuffer.toString();
}
/**
* Private method used to make a recursive in-order traversal from node 'n'
*
* @param node
* The node to perform the inOrder walk from
*/
private void inOrder(Node node, StringBuffer stringBuffer) {
if (node.hasLeft())
inOrder(node.getLeft(), stringBuffer);
stringBuffer.append(node.getPersonName().toString() + ", ");
if (node.hasRight())
inOrder(node.getRight(), stringBuffer);
}
/**
* Public call to start the recursive post-order traversal using the root of
* the tree. Post order traversal is also called postfix and is used to
* create a Reverse Polish notation of a tree suitable for calculating
* equations.
*
* @return A reference to a comma separated String containing the trees
* contents as determined by a post-order traversal.
*/
public String postOrderTraversal() {
StringBuffer stringBuffer = new StringBuffer();
if (root == null)
return "Empty Tree!";
else
postOrder(root, stringBuffer);
return stringBuffer.toString();
}
/**
* Private method used to make a recursive postOrder traversal for node 'n'
*
* @param node
* The node to perform the postOrder walk from
*/
private void postOrder(Node node, StringBuffer stringBuffer) {
if (node.hasLeft())
postOrder(node.getLeft(), stringBuffer);
if (node.hasRight())
postOrder(node.getRight(), stringBuffer);
stringBuffer.append(node.getPersonName().toString() + ", ");
}
/**
* reference method for finding the name in the list of people
*
* @param name
* @return
*/
public Person find(String name) {
return find(name, root);
}
/**
* method to find the name in the list/tree of nodes by comparing variables
*
* @param name
* @param node
* @return
*/
private Person find(String name, Node node) {
if (node == null)
return null;
int order = name.compareTo(node.getPersonName().toString());
if (order == 0)
return node.getPerson();
if (node.hasLeft() && order < 0)
return find(name, node.getLeft());
if (node.hasRight() && order > 0)
return find(name, node.getRight());
return null;
}
/**
* method to print out the binary tree
*/
public void printTree() {
System.out.println("\n");
printIndentedTree(root, 0);
}
/**
* Internal method used to make a recursive reverse order walk from node
* 'n'. This method is useful to see the current structure of the tree
* (turned on its side)
*
* @param node
* The node to start the reverse order walk from
* @param depth
* The current depth of the node in the tree
* @param print
* true if we should print out node, false otherwise
*/
private void printIndentedTree(Node node, int depth) {
if (node.hasRight())
printIndentedTree(node.getRight(), depth + 1);
// Show depth of current code by indenting to the right
for (int d = 0; d < depth; d++)
System.out.print(" ");
System.out.println(node.getPersonName().toString());
if (node.hasLeft())
printIndentedTree(node.getLeft(), depth + 1);
}
}