Advertisement
Guest User

Code Problems :x

a guest
Oct 26th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 0 0
  1. RST.hpp
  2.  
  3. #ifndef RST_HP
  4. #define RST_HPP
  5. #include "BST.hpp"
  6. /*
  7. set expandtab
  8. set shiftwidth=2
  9. set softtabstop=2
  10. gg=G
  11. */
  12. template <typename Data>
  13. class RST : public BST<Data> {
  14. public:
  15. virtual bool insert(const Data& item) {
  16. if (addToTree(this->root, NULL, item))
  17. {
  18. this->isize++;
  19. //perform rotations
  20. return true;
  21. }
  22. else
  23. return false;
  24. }
  25. virtual void RSTNode::leftRotate(RSTNode<Data>*& ptr) {
  26. //this is assuming the right child is of higher priority than the parent
  27. RSTNode<Data>* dummyone = ptr;
  28. RSTNode<Data>* dummytwo = ptr->parent;
  29. RSTNode<Data>* dummythree = ptr->parent->parent;
  30. bool isTheFatherToTheRightOfTheGrandParent = (ptr->parent->parent->right == ptr->parent);
  31. //need to bring up priority and maintain binary tree property
  32. if(dummyone->priority > dummyone->parent->priority) {
  33. if(isTheFatherToTheRightOfTheGrandParent){
  34. ptr->parent->parent->right = dummyone;
  35. ptr->left = dummytwo;
  36. }
  37. if(!isTheFatherToTheRightOfTheGrandParent){
  38. ptr->parent->parent->left = ptr;
  39. ptr->left = dummytwo;
  40. }
  41. }
  42. }
  43. virtual void rightRotate(RSTNode<Data>*& ptr) {
  44. return;
  45. }
  46. virtual bool addToTree(RSTNode<Data>*& ptr, RSTNode<Data>* temp, const Data& num)
  47. {
  48. if (ptr!=NULL)
  49. {
  50. if (num < ptr->data)
  51. {
  52. temp = ptr;
  53. addToTree(ptr->left, temp, num);
  54. }
  55. else if (ptr->data < num)
  56. {
  57. temp = ptr;
  58. addToTree(ptr->right, temp, num);
  59. }
  60. else
  61. return false;
  62. }
  63. else
  64. {
  65. ptr = new RSTNode<Data>(num);
  66. ptr->parent = temp;
  67. return true;
  68. }
  69. }
  70. };
  71. #endif // RST_HPP
  72.  
  73.  
  74. RSTNode.hpp
  75.  
  76. #ifndef RSTNODE_HPP
  77. #define RSTNODE_HPP
  78. #include "BSTNode.hpp"
  79. //for random #
  80. #include <cstdlib>
  81.  
  82. template <typename Data>
  83. class RSTNode : public BSTNode<Data> {
  84. public:
  85. RSTNode(Data const & d) : BSTNode<Data>(d) {
  86. srand((unsigned)time(0));
  87. priority = rand();
  88. left = right = parent = 0;
  89. }
  90. int const priority;
  91. };
  92. #endif // RSTNODE_HPP
  93.  
  94. the above should give enough info but just incase
  95.  
  96. BSTNode.hpp
  97. #ifndef BSTNODE_HPP
  98. #define BSTNODE_HPP
  99. #include <iostream>
  100. #include <iomanip>
  101.  
  102. template<typename Data>
  103. class BSTNode {
  104.  
  105. public:
  106.  
  107. /** Constructor. Initialize a BSTNode with the given Data item,
  108. * no parent, and no children.
  109. */
  110. BSTNode(const Data & d) : data(d) {
  111. left = right = parent = 0;
  112. }
  113.  
  114.  
  115. BSTNode<Data>* left;
  116. BSTNode<Data>* right;
  117. BSTNode<Data>* parent;
  118. Data const data; // the const Data in this node.
  119.  
  120. /** Return the successor of this BSTNode in a BST, or 0 if none.
  121. ** PRECONDITION: this BSTNode is a node in a BST.
  122. ** POSTCONDITION: the BST is unchanged.
  123. ** RETURNS: the BSTNode that is the successor of this BSTNode,
  124. ** or 0 if there is none.
  125. */ // TODO
  126. BSTNode<Data>* successor() {
  127. BSTNode<Data>* cursor;
  128. BSTNode<Data>* par;
  129. cursor = this->right;
  130. par = this->parent;
  131.  
  132.  
  133. if (this->right != NULL)
  134. {
  135. while (cursor->left != NULL) {
  136. cursor = cursor->left;
  137. }
  138. return cursor;
  139. }
  140. if ((this->right == NULL) && (this == par->left))
  141. return this->parent;
  142.  
  143. if ((this->right == NULL) && (this == par->right))
  144. {
  145. do
  146. {
  147. cursor = par;
  148. par = par->parent;
  149. if (par == NULL)
  150. {return cursor;}
  151. }while(cursor != par->left);
  152. return par;
  153. }
  154. if (this->right == NULL && this->parent == NULL)
  155. return NULL;
  156. }
  157. };
  158.  
  159. /** Overload operator<< to print a BSTNode's fields to an ostream. */
  160. template <typename Data>
  161. std::ostream & operator<<(std::ostream& stm, const BSTNode<Data> & n) {
  162. stm << '[';
  163. stm << std::setw(10) << &n; // address of the BSTNode
  164. stm << "; p:" << std::setw(10) << n.parent; // address of its parent
  165. stm << "; l:" << std::setw(10) << n.left; // address of its left child
  166. stm << "; r:" << std::setw(10) << n.right; // address of its right child
  167. stm << "; d:" << n.data; // its data field
  168. stm << ']';
  169. return stm;
  170. }
  171.  
  172. #endif // BSTNODE_HPP
  173.  
  174.  
  175. RST.hpp
  176.  
  177. #ifndef RST_HP
  178. #define RST_HPP
  179. #include "BST.hpp"
  180. /*
  181. set expandtab
  182. set shiftwidth=2
  183. set softtabstop=2
  184. gg=G
  185. */
  186. template <typename Data>
  187. class RST : public BST<Data> {
  188. public:
  189. virtual bool insert(const Data& item) {
  190. if (addToTree(this->root, NULL, item))
  191. {
  192. this->isize++;
  193. //perform rotations
  194. return true;
  195. }
  196. else
  197. return false;
  198. }
  199. virtual void RSTNode::leftRotate(RSTNode<Data>*& ptr) {
  200. //this is assuming the right child is of higher priority than the parent
  201. RSTNode<Data>* dummyone = ptr;
  202. RSTNode<Data>* dummytwo = ptr->parent;
  203. RSTNode<Data>* dummythree = ptr->parent->parent;
  204. bool isTheFatherToTheRightOfTheGrandParent = (ptr->parent->parent->right == ptr->parent);
  205. //need to bring up priority and maintain binary tree property
  206. if(dummyone->priority > dummyone->parent->priority) {
  207. if(isTheFatherToTheRightOfTheGrandParent){
  208. ptr->parent->parent->right = dummyone;
  209. ptr->left = dummytwo;
  210. }
  211. if(!isTheFatherToTheRightOfTheGrandParent){
  212. ptr->parent->parent->left = ptr;
  213. ptr->left = dummytwo;
  214. }
  215. }
  216. }
  217. virtual void rightRotate(RSTNode<Data>*& ptr) {
  218. return;
  219. }
  220. virtual bool addToTree(RSTNode<Data>*& ptr, RSTNode<Data>* temp, const Data& num)
  221. {
  222. if (ptr!=NULL)
  223. {
  224. if (num < ptr->data)
  225. {
  226. temp = ptr;
  227. addToTree(ptr->left, temp, num);
  228. }
  229. else if (ptr->data < num)
  230. {
  231. temp = ptr;
  232. addToTree(ptr->right, temp, num);
  233. }
  234. else
  235. return false;
  236. }
  237. else
  238. {
  239. ptr = new RSTNode<Data>(num);
  240. ptr->parent = temp;
  241. return true;
  242. }
  243. }
  244. };
  245. #endif // RST_HPP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement