-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedBinaryTree.java
184 lines (140 loc) · 4.11 KB
/
LinkedBinaryTree.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
import java.util.Iterator;
public class LinkedBinaryTree<T> implements BinaryTreeADT<T> {
private int count;
private BinaryTreeNode<T> root;
public LinkedBinaryTree() {
count = 0;
root = null;
}
public LinkedBinaryTree (T element) {
count = 1;
root = new BinaryTreeNode<T> (element);
}
public BinaryTreeNode<T> getRoot() {
return(root);
}
public void setRoot(BinaryTreeNode<T> root) {
this.root = root;
}
public boolean isEmpty() {
return count == 0;
}
public int size() {
return count;
}
public boolean contains (T targetElement) {
return findAgain(targetElement,root) != null ;
}
public T find(T targetElement) throws ElementNotFoundException {
BinaryTreeNode<T> current = findAgain(targetElement,root);
if( current == null )
throw new ElementNotFoundException("binary tree");
return (current.getElement());
}
/**
* Returns a reference to the specified target element if it is
* found in this binary tree.
*
* @param targetElement the element being sought in this tree
* @param next the element to begin searching from
*/
private BinaryTreeNode<T> findAgain(T targetElement, BinaryTreeNode<T> next) {
if (next == null)
return null;
if (next.getElement().equals(targetElement))
return next;
BinaryTreeNode<T> temp = findAgain(targetElement, next.getLeft());
if (temp == null)
temp = findAgain(targetElement, next.getRight());
return temp;
}
/**
* Returns a string representation of this binary tree.
*
* @return a string representation of this binary tree
*/
public String toString() {
String s = "";
Iterator<T> list ;
list = iteratorInOrder();
while (list.hasNext()) {
s += list.next().toString();
}
return s;
}
/**
* Performs an inorder traversal on this binary tree by calling an
* overloaded, recursive inorder method that starts with
* the root.
*
* @return an in order iterator over this binary tree
*/
public Iterator<T> iteratorInOrder() {
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
inorder (root, tempList);
return tempList.iterator();
}
/**
* Performs a recursive inorder traversal.
*
* @param node the node to be used as the root for this traversal
* @param tempList the temporary list for use in this traversal
*/
protected void inorder (BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) {
if (node != null) {
inorder (node.getLeft(),tempList);
tempList.addToRear(node.getElement());
inorder (node.getRight(),tempList);
}
}
/**
* Performs an preorder traversal on this binary tree by calling
* an overloaded, recursive preorder method that starts with
* the root.
*
* @return an pre order iterator over this tree
*/
public Iterator<T> iteratorPreOrder() {
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
preorder(root, tempList);
return tempList.iterator();
}
/**
* Performs a recursive preorder traversal.
*
* @param node the node to be used as the root for this traversal
* @param tempList the temporary list for use in this traversal
*/
protected void preorder (BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) {
if (node != null) {
tempList.addToRear(node.getElement());
preorder(node.getLeft(),tempList);
preorder(node.getRight(),tempList);
}
}
/**
* Performs an postorder traversal on this binary tree by calling
* an overloaded, recursive postorder method that starts
* with the root.
*
* @return a post order iterator over this tree
*/
public Iterator<T> iteratorPostOrder() {
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
postorder(root, tempList);
return tempList.iterator();
}
/**
* Performs a recursive postorder traversal.
*
* @param node the node to be used as the root for this traversal
* @param tempList the temporary list for use in this traversal
*/
protected void postorder (BinaryTreeNode<T> node, ArrayUnorderedList<T> tempList) {
if (node != null) {
postorder(node.getLeft(),tempList);
postorder(node.getRight(),tempList);
tempList.addToRear(node.getElement());
}
}
}