/home/sjenks/NetBeansProjects/CS2/src/trees/BinaryTree.java |
1
2
3
4
5
6
7 package trees;
8
9
10
11 @author
12
13 public class BinaryTree<K, V> implements BinaryTreeADT<K, V> {
14
15 private K key;
16 private V value;
17 private BinaryTree<K, V> lchild;
18 private BinaryTree<K, V> rchild;
19
20
21
22
23 public BinaryTree() {
24 key = null;
25 }
26
27
28 @return
29
30 public V value() {
31 return value;
32 }
33
34
35 Creates new V value instance
36
37 @param value
38
39 public void setValue(V value) {
40 this.value = value;
41 }
42
43
44 Checks to see if a node is empty in the tree
45
46 @return
47
48
49 public boolean empty() {
50 return key == null;
51 }
52
53
54 Computes the height of the tree
55
56 @return
57
58
59 public int height() {
60 if (empty()) {
61 return 0;
62 } else {
63 return 1 + max(lchild.height(), rchild.height());
64 }
65 }
66
67
68 Determines which child has the highest value Useful for determining the
69 height of the tree.
70
71 @param a
72 @param b
73 @return
74
75 private int max(int a, int b) {
76 if (a > b) {
77 return a;
78 } else {
79 return b;
80 }
81 }
82
83
84
85
86
87 public void preorder() {
88 if (!empty()) {
89 visit();
90 lchild.preorder();
91 rchild.preorder();
92 }
93 }
94
95
96
97
98
99 public void inorder() {
100 if (!empty()) {
101 lchild.inorder();
102 visit();
103 rchild.inorder();
104 }
105 }
106
107
108
109
110
111 public void postorder() {
112 if (!empty()) {
113 lchild.postorder();
114 rchild.postorder();
115 visit();
116 }
117 }
118
119
120 orders the nodes so it forms a binary search tree
121
122 @param key
123 @param value
124 @throws
125
126
127 public void addST(K key, V value) throws BinaryTreeCreationException {
128 if (empty()) {
129 sprout(key, value);
130 } else {
131 Comparable cey = (Comparable) key;
132 Comparable t = (Comparable) this.key;
133 if (cey.compareTo(t) < 0) {
134 lchild.addST(key, value);
135 } else if (cey.compareTo(t) > 0) {
136 rchild.addST(key, value);
137 } else {
138 throw new BinaryTreeCreationException("### can't have equality in bst");
139 }
140 }
141 }
142
143
144 orders the nodes so it forms a tree with random connections
145
146 @param key
147 @param value
148
149 public void addND(K key, V value) {
150 if (empty()) {
151 sprout(key, value);
152 } else if (Math.random() < 0.5) {
153 lchild.addND(key, value);
154 } else {
155 rchild.addND(key, value);
156 }
157 }
158
159
160 This allows a person to make their own BST
161
162 @param key
163 @param value
164 @param dir
165 @throws
166
167
168 public void addLR(K key, V value, String dir) throws BinaryTreeCreationException {
169 try {
170 if (dir.equals("")) {
171 sprout(key, value);
172 } else if (dir.substring(0, 1).equalsIgnoreCase("L")) {
173 lchild.addLR(key, value, dir.substring(1));
174 } else if (dir.substring(0, 1).equalsIgnoreCase("R")) {
175 rchild.addLR(key, value, dir.substring(1));
176 } else {
177 throw new BinaryTreeCreationException("### strange directional indicator: " + dir.substring(0, 1));
178 }
179 } catch (BinaryTreeCreationException e) {
180 throw new BinaryTreeCreationException("### no such location in the tree");
181 }
182 }
183
184
185 gets the value of the node by looking up the name
186
187 @param key
188 @return
189
190 public V get(K key) {
191 BinaryTree<K, V> t = find(key);
192 return t.value;
193 }
194
195
196 finds a node in the tree
197
198 @param key
199 @return
200
201 public BinaryTree<K, V> find(K key) {
202 if (empty()) {
203 return null;
204 } else if (this.key.equals(key)) {
205 return this;
206 } else {
207 BinaryTree<K, V> l = lchild.find(key);
208 if (l != null) {
209 return l;
210 }
211 BinaryTree<K, V> r = rchild.find(key);
212 if (r != null) {
213 return r;
214 }
215 return null;
216 }
217 }
218
219
220 sees if a node is even in the tree
221
222 @param key
223 @return
224
225 public boolean member(K key) {
226 if (empty()) {
227 return false;
228 } else if (this.key.equals(key)) {
229 return true;
230 } else {
231 return (lchild.member(key) | rchild.member(key));
232 }
233 }
234
235
236
237
238 public void visit() {
239 String rep;
240 if (value == null) {
241 rep = "---";
242 } else {
243 rep = value.toString();
244 }
245 String s = "[" + key + ":" + rep + "]";
246 System.out.println(s);
247 }
248
249
250 to make a new node
251
252 @param key
253 @param value
254
255 public void sprout(K key, V value) {
256 this.key = key;
257 this.value = value;
258 this.lchild = new BinaryTree<K, V>();
259 this.rchild = new BinaryTree<K, V>();
260 }
261
262 }
263