BasicAVL.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. public class BasicAVL
  2. {
  3. private class AVLNode
  4. {
  5. int element;
  6. int height;
  7. AVLNode leftChild;
  8. AVLNode rightChild;
  9. public AVLNode(int element)
  10. {
  11. this.element = element;
  12. this.height = 0;
  13. }
  14. }
  15. private AVLNode root;
  16. public BasicAVL()
  17. {
  18. root = null;
  19. }
  20. public void insert(int item)
  21. {
  22. root = insert(root, item);
  23. }
  24. private AVLNode insert(AVLNode root, int item)
  25. {
  26. if(root == null)
  27. {
  28. return new AVLNode(item);
  29. }
  30. else if(item < root.element)
  31. {
  32. root.leftChild = insert(root.leftChild, item);
  33. if(root.leftChild.height >= height(root.rightChild) + 2)
  34. {
  35. if(item < root.leftChild.element)
  36. {
  37. root = rotateWithLeft(root);
  38. }
  39. else
  40. {
  41. root = doubleRightLeft(root);
  42. }
  43. }
  44. }
  45. else
  46. {
  47. root.rightChild = insert(root.rightChild, item);
  48. if(root.rightChild.height >= height(root.leftChild) + 2)
  49. {
  50. if(item < root.rightChild.element)
  51. {
  52. root = doubleLeftRight(root);
  53. }
  54. else
  55. {
  56. root = rotateWithRight(root);
  57. }
  58. }
  59. }
  60. root.height = Math.max(height(root.leftChild), height(root.rightChild)) + 1;
  61. return root;
  62. }
  63. private AVLNode rotateWithRight(AVLNode oldRoot)
  64. {
  65. AVLNode newRoot = oldRoot.rightChild;
  66. oldRoot.rightChild = newRoot.leftChild;
  67. newRoot.leftChild = oldRoot;
  68. oldRoot.height = 1 + Math.max(height(oldRoot.leftChild), height(oldRoot.rightChild));
  69. newRoot.height = 1 + Math.max(height(newRoot.rightChild), newRoot.leftChild.height);
  70. return newRoot;
  71. }
  72. private AVLNode rotateWithLeft(AVLNode oldRoot)
  73. {
  74. AVLNode newRoot = oldRoot.leftChild;
  75. oldRoot.leftChild = newRoot.rightChild;
  76. newRoot.rightChild = oldRoot;
  77. oldRoot.height = 1 + Math.max(height(oldRoot.leftChild), height(oldRoot.rightChild));
  78. newRoot.height = 1 + Math.max(height(newRoot.leftChild), newRoot.rightChild.height);
  79. return newRoot;
  80. }
  81. private AVLNode doubleLeftRight(AVLNode oldRoot)
  82. {
  83. oldRoot.leftChild = rotateWithRight(oldRoot.leftChild);
  84. return rotateWithLeft(oldRoot);
  85. }
  86. private AVLNode doubleRightLeft(AVLNode oldRoot)
  87. {
  88. oldRoot.rightChild = rotateWithLeft(oldRoot.rightChild);
  89. return rotateWithRight(oldRoot);
  90. }
  91. private int height(AVLNode node)
  92. {
  93. return node == null? -1:node.height;
  94. }
  95. public void print()
  96. {
  97. printHelper(root, "");
  98. }
  99. private void printHelper(AVLNode chkNode, String indent)
  100. {
  101. if (chkNode != null)
  102. {
  103. System.out.println(indent + chkNode.element);
  104. indent += " ";
  105. printHelper(chkNode.leftChild, indent);
  106. printHelper(chkNode.rightChild, indent);
  107. }
  108. }
  109. }