lerasah

AVLTree.java

Aug 9th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.59 KB | None | 0 0
  1.     import java.util.Stack;
  2.    
  3.     public class AVLTree<K extends Comparable<K>> {
  4.    
  5.         public static final int LEFT_HIGHER = -1;
  6.         public static final int EQUAL_HEIGHT = 0;
  7.         public static final int RIGHT_HIGHER = 1;
  8.         private AVLNode<K> root;
  9.         private Boolean taller;
  10.    
  11.         public AVLTree() {
  12.    
  13.             root = null;
  14.             taller = true;
  15.         }
  16.    
  17.         public void add(K input) {
  18.    
  19.             root = add(root, input);
  20.         }
  21.    
  22.         private AVLNode<K> add(AVLNode<K> subroot, K input) {
  23.    
  24.             if (subroot == null) {
  25.    
  26.                 subroot = new AVLNode<K>(input);
  27.                 this.taller = true;
  28.             } else if (input.compareTo(subroot.data) == 0) {
  29.    
  30.                 this.taller = false;
  31.             } else if (input.compareTo(subroot.data) < 0) {
  32.    
  33.                 subroot.left = add(subroot.left, input);
  34.                 if (this.taller) {
  35.    
  36.                     if (subroot.balance == LEFT_HIGHER) {
  37.    
  38.                         subroot = leftBalance(subroot);
  39.                         taller = false;
  40.                     } else if (subroot.balance == EQUAL_HEIGHT) {
  41.    
  42.                         subroot.balance = LEFT_HIGHER;
  43.                     } else if (subroot.balance == RIGHT_HIGHER) {
  44.    
  45.                         this.taller = false;
  46.                         subroot.balance = EQUAL_HEIGHT;
  47.                     }
  48.                 }
  49.             } else {
  50.    
  51.                 subroot.right = add(subroot.right, input);
  52.                 if (this.taller) {
  53.    
  54.                     if (subroot.balance == LEFT_HIGHER) {
  55.    
  56.                         taller = false;
  57.                         subroot.balance = EQUAL_HEIGHT;
  58.                     } else if (subroot.balance == EQUAL_HEIGHT) {
  59.    
  60.                         subroot.balance = RIGHT_HIGHER;
  61.                     } else if (subroot.balance == RIGHT_HIGHER) {
  62.    
  63.                         subroot = rightBalance(subroot);
  64.                         taller = false;
  65.                     }
  66.                 }
  67.             }
  68.    
  69.             return subroot;
  70.         }
  71.    
  72.         public AVLNode<K> leftBalance(AVLNode<K> subroot) {
  73.    
  74.             AVLNode<K> leftTree = subroot.left;
  75.             AVLNode<K> subTree = null;
  76.             if (leftTree.balance == LEFT_HIGHER) {
  77.    
  78.                 subroot.balance = EQUAL_HEIGHT;
  79.                 leftTree.balance = EQUAL_HEIGHT;
  80.                 subroot = rotateRight(subroot);
  81.                 this.taller = false;
  82.             }
  83.             if (leftTree.balance == RIGHT_HIGHER) {
  84.    
  85.                 subTree = leftTree.right;
  86.                 subTree.balance = EQUAL_HEIGHT;
  87.                 leftTree = rotateLeft(leftTree);
  88.                 subroot.left = leftTree;
  89.                 subroot = rotateRight(subroot);
  90.    
  91.                 if (subTree.balance == EQUAL_HEIGHT) {
  92.    
  93.                     subroot.balance = EQUAL_HEIGHT;
  94.                     leftTree.balance = EQUAL_HEIGHT;
  95.                 } else if (subTree.balance == LEFT_HIGHER) {
  96.    
  97.                     subroot.balance = EQUAL_HEIGHT;
  98.                     leftTree.balance = LEFT_HIGHER;
  99.                 } else {
  100.    
  101.                     subroot.balance = RIGHT_HIGHER;
  102.                     leftTree.balance = EQUAL_HEIGHT;
  103.                 }
  104.             }
  105.    
  106.             return subroot;
  107.         }
  108.    
  109.         public AVLNode<K> rightBalance(AVLNode<K> subroot) {
  110.    
  111.             AVLNode<K> rightTree = subroot.right;
  112.             AVLNode<K> subTree = null;
  113.             if (rightTree.balance == RIGHT_HIGHER) {
  114.    
  115.                 subroot.balance = EQUAL_HEIGHT;
  116.                 rightTree.balance = EQUAL_HEIGHT;
  117.                 subroot = rotateLeft(subroot);
  118.             }
  119.             if (rightTree.balance == LEFT_HIGHER) {
  120.    
  121.                 subTree = rightTree.left;
  122.                 subTree.balance = EQUAL_HEIGHT;
  123.                 rightTree = rotateRight(rightTree);
  124.                 subroot.right = rightTree;
  125.                 subroot = rotateLeft(subroot);
  126.    
  127.                 if (subTree.balance == EQUAL_HEIGHT) {
  128.    
  129.                     subroot.balance = EQUAL_HEIGHT;
  130.                     rightTree.balance = EQUAL_HEIGHT;
  131.                 } else if (subTree.balance == LEFT_HIGHER) {
  132.    
  133.                     subroot.balance = EQUAL_HEIGHT;
  134.                     rightTree.balance = RIGHT_HIGHER;
  135.                 } else {
  136.    
  137.                     subroot.balance = LEFT_HIGHER;
  138.                     rightTree.balance = EQUAL_HEIGHT;
  139.                 }
  140.             }
  141.    
  142.             return subroot;
  143.         }
  144.    
  145.         private AVLNode<K> rotateLeft(AVLNode<K> subroot) {
  146.    
  147.             AVLNode<K> rightSubTree = subroot.right;
  148.             subroot.right = rightSubTree.left;
  149.             rightSubTree.left = subroot;
  150.             return rightSubTree;
  151.         }
  152.    
  153.         private AVLNode<K> rotateRight(AVLNode<K> subroot) {
  154.    
  155.             AVLNode<K> leftSubTree = subroot.left;
  156.             subroot.left = leftSubTree.right;
  157.             leftSubTree.right = subroot;
  158.             return leftSubTree;
  159.         }
  160.    
  161.         public int getHeight(AVLNode<K> subroot) {
  162.    
  163.             if (subroot == null)
  164.                 return 0;
  165.             if (subroot.left == null && subroot.right == null)
  166.                 return 1;
  167.             int hl = subroot.left != null ? getHeight(subroot.left) : 0;
  168.             int hr = subroot.right != null ? getHeight(subroot.right) : 0;
  169.             return hl > hr ? hl + 1 : hl + 1;
  170.         }
  171.    
  172.         // copy from binary tree
  173.         public void toDot() {
  174.             AVLNode<?> n;
  175.             // create a variable to contain result
  176.             String dot = "graph BinaryTree {\n";
  177.             Stack<AVLNode<?>> stack = new Stack<AVLNode<?>>();
  178.    
  179.             // begin with root
  180.             n = root;
  181.             stack.push(n);
  182.             do {
  183.                 // append current node to result
  184.                 dot += n.toDot();
  185.                 // now if current node has childs and wed add it to queue for
  186.                 // parsing later
  187.                 if (n.left != null) {
  188.                     stack.push(n.left);
  189.                 }
  190.                 if (n.right != null) {
  191.                     stack.push(n.right);
  192.                 }
  193.                 n = stack.pop();
  194.             } while (!stack.empty());
  195.             // tree traversal with NO recursion ! cool huh? instead of the call
  196.             // stack we use a stack.
  197.             // Question for you is, am I using depth 1st or breath 1st traversal?
  198.             // Answer: this is breath 1st because it parse all the child nodes in
  199.             // same level before move to next level
  200.             System.out.print(dot + "\n}");
  201.         }
  202.     }
Advertisement
Add Comment
Please, Sign In to add comment