|
| 1 | +package org.alda.structure.tree.bst.bbt; |
| 2 | + |
| 3 | +/** |
| 4 | + * @author bcExpt1123 |
| 5 | + * |
| 6 | + * <h1>BBT: Balanced Binary Tree</h1> |
| 7 | + * |
| 8 | + * <h2>AVL Tree</h2> |
| 9 | + * <p>An <b>AVL Tree</b> is a self-balancing binary search tree where the height difference |
| 10 | + * (balance factor) between the left and right subtrees of any node is at most <b>1</b>.</p> |
| 11 | + * |
| 12 | + * <h2>Balancing Factor</h2> |
| 13 | + * <p>The <b>balance factor</b> of a node is calculated as:</p> |
| 14 | + * <pre> |
| 15 | + * Balance Factor = Height of Right Subtree - Height of Left Subtree |
| 16 | + * </pre> |
| 17 | + * <p>The balance factor must always be in the range <b>[-1, 0, 1]</b> to maintain balance.</p> |
| 18 | + * |
| 19 | + * <h2>Operations</h2> |
| 20 | + * <ul> |
| 21 | + * <li><b>Rotation</b>: Used to restore balance after insertions and deletions. This includes: |
| 22 | + * <ul> |
| 23 | + * <li><b>Single Rotation</b></li> |
| 24 | + * <li><b>Double Rotation</b></li> |
| 25 | + * </ul> |
| 26 | + * </li> |
| 27 | + * <li><b>Insertion</b>: If inserting a node causes an imbalance, rotations are performed to restore balance.</li> |
| 28 | + * <li><b>Deletion</b>: Removing a node may also lead to an imbalance, requiring rotations to maintain the AVL property.</li> |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * <h2>Time Complexity</h2> |
| 32 | + * <p>Due to self-balancing, AVL trees provide efficient operations:</p> |
| 33 | + * <ul> |
| 34 | + * <li><b>Search</b>: O(log n)</li> |
| 35 | + * <li><b>Insertion</b>: O(log n)</li> |
| 36 | + * <li><b>Deletion</b>: O(log n)</li> |
| 37 | + * </ul> |
| 38 | + */ |
| 39 | +public class AVL<T extends Comparable<T>> { |
| 40 | + |
| 41 | + public Node<T> insert(Node<T> root, T key) { |
| 42 | + if (root == null) { |
| 43 | + return new Node<>(key); |
| 44 | + } else if (key.compareTo(root.key) < 0) { |
| 45 | + root.left = insert(root.left, key); |
| 46 | + } else { |
| 47 | + root.right = insert(root.right, key); |
| 48 | + } |
| 49 | + |
| 50 | + root.height = 1 + Math.max(getHeight(root.left), getHeight(root.right)); |
| 51 | + Integer balance = getBalance(root); |
| 52 | + |
| 53 | + if (balance > 1 && key.compareTo(root.left.key) < 0) { |
| 54 | + return rotateRight(root); |
| 55 | + } |
| 56 | + if (balance < -1 && key.compareTo(root.right.key) > 0) { |
| 57 | + return rotateLeft(root); |
| 58 | + } |
| 59 | + if (balance > 1 && key.compareTo(root.left.key) > 0) { |
| 60 | + root.left = rotateLeft(root.left); |
| 61 | + return rotateRight(root); |
| 62 | + } |
| 63 | + if (balance < -1 && key.compareTo(root.right.key) < 0) { |
| 64 | + root.right = rotateRight(root.right); |
| 65 | + return rotateLeft(root); |
| 66 | + } |
| 67 | + return root; |
| 68 | + } |
| 69 | + |
| 70 | + public Integer getHeight(Node<T> root) { |
| 71 | + if(root == null) return 0; |
| 72 | + return root.height; |
| 73 | + } |
| 74 | + |
| 75 | + public Integer getBalance(Node<T> root) { |
| 76 | + if(root == null) return 0; |
| 77 | + return getHeight(root.left) + getHeight(root.right); |
| 78 | + } |
| 79 | + |
| 80 | + public Node<T> rotateRight(Node<T> z) { |
| 81 | + Node<T> y = z.left; |
| 82 | + Node<T> T = y.right; |
| 83 | + y.right = z; |
| 84 | + z.left = T; |
| 85 | + z.height = 1 + Math.max(getHeight(z.left), getHeight(z.right)); |
| 86 | + y.height = 1 + Math.max(getHeight(y.left), getHeight(y.right)); |
| 87 | + return y; |
| 88 | + } |
| 89 | + |
| 90 | + public Node<T> rotateLeft(Node<T> z) { |
| 91 | + Node<T> y = z.right; |
| 92 | + Node<T> T = y.left; |
| 93 | + y.left = z; |
| 94 | + z.right = T; |
| 95 | + z.height = 1 + Math.max(getHeight(z.left), getHeight(z.right)); |
| 96 | + y.height = 1 + Math.max(getHeight(y.left), getHeight(y.right)); |
| 97 | + return y; |
| 98 | + } |
| 99 | + |
| 100 | + public static class Node<T> { |
| 101 | + T key; |
| 102 | + Node<T> left; |
| 103 | + Node<T> right; |
| 104 | + Integer height; |
| 105 | + |
| 106 | + public Node(T key){ |
| 107 | + this.key = key; |
| 108 | + left = null; |
| 109 | + right = null; |
| 110 | + height = 1; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | + |
0 commit comments