|
| 1 | +package Trees; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class AVL { |
| 6 | + |
| 7 | + private class AVLNode { |
| 8 | + |
| 9 | + int val; |
| 10 | + int height; |
| 11 | + AVLNode left, right; |
| 12 | + |
| 13 | + public AVLNode(int val) { |
| 14 | + this.val = val; |
| 15 | + this.height = 1; |
| 16 | + } |
| 17 | + |
| 18 | + } |
| 19 | + private AVLNode root; |
| 20 | + |
| 21 | + public void add(int val) { |
| 22 | + root = add(val, root); |
| 23 | + } |
| 24 | + |
| 25 | + private int getHeight(AVLNode node) { |
| 26 | + return node == null ? 0 : node.height; |
| 27 | + } |
| 28 | + |
| 29 | + private int getBalanceFactor(AVLNode node) { |
| 30 | + if (node == null) { |
| 31 | + return 0; |
| 32 | + } |
| 33 | + return getHeight(node.left) - getHeight(node.left); |
| 34 | + } |
| 35 | + |
| 36 | + private AVLNode add(int val, AVLNode root) { |
| 37 | + if (root == null) { |
| 38 | + return new AVLNode(val); |
| 39 | + } else if (root.val > val) { |
| 40 | + root.left = add(val, root.left); |
| 41 | + } else if (root.val < val) { |
| 42 | + root.right = add(val, root.right); |
| 43 | + } else { |
| 44 | + return root; |
| 45 | + } |
| 46 | + root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right)); |
| 47 | + int balanceFactor = getBalanceFactor(root); |
| 48 | + // Left Left Rotation |
| 49 | + if (balanceFactor > 1 && root.left.val > val) { |
| 50 | + root = leftRotate(root); |
| 51 | + } |
| 52 | + // Right Right Rotation |
| 53 | + if (balanceFactor < -1 && root.right.val < val) { |
| 54 | + root = rightRotate(root); |
| 55 | + } |
| 56 | + // Left Right Rotation |
| 57 | + if (balanceFactor > 1 && root.left.val < val) { |
| 58 | + root.left = leftRotate(root.left); |
| 59 | + root = rightRotate(root); |
| 60 | + } |
| 61 | + // Right Left Rotation |
| 62 | + if (balanceFactor < -1 && root.right.val > val) { |
| 63 | + root.right = rightRotate(root.right); |
| 64 | + root = leftRotate(root); |
| 65 | + } |
| 66 | + return root; |
| 67 | + } |
| 68 | + |
| 69 | + private AVLNode leftRotate(AVLNode node) { |
| 70 | + AVLNode temp = node.right; |
| 71 | + AVLNode x = temp.left; |
| 72 | + temp.left = node; |
| 73 | + node.right = x; |
| 74 | + temp.height = 1 + Math.max(getHeight(temp.left), getHeight(temp.right)); |
| 75 | + node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right)); |
| 76 | + return temp; |
| 77 | + } |
| 78 | + |
| 79 | + private AVLNode rightRotate(AVLNode node) { |
| 80 | + AVLNode temp = node.left; |
| 81 | + AVLNode y = temp.right; |
| 82 | + temp.right = node; |
| 83 | + node.left = y; |
| 84 | + temp.height = 1 + Math.max(getHeight(temp.left), getHeight(temp.right)); |
| 85 | + node.height = 1 + Math.max(getHeight(node.left), getHeight(node.right)); |
| 86 | + return temp; |
| 87 | + } |
| 88 | + |
| 89 | + public List<Object> inOrder() { |
| 90 | + return inOrder(root); |
| 91 | + } |
| 92 | + |
| 93 | + private List<Object> inOrder(AVLNode node) { |
| 94 | + if (node == null) { |
| 95 | + return new ArrayList<>(); |
| 96 | + } |
| 97 | + List<Object> ans = new ArrayList<>(); |
| 98 | + ans.addAll(inOrder(node.left)); |
| 99 | + ans.add(node.val); |
| 100 | + ans.addAll(inOrder(node.right)); |
| 101 | + return ans; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String toString() { |
| 106 | + return inOrder().toString(); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments