BST.h 3.4 KB

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