Advertisement
Guest User

Untitled

a guest
May 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. #include <cassert>
  2.  
  3. class RBTree
  4. {
  5. private:
  6. typedef enum tag_color
  7. {
  8. RED,
  9. BLACK
  10. }color_t;
  11.  
  12. typedef struct tag_node
  13. {
  14. struct tag_node *p;
  15. struct tag_node *left;
  16. struct tag_node *right;
  17. int key;
  18. int data;
  19. color_t color;
  20. }node_t;
  21.  
  22. node_t *root;
  23. node_t *nil;
  24.  
  25. void Right_Rotate(node_t *x);
  26. void Left_Rotate(node_t *x);
  27. node_t *RB_Find_Successor(node_t *z) const;
  28. void RB_Insert_Fixup(node_t *z);
  29. node_t *RB_Find(int key);
  30. void RB_Delete_Fixup(node_t *x);
  31. void RB_Clear(node_t *x);
  32.  
  33.  
  34. public:
  35. RBTree();
  36. void RB_Insert(int key, int data);
  37. bool RB_Delete(int key);
  38. ~RBTree();
  39. };
  40.  
  41.  
  42. RBTree::RBTree()
  43. {
  44. nil = new node_t;
  45. nil->left = 0;
  46. nil->right = 0;
  47. nil->p = 0;
  48. nil->color = BLACK;
  49. nil->key = 0;
  50. nil->data = 0;
  51. root = nil;
  52. }
  53.  
  54. void RBTree::Left_Rotate(node_t *x)
  55. {
  56. node_t *y = x->right;
  57.  
  58. x->right = y->left;
  59.  
  60. if (y->left != nil)
  61. (y->left)->p = x;
  62. y->p = x->p;
  63. if (x->p ==nil)
  64. root = y;
  65. else
  66. if (x == (x->p)->left)
  67. (x->p)->left = y;
  68. else
  69. (x->p)->right = y;
  70.  
  71. y->left = x;
  72. x->p = y;
  73. }
  74.  
  75. void RBTree::Right_Rotate(node_t *x)
  76. {
  77. node_t *y = x->left;
  78.  
  79. x->left = y->right;
  80.  
  81. if (y->right != nil)
  82. (y->right)->p = x;
  83. y->p = x->p;
  84. if (x->p == nil)
  85. root = y;
  86. else
  87. if (x == (x->p)->left)
  88. (x->p)->left = y;
  89. else
  90. (x->p)->right = y;
  91.  
  92. y->right = x;
  93. x->p = y;
  94. }
  95.  
  96. void RBTree::RB_Insert_Fixup(node_t *z)
  97. {
  98. node_t *y;
  99.  
  100. while (z->p->color == RED)
  101. if (z->p == z->p->p->left)
  102. {
  103. y = z->p->p->right;
  104. if (y->color == RED)
  105. {
  106. z->p->color = BLACK;
  107. y->color = BLACK;
  108. z->p->p->color = RED;
  109. z = z->p->p;
  110. }
  111. else
  112. {
  113. if (z == z->p->right)
  114. {
  115. z = z->p;
  116. Left_Rotate(z);
  117. }
  118. z->p->color = BLACK;
  119. z->p->p->color = RED;
  120. Right_Rotate(z->p->p);
  121. }
  122. }
  123. else
  124. {
  125. y = z->p->p->left;
  126. if (y->color == RED)
  127. {
  128. z->p->color = BLACK;
  129. y->color = BLACK;
  130. z->p->p->color = RED;
  131. z = z->p->p;
  132. }
  133. else
  134. {
  135. if (z == z->p->left)
  136. {
  137. z = z->p;
  138. Right_Rotate(z);
  139. }
  140. z->p->color = BLACK;
  141. z->p->p->color = RED;
  142. Left_Rotate(z->p->p);
  143. }
  144. }
  145.  
  146. root->color = BLACK;
  147. }
  148.  
  149. void RBTree::RB_Insert(int key, int data)
  150. {
  151. node_t *x, *y, *z = new node_t;
  152.  
  153. z->data = data;
  154. z->key = key;
  155.  
  156. if (root == nil)
  157. {
  158. root = new node_t;
  159. root->data = data;
  160. root->key = key;
  161. root->color = BLACK;
  162. root->left = nil;
  163. root->right = nil;
  164. root->p = nil;
  165.  
  166. return;
  167. }
  168.  
  169. y = nil;
  170. x = root;
  171. while (x != nil)
  172. {
  173. y = x;
  174. if (z->key <= x->key)
  175. x = x->left;
  176. else
  177. x = x->right;
  178. }
  179. z->p = y;
  180. if (y == nil)
  181. root = z;
  182. else
  183. if (z->key <= y->key)
  184. y->left = z;
  185. else
  186. y->right = z;
  187.  
  188. z->left = nil;
  189. z->right = nil;
  190. z->color = RED;
  191. RB_Insert_Fixup(z);
  192. }
  193.  
  194. /*void *RBTree::RB_Find(int key)
  195. {
  196. node
  197. }*/
  198.  
  199. RBTree::node_t *RBTree::RB_Find_Successor(node_t *z) const
  200. {
  201. node_t *y;
  202.  
  203. if (z->right != nil)
  204. {
  205. z = z->right;
  206. while (z->left != nil)
  207. z = z->left;
  208.  
  209. return z;
  210. }
  211. y = z->p;
  212. while (y != nil && z== y->right)
  213. {
  214. z = y;
  215. y = y->p;
  216. }
  217.  
  218. return y;
  219. }
  220.  
  221. bool RBTree::RB_Delete(int key)
  222. {
  223. node_t *x, *y, *z = root;
  224. bool fixup_flag = false;
  225.  
  226. do
  227. {
  228. if (z->key == key)
  229. break;
  230. if (key < z->key)
  231. z = z->left;
  232. else
  233. z = z->right;
  234. } while (z != nil);
  235.  
  236. if (z == nil)
  237. return false;
  238. else
  239. {
  240. if (z->left == nil || z->right == nil)
  241. y = z;
  242. else
  243. y = RB_Find_Successor(z);
  244. }
  245. if (y->left != nil)
  246. x = y->left;
  247. else
  248. x = y->right;
  249. x->p = y->p;
  250. if (y->p == nil)
  251. root = x;
  252. else
  253. if (y == y->p->left)
  254. y->p->left = x;
  255. else
  256. y->p->right = x;
  257. if (y != z)
  258. {
  259. z->key = y->key;
  260. z->data = y->data;
  261. }
  262. if (y->color == BLACK)
  263. fixup_flag = true;
  264.  
  265. delete y;
  266.  
  267. if (fixup_flag == true)
  268. RB_Delete_Fixup(x);
  269.  
  270. return true;
  271. }
  272.  
  273. void RBTree::RB_Delete_Fixup(node_t *x)
  274. {
  275. node_t *w;
  276.  
  277. while (x != root && x->color == BLACK)
  278. if (x == x->p->left)
  279. {
  280. w = x->p->right;
  281. if (w->color == RED)
  282. {
  283. w->color = BLACK;
  284. x->p->color = RED;
  285. Left_Rotate(x->p);
  286. //w = x->p->right;
  287. }
  288. if (w->left->color == BLACK && w->right->color == BLACK)
  289. {
  290. w->color = RED;
  291. x = x->p;
  292. }
  293. else
  294. {
  295. if (w->right->color == BLACK)
  296. {
  297. w->left->color = BLACK;
  298. w->color = RED;
  299. Right_Rotate(w);
  300. w = x->p->right;
  301. }
  302. w->color = x->p->color;
  303. x->p->color = BLACK;
  304. w->right->color = BLACK;
  305. Left_Rotate(x->p);
  306. x = root;
  307. }
  308. }
  309. else
  310. {
  311. w = x->p->left;
  312. if (w->color == RED)
  313. {
  314. w->color = BLACK;
  315. x->p->color = RED;
  316. Right_Rotate(x->p);
  317. //w = x->p->left;
  318. }
  319. if (w->right->color == BLACK && w->left->color == BLACK)
  320. {
  321. w->color = RED;
  322. x = x->p;
  323. }
  324. else
  325. {
  326. if (w->left->color == BLACK)
  327. {
  328. w->right->color = BLACK;
  329. w->color = RED;
  330. Left_Rotate(w);
  331. w = x->p->left;
  332. }
  333. w->color = x->p->color;
  334. x->p->color = BLACK;
  335. w->left->color = BLACK;
  336. Right_Rotate(x->p);
  337. x = root;
  338. }
  339. }
  340. }
  341.  
  342. void RBTree::RB_Clear(node_t *x)
  343. {
  344. if (x == 0)
  345. return;
  346.  
  347. RB_Clear(x->left);
  348. RB_Clear(x->right);
  349.  
  350. delete x;
  351. }
  352.  
  353. RBTree::~RBTree()
  354. {
  355. RB_Clear(root);
  356. }
  357.  
  358. int main()
  359. {
  360.  
  361. RBTree tree;
  362.  
  363. tree.RB_Insert(1, 1);
  364. tree.RB_Insert(2, 2);
  365. tree.RB_Insert(4, 4);
  366. tree.RB_Insert(5, 5);
  367. tree.RB_Insert(7, 7);
  368. tree.RB_Insert(8, 8);
  369. tree.RB_Insert(11, 11);
  370. tree.RB_Insert(14, 14);
  371. tree.RB_Insert(15, 15);
  372.  
  373. tree.RB_Delete(2);
  374. tree.RB_Delete(7);
  375. tree.RB_Delete(8);
  376. tree.RB_Delete(1);
  377.  
  378. return 0;
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement