volscolts16 3 Junior Poster in Training

Problem: I am not sure how to get the Treeiterator started into the main, or how output to the file. Thank you in advance sorry for the long read, but it is a long project, for such little output.


The assignment:
Write a program that maintains the names, addresses, and telephone numbers of your friends and relatives and thus serves as an address book. You should be able to insert, delete, modify, or search this data. The person?s name should be the search key, and initially you can assume that the names are unique. The program should be able to save the address book in a file for use later.

Design a class to represent the people in the address book and another class to represent the address book itself. The later class should contain a binary search tree of people as a data field.

You will implement the following operations:
Enter new contact
Delete a contact
View all contact
Edit existing contact
Search by birthday month
Search by state
Output to file
Exit Program


1. class Address {
2.
3. private String street;
4. private String city;
5. private String zip;
6. private String state;
7.
8. public Address(String s, String c, String z, String t) {
9. street = s;
10. city = c;
11. zip = z;
12. state = t;
13. }
14. } // end Address


15. public class FullName implements Comparable<FullName> {
16. private String firstName;
17. private String lastName;
18. public FullName(String first, String last) {
19. firstName = first;
20. lastName = last;
21. } // end constructor
22.
23. public int compareTo(FullName rhs) {
24. // Postcondition: returns 0 if all fields match
25. // if lastName equals rhs.lastName and
26. // firstName is greater than rhs.firstName
27. // returns -1 if lastName is less than rhs.lastName or
28. // if lastName equals rhs.lastName and
29. // firstName is less than rhs.firstName
30. // object.
31. // Throws ClassCastException if rhs cannot be cast to Fullname
32. FullName other = (FullName)rhs;
33. if (lastName.compareTo(((FullName)other).lastName)==0){
34. return firstName.compareTo(((FullName)other).firstName);
35. }
36. else {
37. return lastName.compareTo(((FullName)other).lastName);
38. } // end if
39. } // end compareTo
40.
41. } // end class FullName
42.
43.
44. public class Person extends KeyedItem<String> {
45. // inherits method getKey that returns the search key
46. private FullName name;
47. private String phoneNumber;
48. private Address address;
49. private String day;
50.
51. public Person(String id, FullName name, String phone,
52. Address addr, String birthday ) {
53. super(id); // sets the key value to String id
54. this.name = name;
55. phoneNumber = phone;
56. address = addr;
57. day = birthday;
58. } // end constructor
59.
60. public String toString()
61. {
62. return getKey() + " # " + name;
63. } // end toString
64.
65.
66.
67.
68. } // end Person
69.
70. public abstract class KeyedItem<KT extends Comparable<?super KT>> {
71. private KT searchKey;
72.
73. public KeyedItem(KT key) {
74. searchKey = key;
75. } // end constructor
76.
77. public KT getKey() {
78. return searchKey;
79. } // end getKey
80. } // end KeyedItem
81.
82. // A reference-based implementation of the ADT binary tree
83.
84. public class TreeNode<T> {
85. private T item;
86. private TreeNode<T> leftChild;
87. private TreeNode<T> rightChild;
88.
89. public TreeNode(T newItem) {
90. // Initializes tree node with item and no children.
91. item = newItem;
92. leftChild = null;
93. rightChild = null;
94. } // end constructor
95.
96. public TreeNode(T newItem,
97. TreeNode<T> left, TreeNode<T> right) {
98. // Initializes tree node with item and
99. // the left and right children references.
100. item = newItem;
101. leftChild = left;
102. rightChild = right;
103. } // end constructor
104.
105. public T getItem() {
106. // Returns the item field.
107. return item;
108. } // end getItem
109.
110. public void setItem(T newItem) {
111. // Sets the item field to the new value newItem.
112. item = newItem;
113. } // end setItem
114.
115. public TreeNode<T> getLeft() {
116. // Returns the reference to the left child.
117. return leftChild;
118. } // end getLeft
119.
120. public void setLeft(TreeNode<T> left) {
121. // Sets the left child reference to left.
122. leftChild = left;
123. } // end setLeft
124.
125. public TreeNode<T> getRight() {
126. // Returns the reference to the right child.
127. return rightChild;
128. } // end getRight
129.
130. public void setRight(TreeNode<T> right) {
131. // Sets the right child reference to right.
132. rightChild = right;
133. } // end setRight
134. } // end TreeNode
135.
136. import java.util.LinkedList;
137.
138.
139. public class TreeIterator<T> implements java.util.Iterator<T> {
140. private BinaryTreeBasis<T> binTree;
141. private TreeNode<T> currentNode;
142. private LinkedList <TreeNode<T>> queue; // from JCF
143.
144. public TreeIterator(BinaryTreeBasis<T> bTree) {
145. binTree = bTree;
146. currentNode = null;
147. // empty queue indicates no traversal type currently
148. // selected or end of current traversal has been reached
149. queue = new LinkedList <TreeNode<T>>();
150. } // end constructor
151.
152. public boolean hasNext() {
153. return !queue.isEmpty();
154. } // end hasNext
155.
156. public T next()
157. throws java.util.NoSuchElementException {
158. currentNode = queue.remove();
159. return currentNode.getItem();
160. } // end next
161.
162. public void remove()
163. throws UnsupportedOperationException {
164. throw new UnsupportedOperationException();
165. } // end remove
166.
167. public void setPreorder() {
168. queue.clear();
169. preorder(binTree.root);
170. } // setPreOrder
171.
172. public void setInorder() {
173. queue.clear();
174. inorder(binTree.root);
175. } // end setInorder
176.
177. public void setPostorder() {
178. queue.clear();
179. postorder(binTree.root);
180. } // end setPostorder
181.
182. private void preorder(TreeNode<T> treeNode) {
183. if (treeNode != null) {
184. queue.add(treeNode);
185. preorder(treeNode.getLeft());
186. preorder(treeNode.getRight());
187. } // end if
188. } // end preorder
189.
190. private void inorder(TreeNode<T> treeNode) {
191. if (treeNode != null) {
192. inorder(treeNode.getLeft());
193. queue.add(treeNode);
194. inorder(treeNode.getRight());
195. } // end if
196. } // end inorder
197.
198. private void postorder(TreeNode<T> treeNode) {
199. if (treeNode != null) {
200. postorder(treeNode.getLeft());
201. postorder(treeNode.getRight());
202. queue.add(treeNode);
203. } // end if
204. } // end postorder
205. } // end TreeIterator
206.
207. public class TreeException extends RuntimeException {
208. public TreeException(String s) {
209. super(s);
210. } // end constructor
211.
212. public abstract class BinaryTreeBasis<T> {
213. protected TreeNode<T> root;
214.
215. public BinaryTreeBasis() {
216. root = null;
217. } // end default constructor
218.
219. public BinaryTreeBasis(T rootItem) {
220. root = new TreeNode<T>(rootItem, null, null);
221. } // end constructor
222.
223. public boolean isEmpty() {
224. // Returns true if the tree is empty, else returns false.
225. return root == null;
226. } // end isEmpty
227.
228. public void makeEmpty() {
229. // Removes all nodes from the tree.
230. root = null;
231. } // end makeEmpty
232.
233. public T getRootItem() throws TreeException {
234. // Returns the item in the trees root.
235. if (root == null) {
236. throw new TreeException("TreeException: Empty tree");
237. }
238. else {
239. return root.getItem();
240. } // end if
241. } // end getRootItem
242.
243. public abstract void setRootItem(T newItem);
244. // Throws UnsupportedOperationException if operation
245. // is not supported.
246.
247. } // end BinaryTreeBasis
248.
249. public class BinarySearchTree<T extends KeyedItem<KT>,
250. KT extends Comparable<? super KT>>
251. extends BinaryTreeBasis<T> {
252. // inherits isEmpty(), makeEmpty(), getRootItem(), and
253. // the use of the constructors from BinaryTreeBasis
254.
255. public BinarySearchTree(String name, String address, String birthday )
256. {
257.
258. } // end default constructor
259.
260. public BinarySearchTree(T rootItem) {
261. super(rootItem);
262. } // end constructor
263.
264. public void setRootItem(T newItem)
265. throws UnsupportedOperationException {
266. throw new UnsupportedOperationException();
267. } // end setRootItem
268.
269. public void insert(T newItem) {
270. root = insertItem(root, newItem);
271. } // end insert
272.
273. public T retrieve(KT searchKey) {
274. return retrieveItem(root, searchKey);
275. } // end retrieve
276.
277. public void delete(KT searchKey)
278. throws TreeException {
279. root = deleteItem(root, searchKey);
280. } // end delete
281.
282. public void delete(T item)
283. throws TreeException {
284. root = deleteItem(root, item.getKey());
285. } // end delete
286.
287. protected TreeNode<T> insertItem(TreeNode<T> tNode,
288. T newItem) {
289. TreeNode<T> newSubtree;
290. if (tNode == null) {
291. // position of insertion found; insert after leaf
292. // create a new node
293. tNode = new TreeNode<T>(newItem, null, null);
294. return tNode;
295. } // end if
296. T nodeItem = tNode.getItem();
297.
298. // search for the insertion position
299.
300. if (newItem.getKey().compareTo(nodeItem.getKey()) < 0) {
301. // search the left subtree
302. newSubtree = insertItem(tNode.getLeft(), newItem);
303. tNode.setLeft(newSubtree);
304. return tNode;
305. }
306. else { // search the right subtree
307. newSubtree = insertItem(tNode.getRight(), newItem);
308. tNode.setRight(newSubtree);
309. return tNode;
310. } // end if
311. } // end insertItem
312.
313. protected T retrieveItem(TreeNode<T> tNode,
314. KT searchKey) {
315. T treeItem;
316. if (tNode == null) {
317. treeItem = null;
318. }
319. else {
320. T nodeItem = tNode.getItem();
321. if (searchKey.compareTo(nodeItem.getKey()) == 0) {
322. // item is in the root of some subtree
323. treeItem = tNode.getItem();
324. }
325. else if (searchKey.compareTo(nodeItem.getKey()) < 0) {
326. // search the left subtree
327. treeItem = retrieveItem(tNode.getLeft(), searchKey);
328. }
329. else { // search the right subtree
330. treeItem = retrieveItem(tNode.getRight(), searchKey);
331. } // end if
332. } // end if
333. return treeItem;
334. } // end retrieveItem
335.
336. protected TreeNode<T> deleteItem(TreeNode<T> tNode,
337. KT searchKey) {
338. // Calls: deleteNode.
339. TreeNode<T> newSubtree;
340. if (tNode == null) {
341. throw new TreeException("TreeException: Item not found");
342. }
343. else {
344. T nodeItem = tNode.getItem();
345. if (searchKey.compareTo(nodeItem.getKey()) == 0) {
346. // item is in the root of some subtree
347. tNode = deleteNode(tNode); // delete the item
348. }
349. // else search for the item
350. else if (searchKey.compareTo(nodeItem.getKey()) < 0) {
351. // search the left subtree
352. newSubtree = deleteItem(tNode.getLeft(), searchKey);
353. tNode.setLeft(newSubtree);
354. }
355. else { // search the right subtree
356. newSubtree = deleteItem(tNode.getRight(), searchKey);
357. tNode.setRight(newSubtree);
358. } // end if
359. } // end if
360. return tNode;
361. } // end deleteItem
362.
363. protected TreeNode<T> deleteNode(TreeNode<T> tNode) {
364. // Algorithm note: There are four cases to consider:
365. // 1. The tNode is a leaf.
366. // 2. The tNode has no left child.
367. // 3. The tNode has no right child.
368. // 4. The tNode has two children.
369. // Calls: findLeftmost and deleteLeftmost
370. T replacementItem;
371.
372. // test for a leaf
373. if ( (tNode.getLeft() == null) &&
374. (tNode.getRight() == null) ) {
375. return null;
376. } // end if leaf
377.
378. // test for no left child
379. else if (tNode.getLeft() == null) {
380. return tNode.getRight();
381. } // end if no left child
382.
383. // test for no right child
384. else if (tNode.getRight() == null) {
385. return tNode.getLeft();
386. } // end if no right child
387.
388. // there are two children:
389. // retrieve and delete the inorder successor
390. else {
391. replacementItem = findLeftmost(tNode.getRight());
392. tNode.setItem(replacementItem);
393. tNode.setRight(deleteLeftmost(tNode.getRight()));
394. return tNode;
395. } // end if
396. } // end deleteNode
397.
398. protected T findLeftmost(TreeNode<T> tNode) {
399. if (tNode.getLeft() == null) {
400. return tNode.getItem();
401. }
402. else {
403. return findLeftmost(tNode.getLeft());
404. } // end if
405. } // end findLeftmost
406.
407. protected TreeNode<T> deleteLeftmost(TreeNode<T> tNode) {
408. if (tNode.getLeft() == null) {
409. return tNode.getRight();
410. }
411. else {
412. tNode.setLeft(deleteLeftmost(tNode.getLeft()));
413. return tNode;
414. } // end if
415. } // end deleteLeftmost
416.
417. } // end BinarySearchTree
418.
419.
420. public class AddressBox
421. {
422. public static BinarySearchTree<?,FullName> tree;
423.
424. // TreeIterator interator = new TreeIterator(BinaryTreeBasis<T> bTree) ; ASK about this
425.
426. public static void main (String [] args)
427. {
428. boolean done = false;
429.
430. Scanner in = new Scanner(System.in);
431.
432. while(done != true)
433. {
434. System.out.println("If you want to search input name: press 1, if id: press 2, if u want to add a contact: press 3, to close: press 4");
435. String select = in.nextLine();
436.
437. if(select.equals("1"))
438. {
439. System.out.println("Input full id ");
440. int id = in.nextInt();
441. }
442. else if(select.equals("2"))
443. {
444. System.out.println("Input full name ");
445. String name = in.nextLine();
446. }
447.
448. else if (select.equals("3"))
449. {
450.
451. System.out.println("Enter ID");
452. String id = in.nextLine();
453.
454. System.out.println("first name ");
455. String firstName = in.nextLine();
456.
457. System.out.println("last name ");
458. String lastName = in.nextLine();
459.
460. FullName name = new FullName(firstName, lastName);
461.
462. System.out.println("street");
463. String street = in.nextLine();
464.
465. System.out.println("city");
466. String city = in.nextLine();
467.
468. System.out.println("zip");
469. String zip = in.nextLine();
470.
471. System.out.println("State");
472. String state = in.nextLine();
473.
474. Address ad = new Address(street, city, zip, state);
475.
476. System.out.println("birthday");
477. String birthday = in.nextLine();
478.
479. System.out.println("Phone");
480. String phone = in.nextLine();
481.
482. Person Record = new Person (id, name, phone, ad, birthday);
483. }
484. else if(select.equals("4"))
485. {
486. done = true;
487. try
488. {
489. PrintWriter output = new PrintWriter(new File("C:\\Documents and Settings\\cs306\\Desktop\\New Text Document.txt"));
490. output.interator.setPostorder(); /// this too
491. }
492. catch (IOException e)
493. {
494. System.out.println(e);
495. System.exit(0);
496. }
497.
498. }
499. }
500. }
501. }