Dictionary.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * Mine, except for the iteratorInOrder and inorder methods,
  3. * which I took from LinkedBinaryTree to do the toString() printout.
  4. */
  5. import java.util.Iterator;
  6. public class Dictionary<T> extends AVLTree<T> implements DictionaryADT<T> {
  7. /**
  8. * Constructor
  9. */
  10. public Dictionary () {
  11. super();
  12. }
  13. /**
  14. * findEntry:
  15. * Returns a String.
  16. * Searches for specified element in the dictionary.
  17. */
  18. public String findEntry (T key) {
  19. Node<T> temp = this.find(key);
  20. return temp.toString();
  21. }
  22. /**
  23. * insertEntry:
  24. * Returns void.
  25. * Inserts the specified element with the specified key into the dictionary.
  26. */
  27. public void insertEntry (T key, T element) {
  28. this.insert(key, element);
  29. }
  30. /**
  31. * removeEntry:
  32. * Returns a String.
  33. * Searches for and removes the specified element from the dictionary, and returns its value.
  34. */
  35. public String removeEntry (T key) {
  36. Node<T> temp = this.find(key);
  37. System.out.println (temp.toString());
  38. this.remove(temp);
  39. return ("");
  40. }
  41. /**
  42. * toString:
  43. * Returns a String.
  44. * Gives a printout of the tree.
  45. */
  46. public String toString () {
  47. String result = "";
  48. Iterator<T> iterator = iteratorInOrder();
  49. while (iterator.hasNext()){
  50. result += (iterator.next() + "\n");
  51. }
  52. return result;
  53. }
  54. }