SplayTree.h 3.4 KB

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