-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultBinaryTree.java
More file actions
242 lines (225 loc) · 5.84 KB
/
DefaultBinaryTree.java
File metadata and controls
242 lines (225 loc) · 5.84 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 datastructures;
/**
* This class creates a binary tree
*
* @author Ching2 Huang
*
* @param data
*/
public class DefaultBinaryTree<T> implements BinaryTree<T> {
// the root of the tree
private DefaultBinaryTreeNode<T> root;
/**
* Constructor initializes the root null
*/
public DefaultBinaryTree() {
// root is null
root = null;
}
/**
* main method creates a tree with 7 dwarfs
*
* @param arguments
*/
public static void main(String[] args) {
// a tree for seven dwarfs
DefaultBinaryTree<String> tree = new DefaultBinaryTree<String>();
// happy dwarf
DefaultBinaryTreeNode<String> happy = new DefaultBinaryTreeNode<String>(
"Happy");
// doc dwarf
DefaultBinaryTreeNode<String> doc = new DefaultBinaryTreeNode<String>(
"Doc");
// bashful dwarf
DefaultBinaryTreeNode<String> bashful = new DefaultBinaryTreeNode<String>(
"Bashful");
// grumpy dwarf
DefaultBinaryTreeNode<String> grumpy = new DefaultBinaryTreeNode<String>(
"Grumpy");
// sleepy dwarf
DefaultBinaryTreeNode<String> sleepy = new DefaultBinaryTreeNode<String>(
"Sleepy");
// sneezy dwarf
DefaultBinaryTreeNode<String> sneezy = new DefaultBinaryTreeNode<String>(
"Sneezy");
// set happy dwarf to be the root of the tree
tree.setRoot(happy);
// doc is the first left subtree
happy.setLeftChild(doc);
// set bashful as the left child of doc
doc.setLeftChild(bashful);
// set grumpy as the right child of doc
doc.setRightChild(grumpy);
// set sleep to be the first right subtree
happy.setRightChild(sleepy);
// set sneezy to be the left child of sleepy
sleepy.setRightChild(sneezy);
}
/**
* Get the root node for this tree.
*
* @return root or null if tree is empty.
*/
@Override
public BinaryTreeNode<T> getRoot() {
// if the root doesn't exist
if (root == null) {
// return null
return null;
} else {
// otherwise, return the root
return root;
}
}
/**
* Set the root node for this tree.
*/
@Override
public void setRoot(BinaryTreeNode<T> root) {
// set root
this.root = (DefaultBinaryTreeNode<T>) root;
}
/**
* Test if the tree is empty.
*
* @return true if tree has no data.
*/
@Override
public boolean isEmpty() {
// check whether the root exist
return root == null;
}
/**
* Get the data of this tree using inorder traversal. left -- data -- right
*
* @return inorder List.
*/
@Override
public LinkedList<T> inorderTraversal() {
// create a list to store data
LinkedList<T> list = new LinkedList<T>();
// call inorder traversal method to organize the data
inorderTraversal(root, list);
// return the list with data
return list;
}
/**
* Get the data of this tree using inorder traversal. left -- data -- right
*
* @param root
* @param list
* to store data
*/
private void inorderTraversal(BinaryTreeNode<T> node, LinkedList<T> list) {
// if node doesn't exist
if (node == null) {
// return here
return;
// if the node has no children
} else if (node.isLeaf()) {
// add data to the list
list.insertLast(node.getData());
} else {
// otherwise, get the left child
inorderTraversal(node.getLeftChild(), list);
// then get the data
list.insertLast(node.getData());
// finally, get the right child
inorderTraversal(node.getRightChild(), list);
}
}
/**
* Get the data of this tree using preorder traversal. data -- left -- right
*
* @return preorder List.
*/
@Override
public LinkedList<T> preorderTraversal() {
// create a list to store data
LinkedList<T> list = new LinkedList<T>();
// call preorder method to organize the data and add them to the list
preorderTraversal(root, list);
// return a list with data
return list;
}
/**
* Get the data of this tree using preorder traversal. data -- left -- right
*
* @param root
* @param data
* list
*/
private void preorderTraversal(BinaryTreeNode<T> node, LinkedList<T> list) {
// if the node doens't exist
if (node == null) {
// return here
return;
// if the node has no children
} else if (node.isLeaf()) {
// add the data to the list
list.insertLast(node.getData());
} else {
// otherwise, get the data of the node
list.insertLast(node.getData());
// keep checking from the left child
preorderTraversal(node.getLeftChild(), list);
// keep checking from the rigth child
preorderTraversal(node.getRightChild(), list);
}
}
/**
* Get the data of this tree using postorder traversal. left -- right --
* data
*
* @return postorder List.
*/
@Override
public LinkedList<T> postorderTraversal() {
LinkedList<T> list = new LinkedList<T>();
postorderTraversal(root, list);
return list;
}
/**
* Get the data of this tree using postorder traversal. left -- right --
* data
*
* @param root
* @param list
*/
private void postorderTraversal(BinaryTreeNode<T> node, LinkedList<T> list) {
// if the node doesn't exist
if (node == null) {
// return here
return;
// if the node has no children
} else if (node.isLeaf()) {
// add the data of the node to the list
list.insertLast(node.getData());
} else {
// otherwise, get the left child first
postorderTraversal(node.getLeftChild(), list);
// then get the right child
postorderTraversal(node.getRightChild(), list);
// finally, get the data
list.insertLast(node.getData());
}
}
public String printLeaves(BinaryTreeNode<T> node) {
LinkedList<T> list = new LinkedList<T>();
printLeaves(node, list);
return list.toString();
}
private void printLeaves(BinaryTreeNode<T> node, LinkedList<T> list) {
// if node doesn't exist
if (node == null) {
// return here
return;
// if the node has no children
} else if (node.isLeaf()) {
list.insertLast(node.getData());
} else {
printLeaves(node.getLeftChild(), list);
printLeaves(node.getRightChild(), list);
}
}
}