SplayTree.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SplayTree.h
  2. * Tahmid Rahman
  3. * Dylan Jeffers
  4. *
  5. * This is the header file
  6. * for SplayTree
  7. */
  8. #ifndef SPLAYTREE_H_
  9. #define SPLAYTREE_H_
  10. #include "BST.h"
  11. #include "pair.h"
  12. #include "library/queue.h"
  13. #include <iostream>
  14. using namespace std;
  15. // Forward declaration of SplayTreeNode class
  16. template <typename K, typename V> class SplayTreeNode;
  17. template<typename K, typename V>
  18. class SplayTree: public BST<K,V> {
  19. private:
  20. int size;
  21. SplayTreeNode<K,V>* root;
  22. public:
  23. SplayTree();
  24. ~SplayTree();
  25. /* All public functions declared/detailed in BST.h */
  26. /* The following methods are defined in SplayTree-inl.h */
  27. int getSize();
  28. bool isEmpty();
  29. int getHeight();
  30. K getMax();
  31. K getMin();
  32. /* dictionary operations */
  33. void insert (K key, V value);
  34. void update (K key, V value);
  35. bool contains (K key);
  36. void remove (K key);
  37. V find (K key);
  38. /* traversal operations */
  39. Queue< Pair<K,V> >* getPreOrder();
  40. Queue< Pair<K,V> >* getInOrder();
  41. Queue< Pair<K,V> >* getPostOrder();
  42. Queue< Pair<K,V> >* getLevelOrder();
  43. K getRootKey();
  44. private:
  45. /*declarations of our interal private methods */
  46. SplayTreeNode<K,V>* insertInSubtree(SplayTreeNode<K,V>* current, K key, V value, bool* inserted, bool* skip);
  47. void updateInSubtree(SplayTreeNode<K,V>* current, K key, V value);
  48. SplayTreeNode<K,V>* removeFromSubtree (SplayTreeNode<K,V>* current, K key);
  49. bool containsInSubtree (SplayTreeNode<K,V>* current, K key, bool * skip);
  50. K getMaxInSubtree(SplayTreeNode<K,V>* current);
  51. K getMinInSubtree(SplayTreeNode<K,V>* current);
  52. void buildPreOrder (SplayTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  53. void buildInOrder (SplayTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  54. void buildPostOrder(SplayTreeNode<K,V>* current, Queue< Pair<K,V> >* it);
  55. void traverseAndDelete (SplayTreeNode<K,V>* current);
  56. SplayTreeNode<K,V>* splay(SplayTreeNode<K,V>* current, K key);
  57. int getHeightOfSubtree(SplayTreeNode<K,V>* current);
  58. /* the six rotations needed to fix each of the six imbalances*/
  59. SplayTreeNode<K,V>* rightRotate(SplayTreeNode<K,V>* current);
  60. SplayTreeNode<K,V>* leftRotate(SplayTreeNode<K,V>* current);
  61. SplayTreeNode<K,V>* rightLeftRotate(SplayTreeNode<K,V>* current);
  62. SplayTreeNode<K,V>* leftRightRotate(SplayTreeNode<K,V>* current);
  63. SplayTreeNode<K,V>* rightRightRotate(SplayTreeNode<K,V>* current);
  64. SplayTreeNode<K,V>* leftLeftRotate(SplayTreeNode<K,V>* current);
  65. };
  66. /* SplayTreeNode is templated class that stores data for each node in the
  67. SplayTree */
  68. template <typename K, typename V>
  69. class SplayTreeNode {
  70. private:
  71. K key;
  72. V value;
  73. /*using parent implementation vs record implementation (ask me if
  74. if you're unsure what this means */
  75. SplayTreeNode<K,V> * left;
  76. SplayTreeNode<K,V> * right;
  77. SplayTreeNode();
  78. SplayTreeNode(K k, V v);
  79. int getHeight();
  80. //so SplayTree can directly access private aspects of this class
  81. friend class SplayTree<K,V>;
  82. };
  83. #include "SplayTree-inl.h"
  84. #include "SplayTree-private-inl.h"
  85. #endif