Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. public void RightRotate(Node wez) {
  2. Node y=wez.getLeft();
  3. wez.setLeft(y.getRight());
  4. if(y.getRight()!=null) {
  5. y.getRight().setParent(wez);
  6. }
  7. y.setParent(wez.getParent());
  8. if(wez.getParent()==null) {
  9. root=y;
  10. }
  11. else {
  12. if(wez==wez.getParent().getRight()) {
  13. wez.getParent().setRight(y);
  14. }
  15. else {
  16. wez.getParent().setLeft(y);
  17. }
  18. }
  19. y.setRight(wez);
  20. wez.setParent(y);
  21. }
  22. public void LeftRotate(Node wez) {
  23. Node y=wez.getRight();
  24. wez.setRight(y.getLeft());
  25. if(y.getLeft()!=null) {
  26. y.getLeft().setParent(wez);
  27. }
  28. y.setParent(wez.getParent());
  29. if(wez.getParent()==null) {
  30. root=y;
  31. }
  32. else {
  33. if(wez==wez.getParent().getLeft()) {
  34. wez.getParent().setLeft(y);
  35. }
  36. else {
  37. wez.getParent().setRight(y);
  38. }
  39. }
  40. y.setLeft(wez);
  41. wez.setParent(y);
  42.  
  43.  
  44. }
  45. public void insert2(int key)throws DuplicateElementException{
  46.  
  47.  
  48. insert(key);
  49.  
  50.  
  51.  
  52. Node x=find(key);
  53. while(x!=root&&x.getParent().getColor()=="R") {
  54. if(x.getParent()==x.getParent().getParent().getLeft()) {
  55. Node y=x.getParent().getParent();
  56. if(y.getColor()=="R") {
  57. x.getParent().setColor("B");
  58. y.setColor("B");
  59. x.getParent().getParent().setColor("R");
  60. x=x.getParent().getParent();
  61. }
  62. else{
  63. if(x==x.getParent().getRight()) {
  64. x=x.getParent();
  65. LeftRotate(x);
  66. }
  67. x.getParent().setColor("B");
  68. x.getParent().getParent().setColor("R");
  69. RightRotate(x.getParent().getParent());
  70.  
  71. }
  72. }
  73. else {
  74. Node y=x.getParent().getParent();
  75. if(y.getColor()=="R") {
  76. x.getParent().setColor("B");
  77. y.setColor("B");
  78. x.getParent().getParent().setColor("R");
  79. x=x.getParent().getParent();
  80. }
  81. else{
  82. if(x==x.getParent().getLeft()) {
  83. x=x.getParent();
  84. RightRotate(x);
  85. }
  86. x.getParent().setColor("B");
  87. x.getParent().getParent().setColor("R");
  88. LeftRotate(x.getParent().getParent());
  89. }
  90.  
  91. }
  92. }
  93.  
  94. root.setColor("B");
  95. }
  96. public void rec(Node n) {
  97. if(n!=null) {
  98. if(n!=root&&n.getParent().getColor()=="R") {
  99. n.setColor("B");
  100. }
  101. rec(n.getLeft());
  102. rec(n.getRight());
  103. }
  104. }
  105. public void insert(int key)throws DuplicateElementException{
  106.  
  107. root=insert(root,key);
  108. nodes++;
  109. }
  110.  
  111. private Node insert(Node root, int key)throws DuplicateElementException {
  112.  
  113. if(root==null) {
  114. root=new Node(key);
  115.  
  116. }
  117. else{
  118. if (key < root.getKey()) {
  119.  
  120.  
  121. Node lchild = insert(root.getLeft(), key);
  122. root.setLeft(lchild);
  123. lchild.setParent(root);
  124.  
  125.  
  126.  
  127.  
  128. }
  129. else if (key > root.getKey()) {
  130. Node rchild = insert(root.getRight(), key);
  131. root.setRight(rchild);
  132. rchild.setParent(root);
  133.  
  134. }
  135. else
  136.  
  137. throw new DuplicateElementException();
  138. }
  139.  
  140. return root;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement