SplayTree.h 3.5 KB

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