Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. package classes;
  2.  
  3. public class SelfBalancingTree {
  4.  
  5. private NodeTwo root;
  6.  
  7. private int svd = -1;
  8. private int size;
  9.  
  10. public SelfBalancingTree(int size) {
  11. this.size = size;
  12. }
  13.  
  14. public int height(NodeTwo N) {
  15. if (N == null)
  16. return 0;
  17.  
  18. return N.height;
  19. }
  20.  
  21. // A utility function to get maximum of two integers
  22. public int max(int a, int b) {
  23. return (a > b) ? a : b;
  24. }
  25.  
  26. // A utility function to right rotate subtree rooted with y
  27. // See the diagram given above.
  28. public NodeTwo rightRotate(NodeTwo y) {
  29. NodeTwo x = y.left;
  30. NodeTwo T2 = x.right;
  31.  
  32. // Perform rotation
  33. x.right = y;
  34. y.left = T2;
  35.  
  36. // Update heights
  37. y.height = max(height(y.left), height(y.right)) + 1;
  38. x.height = max(height(x.left), height(x.right)) + 1;
  39.  
  40. // Return new root
  41. return x;
  42. }
  43.  
  44. // A utility function to left rotate subtree rooted with x
  45. // See the diagram given above.
  46. public NodeTwo leftRotate(NodeTwo x) {
  47. NodeTwo y = x.right;
  48. NodeTwo T2 = y.left;
  49.  
  50. // Perform rotation
  51. y.left = x;
  52. x.right = T2;
  53.  
  54. // Update heights
  55. x.height = max(height(x.left), height(x.right)) + 1;
  56. y.height = max(height(y.left), height(y.right)) + 1;
  57.  
  58. // Return new root
  59. return y;
  60. }
  61.  
  62. // Get Balance factor of NodeTwo N
  63. public int getBalance(NodeTwo N) {
  64. if (N == null)
  65. return 0;
  66.  
  67. return height(N.left) - height(N.right);
  68. }
  69.  
  70. public int insert(int value) {
  71. root = insert(root, value);
  72. if (svd != -1) return svd;
  73. return -1;
  74. }
  75.  
  76. public NodeTwo insert(NodeTwo node, int key) {
  77. if (node == null)
  78. return (new NodeTwo(key));
  79.  
  80. if (key < node.key)
  81. node.left = insert(node.left, key);
  82. else if (key > node.key)
  83. node.right = insert(node.right, key);
  84. else {
  85. node.count += 1;
  86. if (node.count > (size / 2)) svd = node.key;
  87. return node;
  88. }
  89. /* 2. Update height of this ancestor NodeTwo */
  90. node.height = 1 + max(height(node.left),
  91. height(node.right));
  92.  
  93. /* 3. Get the balance factor of this ancestor
  94. NodeTwo to check whether this NodeTwo became
  95. unbalanced */
  96. int balance = getBalance(node);
  97.  
  98. // If this NodeTwo becomes unbalanced, then there
  99. // are 4 cases Left Left Case
  100. if (balance > 1 && key < node.left.key)
  101. return rightRotate(node);
  102.  
  103. // Right Right Case
  104. if (balance < -1 && key > node.right.key)
  105. return leftRotate(node);
  106.  
  107. // Left Right Case
  108. if (balance > 1 && key > node.left.key) {
  109. node.left = leftRotate(node.left);
  110. return rightRotate(node);
  111. }
  112.  
  113. // Right Left Case
  114. if (balance < -1 && key < node.right.key) {
  115. node.right = rightRotate(node.right);
  116. return leftRotate(node);
  117. }
  118.  
  119. /* return the (unchanged) NodeTwo pointer */
  120. return node;
  121. }
  122. }
  123. package classes;
  124.  
  125. public class NodeTwo {
  126.  
  127. public int key, height;
  128. public NodeTwo left, right;
  129. public int count;
  130.  
  131. NodeTwo(int d) {
  132. key = d;
  133. height = 1;
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement