BST.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*Dylan Jeffers
  2. *Tahmid Rahman
  3. *founders of RahmROMJeffers ltd.
  4. */
  5. #ifndef BST_H_
  6. #define BST_H_
  7. #include "pair.h"
  8. #include "library/queue.h"
  9. /* An abstract (pure-virtual) representation of a binary-search-tree
  10. * (BST). Note that it is template definition based on a type for key
  11. * (K) and type for value (V). BST is a dictionary structure; that is,
  12. * elements are indexed by key and all keys must be unique.
  13. */
  14. template <typename K, typename V>
  15. class BST {
  16. public:
  17. virtual ~BST() {};
  18. /* getSize - returns the size of the BST
  19. * @return int: the number of key-value pairs in the data structure
  20. */
  21. virtual int getSize() = 0;
  22. /* isEmpty - returns true if the tree is empty
  23. * @return bool: true if there are no elements in the BST
  24. */
  25. virtual bool isEmpty() = 0;
  26. /* getHeight - returns a height for the tree (i.e., largest
  27. * depth for any leaf node)
  28. * @return int: height of tree, -1 if tree is empty
  29. */
  30. virtual int getHeight() = 0;
  31. /* getMax - returns the largest key in the data structure
  32. * @return K: maximum key in the BST
  33. */
  34. virtual K getMax() = 0;
  35. /* getMin - returns the smallest key in the data structure
  36. * @return K: minimum key in the BST
  37. */
  38. virtual K getMin() = 0;
  39. /* insert - inserts the key-value pair into the tree
  40. * @param key - key for indexing the new element
  41. * @param value - value associated with the given key
  42. * @error runtime_error if they key already exists
  43. */
  44. virtual void insert(K key, V value) = 0;
  45. /* update - finds the element indexed by the given key and updates
  46. * its value to the provided value parameter
  47. * @param key - key for finding the existing element
  48. * @param value - the new value to store for the given key
  49. * @error runtime_error if the key is not found in the BST
  50. */
  51. virtual void update(K key, V value) = 0;
  52. /* find - returns the value associated with the given key
  53. * @param key - index key for element to find
  54. * @error runtime_error if the key is not found in the BST
  55. * @return V: value associated with given key
  56. */
  57. virtual V find(K key) = 0;
  58. /* contains - returns true if there exists an element in the BST
  59. * with the given key
  60. * @param key - index key to search for
  61. * @return bool: true if the given key exists in the BST
  62. */
  63. virtual bool contains(K key) = 0;
  64. /* remove - deletes the element with given key from the tree
  65. * @param key - index key to search for and remove
  66. * @error: runtime_error if they key is not found
  67. */
  68. virtual void remove(K key) = 0;
  69. /* The following are the iterators provided for the BST interface.
  70. * Each returns a linear ordering of all elements in the BST stored in a
  71. * Queue data structure. Each key-value is stored as a Pair object
  72. * @return Queue< Pair<K,V> >*: a pointer to a dynamically allocated
  73. * Queue with key-value pairs. The caller is responsible for handling
  74. * the heap memory deallocation
  75. */
  76. virtual Queue< Pair<K,V> >* getPreOrder() = 0;
  77. virtual Queue< Pair<K,V> >* getInOrder() = 0;
  78. virtual Queue< Pair<K,V> >* getPostOrder() = 0;
  79. virtual Queue< Pair<K,V> >* getLevelOrder() = 0;
  80. };
  81. #endif