Guest User

Untitled

a guest
Mar 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package bst;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class BST {
  6.  
  7. protected BSTNode root;
  8. protected int size;
  9.  
  10. public boolean isEmpty() {
  11. return root == null;
  12. }
  13.  
  14. public void insert(int data) {
  15. if (this.isEmpty()) {
  16. this.root = new BSTNode(data);
  17. } else {
  18. this.insert(this.root, data);
  19. }
  20. this.size++;
  21. }
  22.  
  23. private void insert(BSTNode actual, int data) {
  24. if (data >= actual.data) {
  25. if (actual.right == null) {
  26. actual.right = new BSTNode(data);
  27. actual.right.parent = actual;
  28. } else {
  29. this.insert(actual.right, data);
  30. }
  31. } else {
  32. if (actual.left == null) {
  33. actual.left = new BSTNode(data);
  34. actual.left.parent = actual;
  35. } else {
  36. this.insert(actual.left, data);
  37. }
  38. }
  39. }
  40.  
  41. public int[] preOrder() {
  42. int[] array = new int[this.size];
  43. int[] idx = new int[] { 0 };
  44.  
  45. if (!this.isEmpty()) {
  46. this.preOrderSteps(this.root, array, idx);
  47. }
  48.  
  49. return array;
  50. }
  51.  
  52. private void preOrderSteps(BSTNode actual, int[] array, int[] idx) {
  53. array[idx[0]++] = actual.data;
  54.  
  55. if (actual.left != null) {
  56. this.preOrderSteps(actual.left, array, idx);
  57. }
  58.  
  59. if (actual.right != null) {
  60. this.preOrderSteps(actual.right, array, idx);
  61. }
  62. }
  63.  
  64. public void simpleBalance() {
  65. if (this.root.left != null && this.root.right != null) {
  66. System.out.println("balanceada");
  67. } else if (this.root.right != null) {
  68. if (this.root.right.right != null) {
  69. this.leftRotation(this.root);
  70. } else if (this.root.right.left != null) {
  71. this.rightRotation(this.root.right);
  72. this.leftRotation(this.root);
  73. }
  74. } else if (this.root.left != null) {
  75. if (this.root.left.left != null) {
  76. this.rightRotation(this.root);
  77. } else if (this.root.left.right != null) {
  78. this.leftRotation(this.root.left);
  79. this.rightRotation(this.root);
  80. }
  81. }
  82. }
  83.  
  84. private void leftRotation(BSTNode node) {
  85. System.out.println("rot_esq(" + node.data + ")");
  86.  
  87. if (node == this.root) {
  88. this.root = node.right;
  89. node.right.parent = null;
  90. } else {
  91. if (node.parent.right == node) {
  92. node.parent.right = node.right;
  93. } else {
  94. node.parent.left = node.right;
  95. }
  96. node.right.parent = node.parent;
  97. }
  98.  
  99. node.parent = node.right;
  100. node.right = node.parent.left;
  101. node.parent.left = node;
  102.  
  103. if (node.right != null) {
  104. node.right.parent = node;
  105. }
  106.  
  107. System.out.println(this);
  108. }
  109.  
  110. private void rightRotation(BSTNode node) {
  111. System.out.println("rot_dir(" + node.data + ")");
  112.  
  113. if (node == this.root) {
  114. this.root = node.left;
  115. node.left.parent = null;
  116. } else {
  117. if (node.parent.left == node) {
  118. node.parent.left = node.left;
  119. } else {
  120. node.parent.right = node.left;
  121. }
  122. node.left.parent = node.parent;
  123. }
  124.  
  125. node.parent = node.left;
  126. node.left = node.parent.right;
  127. node.parent.right = node;
  128.  
  129. if (node.left != null) {
  130. node.left.parent = node;
  131. }
  132.  
  133. System.out.println(this);
  134. }
  135.  
  136. @Override
  137. public String toString() {
  138. return Arrays.toString(this.preOrder());
  139. }
  140.  
  141. }
Add Comment
Please, Sign In to add comment