Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. package tree;
  2.  
  3. import java.util.Collection;
  4.  
  5. /**
  6.  
  7. * This class is used to represent the empty search tree: a search tree that
  8.  
  9. * contains no entries.
  10.  
  11. *
  12.  
  13. * This class is a singleton class: since all empty search trees are the same,
  14.  
  15. * there is no need for multiple instances of this class. Instead, a single
  16.  
  17. * instance of the class is created and made available through the static field
  18.  
  19. * SINGLETON.
  20.  
  21. *
  22.  
  23. * The constructor is private, preventing other code from mistakenly creating
  24.  
  25. * additional instances of the class.
  26.  
  27. *
  28.  
  29. */
  30.  
  31. public class EmptyTree<K extends Comparable<K>, V> implements Tree<K, V> {
  32.  
  33. /**
  34.  
  35. * This static field references the one and only instance of this class. We
  36.  
  37. * won’t declare generic types for this one, so the same singleton can be used
  38.  
  39. * for any kind of EmptyTree.
  40.  
  41. */
  42.  
  43. private static EmptyTree SINGLETON = new EmptyTree();
  44.  
  45. public static <K extends Comparable<K>, V> EmptyTree<K, V> getInstance() {
  46.  
  47. return SINGLETON;
  48.  
  49. }
  50.  
  51. /**
  52.  
  53. * Constructor is private to enforce it being a singleton
  54.  
  55. *
  56.  
  57. */
  58.  
  59. private EmptyTree() {
  60.  
  61. // Nothing to do
  62.  
  63. }
  64.  
  65. public V search(K key) {
  66.  
  67. return null;
  68.  
  69. // throw new UnsupportedOperationException(“You must implement this method.”);
  70.  
  71. }
  72.  
  73. public NonEmptyTree<K, V> insert(K key, V value) {
  74.  
  75. Tree<K, V> leftTemp = EmptyTree.SINGLETON;
  76.  
  77. Tree<K, V> rightTemp = EmptyTree.SINGLETON;
  78.  
  79. return new NonEmptyTree<K, V>(key, value, leftTemp, rightTemp);
  80.  
  81. // throw new UnsupportedOperationException(“You must implement this method.”);
  82.  
  83. }
  84.  
  85. public Tree<K, V> delete(K key) {
  86.  
  87. return this;
  88.  
  89. // throw new UnsupportedOperationException(“You must implement this method.”);
  90.  
  91. }
  92.  
  93. public K max() throws TreeIsEmptyException {
  94.  
  95. throw new TreeIsEmptyException();
  96.  
  97. // throw new UnsupportedOperationException(“You must implement this method.”);
  98.  
  99. }
  100.  
  101. public K min() throws TreeIsEmptyException {
  102.  
  103. throw new TreeIsEmptyException();
  104.  
  105. // throw new UnsupportedOperationException(“You must implement this method.”);
  106.  
  107. }
  108.  
  109. public int size() {
  110.  
  111. return 0;
  112.  
  113. // throw new UnsupportedOperationException(“You must implement this method.”);
  114.  
  115. }
  116.  
  117. public void addKeysToCollection(Collection<K> c) {
  118.  
  119. return;
  120.  
  121. // throw new UnsupportedOperationException(“You must implement this method.”);
  122.  
  123. }
  124.  
  125. public Tree<K, V> subTree(K fromKey, K toKey) {
  126.  
  127. return this;
  128.  
  129. // throw new UnsupportedOperationException(“You must implement this method.”);
  130.  
  131. }
  132.  
  133. public int height() {
  134.  
  135. return 0;
  136.  
  137. // throw new UnsupportedOperationException(“You must implement this method.”);
  138.  
  139. }
  140.  
  141. public void inorderTraversal(TraversalTask<K, V> p) {
  142.  
  143. // Nothing to do
  144.  
  145. // throw new UnsupportedOperationException(“You must implement this method.”);
  146.  
  147. }
  148.  
  149. public void rightRootLeftTraversal(TraversalTask<K, V> p) {
  150.  
  151. // Nothing to do
  152.  
  153. // throw new UnsupportedOperationException(“You must implement this method.”);
  154.  
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement