Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. #include <iostream>
  2. #include "splay.h"
  3.  
  4. int main(){
  5. auto i = { 7, 8, 19, 10, 15, 9, 14, 12, 11, 23};
  6.  
  7. splay<int>* spl = new splay<int>;
  8. for (auto const& e : i)
  9. spl->insert(e);
  10.  
  11. std::cout<<"Splay Treen"<<spl->print()<<std::endl;
  12. auto number = 10;
  13. if (spl->find(number))
  14. number = 14;
  15. else
  16. std::cout<<"not foundn";
  17.  
  18. std::cout<<"removing "<<number<<std::endl;
  19. spl->remove(number);
  20. std::cout<<spl->print()<<std::endl;
  21.  
  22. return 0;
  23. }
  24.  
  25. #ifndef SPLAY_H
  26. #define SPLAY_H
  27. #include <memory>
  28.  
  29. template <class K, class V>
  30. class node{
  31. public:
  32. node(K const& key,V const& value):
  33. key_ (key), value_ (value)
  34. { left = right = nullptr; }
  35. std::shared_ptr< node<K,V> > left, right;
  36. std::weak_ptr< node<K,V> > parent;
  37. V value()
  38. {return value_;}
  39. K key()
  40. {return key_;}
  41. void key(const K& key)
  42. {key_ =key;}
  43. private:
  44. K key_;
  45. V value_;
  46. };
  47.  
  48.  
  49. template <class T>
  50. class splay
  51. {
  52. public:
  53. typedef node<int,T> node;
  54. splay()
  55. { null_=nullptr; };
  56.  
  57. void insert (const T& value)
  58. { insertNode_ (head_, null_, value); };
  59. void remove (const T& value)
  60. { markNode_ (head_, value); };
  61. bool find (const T& value)
  62. { return searchNode_ (head_, value); };
  63. std::string print ()
  64. { return print_inOrder_ (head_, ""); };
  65.  
  66. private:
  67. std::shared_ptr<node> head_;
  68. std::shared_ptr<node> null_;
  69.  
  70. void insertNode_ (std::shared_ptr<node>& current, //recursive insert
  71. const std::shared_ptr<node>& parent,const T& value)
  72. {
  73. if (current == nullptr){
  74. current = std::make_shared<node> (0, value);
  75. current->parent = parent;
  76. if (current->parent.lock()!= null_)
  77. if (current != head_)
  78. splayNode(current);
  79. }
  80. else if (current->value() < value)
  81. insertNode_ (current->right, current, value);
  82. else if (current->value() > value)
  83. insertNode_ (current->left , current, value);
  84. }
  85.  
  86. void markNode_ (const std::shared_ptr<node>& current,const T& value)
  87. {//recursively finds the value, calls markRemove to remove the node
  88. if (current != nullptr) {
  89. if (current->value() < value)
  90. markNode_ (current->right, value);
  91. else if (current->value() > value)
  92. markNode_ (current->left , value);
  93. else
  94. removeNode_ (current);
  95. }
  96. }
  97.  
  98. void removeNode_ (std::shared_ptr<node> current) //deletes node
  99. {
  100. if (current->right == nullptr) {
  101. if (current->left != nullptr)
  102. current->left->parent = current->parent;
  103. current = current->left;
  104. }
  105. else if (current->left == nullptr) {
  106. current->left->parent = current->parent;
  107. current = current->right;
  108. }
  109. else { //worst case scenario
  110. std::shared_ptr<node> temp = current->right;
  111. while (temp->left != nullptr)
  112. temp = temp->left;
  113. temp->left = current->left;
  114. if (current->left != nullptr) {
  115. temp->left = current->left;
  116. temp->left->parent = temp;
  117. if (temp->right != nullptr)
  118. temp->right->parent= temp->parent;
  119. temp->parent.lock()->left = temp->right;
  120. temp->right=current->right;
  121. temp->right->parent=temp;
  122. if (current->parent.lock() == null_)
  123. head_ = temp;
  124. else if (current == current->parent.lock()->left)
  125. current->parent.lock()->left = temp;
  126. else
  127. current->parent.lock()->right= temp;
  128. current= std::move(temp);
  129. }
  130. }
  131. }
  132.  
  133. bool searchNode_ (const std::shared_ptr<node>& current, const T& value)
  134. { //searches for node.
  135. if (current != nullptr) {
  136. if (current->value() < value)
  137. return searchNode_ (current->right, value);
  138. else if (current->value() > value)
  139. return searchNode_ (current->left , value);
  140. else {
  141. splayNode(current);
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147.  
  148. void splayNode (const std::shared_ptr<node> current) {
  149. while (current != head_){
  150. if (head_ == current->parent.lock()) {
  151. if (head_->left == current)
  152. RR(current);
  153. else
  154. LR(current);
  155. }
  156. else {
  157. if (current->parent.lock()->parent.lock()->left ==
  158. current->parent.lock()) {
  159. if (current->parent.lock()->left == current) {
  160. RR(current->parent.lock());
  161. RR(current);
  162. }
  163. else {
  164. LR(current);
  165. RR(current);
  166. }
  167. }
  168. else {
  169. if (current->parent.lock()->right == current) {
  170. LR(current->parent.lock());
  171. LR(current);
  172. }
  173. else {
  174. RR(current);
  175. LR(current);
  176. }
  177. }
  178. }
  179. }
  180. }
  181.  
  182. void LR(const std::shared_ptr<node> current)
  183. {
  184. current->parent.lock()->right = current->left;
  185. if (current->parent.lock()->right != nullptr)
  186. current->parent.lock()->right->parent = current->parent.lock();
  187. current->left = current->parent.lock();
  188. current->parent = current->left->parent;
  189. if (current->left->parent.lock() == null_)
  190. head_ = current;
  191. else if (current->left == current->left->parent.lock()->left)
  192. current->left->parent.lock()->left = current;
  193. else
  194. current->left->parent.lock()->right= current;
  195. current->left->parent = current;
  196. };
  197.  
  198. void RR(const std::shared_ptr<node> current)
  199. {
  200. current->parent.lock()->left = current->right;
  201. if (current->parent.lock()->left != nullptr)
  202. current->parent.lock()->left->parent = current->parent.lock();
  203. current->right = current->parent.lock();
  204. current->parent = current->right->parent;
  205. if (current->right->parent.lock() == null_)
  206. head_ = current;
  207. else if (current->right == current->right->parent.lock()->left)
  208. current->right->parent.lock()->left = current;
  209. else
  210. current->right->parent.lock()->right= current;
  211. current->right->parent = current;
  212. };
  213.  
  214. std::string print_inOrder_ (const std::shared_ptr<node>& current,
  215. std::string print)
  216. {
  217. if (current != nullptr) {
  218. print+= "["+ std::to_string(current->value())+ "]";
  219. print = print_inOrder_ (current->left, print);
  220. print = print_inOrder_ (current->right, print);
  221. }
  222. return print;
  223. }
  224. };
  225.  
  226. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement