Guest User

Untitled

a guest
Jan 23rd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. package rb_tree;
  2.  
  3. import java.util.Vector;
  4.  
  5. import observer.*;
  6. import tree.*;
  7.  
  8. public class RBTree extends Tree {
  9. public int Rotations = 0;
  10. public int AllRotationsDeletion = 0;
  11. public int MaxRotationsDeletion = 0;
  12. public int AllRotationsInsertion = 0;
  13. public int MaxRotationsInsertion = 0;
  14.  
  15. public RBTree () {
  16. this.Message = new String ("");
  17.  
  18. this.Nil = new RBNode ();
  19. this.Nil.Parent = this.Nil.Left = this.Nil.Right = this.Nil;
  20. this.Root = this.Nil;
  21.  
  22. this.Observers = new Vector<Observer> ();
  23. }
  24.  
  25. public RBNode delete (RBNode z) {
  26. Comparisions = 0;
  27. Rotations = 0;
  28. Deletions++;
  29.  
  30. RBNode y = (compare (z.Left == this.Nil) || compare (z.Right == this.Nil)) ?
  31. z :
  32. (RBNode) this.successor (z),
  33. x = (RBNode) (compare (y.Left != this.Nil) ?
  34. y.Left :
  35. y.Right);
  36.  
  37. x.Parent = y.Parent;
  38.  
  39. if (compare (y.Parent == this.Nil))
  40. this.Root = x;
  41. else if (compare (y == y.Parent.Left))
  42. y.Parent.Left = x;
  43. else
  44. y.Parent.Right = x;
  45.  
  46. if (compare (y != z))
  47. z.Key = y.Key;
  48.  
  49. this.informAll (1, "Delete (T,x), key[x] = "+y.Key);
  50.  
  51. if (y.Color == RBColor.BLACK)
  52. this.deleteFixup (x);
  53.  
  54. this.informAll (0, "RB-Delete (T,x) (RB-Delete-Fixup done), key[x]="+y.Key);
  55.  
  56. AllComparisionsDeletion += Comparisions;
  57. MaxComparisionsDeletion = Comparisions > MaxComparisionsDeletion ? Comparisions : MaxComparisionsDeletion;
  58. AllRotationsDeletion += Rotations;
  59. MaxRotationsDeletion = Rotations > MaxRotationsDeletion ? Rotations : MaxRotationsDeletion;
  60.  
  61. return y;
  62. }
  63.  
  64. private void deleteFixup (RBNode x) {
  65. RBNode w;
  66.  
  67. while (compare (x != this.Root) && compare (x.Color == RBColor.BLACK)) {
  68. if (compare (x == x.Parent.Left)) {
  69. w = (RBNode) x.Parent.Right;
  70. if (compare (w.Color == RBColor.RED)) {
  71. w.colorize (RBColor.BLACK);
  72. ((RBNode) x.Parent).colorize (RBColor.RED);
  73. this.leftRotate (x.Parent);
  74. w = (RBNode) x.Parent.Right;
  75. }
  76. if (compare (((RBNode) w.Left).Color == RBColor.BLACK) && compare (((RBNode) w.Right).Color == RBColor.BLACK)) {
  77. w.colorize (RBColor.RED);
  78. x = (RBNode) x.Parent;
  79. } else {
  80. if (compare (((RBNode) w.Right).Color == RBColor.BLACK)) {
  81. ((RBNode) w.Left).colorize (RBColor.BLACK);
  82. w.colorize (RBColor.RED);
  83. this.rightRotate (w);
  84. w = (RBNode) x.Parent.Right;
  85. }
  86. w.colorize (((RBNode) x.Parent).Color);
  87. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  88. ((RBNode) w.Right).colorize (RBColor.BLACK);
  89. this.leftRotate (x.Parent);
  90. x = (RBNode) this.Root;
  91. }
  92. } else {
  93. w = (RBNode) x.Parent.Left;
  94. if (compare (w.Color == RBColor.RED)) {
  95. w.colorize (RBColor.BLACK);
  96. ((RBNode) x.Parent).colorize (RBColor.RED);
  97. this.rightRotate (x.Parent);
  98. w = (RBNode) x.Parent.Left;
  99. }
  100. if (compare (((RBNode) w.Right).Color == RBColor.BLACK) && compare (((RBNode) w.Left).Color == RBColor.BLACK)) {
  101. w.colorize (RBColor.RED);
  102. x = (RBNode) x.Parent;
  103. } else {
  104. if (compare (((RBNode) w.Left).Color == RBColor.BLACK)) {
  105. ((RBNode) w.Right).colorize (RBColor.BLACK);
  106. w.colorize (RBColor.RED);
  107. this.leftRotate (w);
  108. w = (RBNode) x.Parent.Left;
  109. }
  110. w.colorize (((RBNode) x.Parent).Color);
  111. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  112. ((RBNode) w.Left).colorize (RBColor.BLACK);
  113. this.rightRotate (x.Parent);
  114. x = (RBNode) this.Root;
  115. }
  116. }
  117. }
  118.  
  119. x.colorize (RBColor.BLACK);
  120.  
  121. this.informAll (2, "RB-Delete-Fixup");
  122. }
  123.  
  124. public void insert (RBNode x) {
  125. super.insert ((Node) x);
  126.  
  127. ((RBNode) x).colorize (RBColor.RED);
  128.  
  129. Rotations = 0;
  130. AllComparisionsInsertion -= Comparisions;
  131.  
  132. this.insertFixup (x);
  133.  
  134. this.informAll (0, "RB-Insert (T, x) (Insert, RB-Insert-Fixup done), key[x]="+x.Key);
  135.  
  136. AllComparisionsInsertion += Comparisions;
  137. MaxComparisionsInsertion = Comparisions > MaxComparisionsInsertion ? Comparisions : MaxComparisionsInsertion;
  138. AllRotationsInsertion += Rotations;
  139. MaxRotationsInsertion = Rotations > MaxRotationsInsertion ? Rotations : MaxRotationsInsertion;
  140. }
  141.  
  142. public void insertFixup (RBNode x) {
  143. Node y;
  144.  
  145. while (compare (x != this.Root) && compare (((RBNode) x.Parent).Color == RBColor.RED)) {
  146. if (compare (x.Parent == x.Parent.Parent.Left)) {
  147. y = x.Parent.Parent.Right;
  148. if (compare (((RBNode) y).Color == RBColor.RED)) {
  149. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  150. ((RBNode) y).colorize (RBColor.BLACK);
  151. ((RBNode) x.Parent.Parent).colorize (RBColor.RED);
  152. x = (RBNode) x.Parent.Parent;
  153. } else {
  154. if (compare (x == x.Parent.Right)) {
  155. x = (RBNode) x.Parent;
  156. this.leftRotate (x);
  157. }
  158. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  159. ((RBNode) x.Parent.Parent).colorize (RBColor.RED);
  160. this.rightRotate (x.Parent.Parent);
  161. }
  162. } else {
  163. y = x.Parent.Parent.Left;
  164. if (compare (((RBNode) y).Color == RBColor.RED)) {
  165. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  166. ((RBNode) y).colorize (RBColor.BLACK);
  167. ((RBNode) x.Parent.Parent).colorize (RBColor.RED);
  168. x = (RBNode) x.Parent.Parent;
  169. } else {
  170. if (compare (x == x.Parent.Left)) {
  171. x = (RBNode) x.Parent;
  172. this.rightRotate (x);
  173. }
  174. ((RBNode) x.Parent).colorize (RBColor.BLACK);
  175. ((RBNode) x.Parent.Parent).colorize (RBColor.RED);
  176. this.leftRotate (x.Parent.Parent);
  177. }
  178. }
  179. }
  180.  
  181. ((RBNode) this.Root).colorize (RBColor.BLACK);
  182.  
  183. this.informAll (2, "RB-Insert-Fixup");
  184. }
  185.  
  186. private void leftRotate (Node x) {
  187. if (compare (x.Right == this.Nil))
  188. return;
  189. this.Root.Parent = this.Nil;
  190.  
  191. Node y = x.Right;
  192. x.Right = y.Left;
  193. if (compare (y.Left != this.Nil))
  194. y.Left.Parent = x;
  195. y.Parent = x.Parent;
  196.  
  197. if (compare (x.Parent == this.Nil))
  198. this.Root = y;
  199. else {
  200. if (compare (x == x.Parent.Left))
  201. x.Parent.Left = y;
  202. else
  203. x.Parent.Right = y;
  204. }
  205.  
  206. y.Left = x;
  207. x.Parent = y;
  208.  
  209. this.Rotations++;
  210. this.informAll (2, "Left-Rotate (T, x), key[x]="+x.Key);
  211. }
  212.  
  213. private void rightRotate (Node x) {
  214. if (compare (x.Left == this.Nil))
  215. return;
  216. this.Root.Parent = this.Nil;
  217.  
  218. Node y = x.Left;
  219. x.Left = y.Right;
  220. if (compare (y.Right != this.Nil))
  221. y.Right.Parent = x;
  222. y.Parent = x.Parent;
  223.  
  224.  
  225. if (compare (x.Parent == this.Nil))
  226. this.Root = y;
  227. else {
  228. if (compare (x == x.Parent.Right))
  229. x.Parent.Right = y;
  230. else
  231. x.Parent.Left = y;
  232. }
  233.  
  234. y.Right = x;
  235. x.Parent = y;
  236.  
  237. this.Rotations++;
  238. this.informAll (2, "Right-Rotate (T, x), key[x]="+x.Key);
  239. }
  240. }
Add Comment
Please, Sign In to add comment