|
| 1 | +import { Compare, defaultCompare, ICompareFunction } from '../util'; |
| 2 | +import BinarySearchTree from './binary-search-tree'; |
| 3 | +import { Node } from './models/node'; |
| 4 | + |
| 5 | +enum BalanceFactor { |
| 6 | + UNBALANCED_RIGHT = 1, |
| 7 | + SLIGHTLY_UNBALANCED_RIGHT = 2, |
| 8 | + BALANCED = 3, |
| 9 | + SLIGHTLY_UNBALANCED_LEFT = 4, |
| 10 | + UNBALANCED_LEFT = 5 |
| 11 | +} |
| 12 | + |
| 13 | +export default class AVLTree<T> extends BinarySearchTree<T> { |
| 14 | + |
| 15 | + constructor(protected compareFn: ICompareFunction<T> = defaultCompare) { |
| 16 | + super(compareFn); |
| 17 | + } |
| 18 | + |
| 19 | + private getNodeHeight(node: Node<T>): number { |
| 20 | + if (node == null) { |
| 21 | + return -1; |
| 22 | + } |
| 23 | + return Math.max(this.getNodeHeight(node.left), this.getNodeHeight(node.right)) + 1; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Left left case: rotate right |
| 28 | + * |
| 29 | + * b a |
| 30 | + * / \ / \ |
| 31 | + * a e -> rotationLL(b) -> c b |
| 32 | + * / \ / \ |
| 33 | + * c d d e |
| 34 | + * |
| 35 | + * @param node Node<T> |
| 36 | + */ |
| 37 | + private rotationLL(node: Node<T>) { |
| 38 | + const tmp = node.left; |
| 39 | + node.left = tmp.right; |
| 40 | + tmp.right = node; |
| 41 | + return tmp; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Right right case: rotate left |
| 46 | + * |
| 47 | + * a b |
| 48 | + * / \ / \ |
| 49 | + * c b -> rotationRR(a) -> a e |
| 50 | + * / \ / \ |
| 51 | + * d e c d |
| 52 | + * |
| 53 | + * @param node Node<T> |
| 54 | + */ |
| 55 | + private rotationRR(node: Node<T>) { |
| 56 | + const tmp = node.right; |
| 57 | + node.right = tmp.left; |
| 58 | + tmp.left = node; |
| 59 | + return tmp; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Left right case: rotate left then right |
| 64 | + * @param node Node<T> |
| 65 | + */ |
| 66 | + private rotationLR(node: Node<T>) { |
| 67 | + node.left = this.rotationRR(node.left); |
| 68 | + return this.rotationLL(node); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Right left case: rotate right then left |
| 73 | + * @param node Node<T> |
| 74 | + */ |
| 75 | + private rotationRL(node: Node<T>) { |
| 76 | + node.right = this.rotationLL(node.right); |
| 77 | + return this.rotationRR(node); |
| 78 | + } |
| 79 | + |
| 80 | + private getBalanceFactor(node: Node<T>) { |
| 81 | + const heightDifference = this.getNodeHeight(node.left) - this.getNodeHeight(node.right); |
| 82 | + switch (heightDifference) { |
| 83 | + case -2: |
| 84 | + return BalanceFactor.UNBALANCED_RIGHT; |
| 85 | + case -1: |
| 86 | + return BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT; |
| 87 | + case 1: |
| 88 | + return BalanceFactor.SLIGHTLY_UNBALANCED_LEFT; |
| 89 | + case 2: |
| 90 | + return BalanceFactor.UNBALANCED_LEFT; |
| 91 | + default: |
| 92 | + return BalanceFactor.BALANCED; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + insert(key: T) { |
| 97 | + this.root = this.insertNode(this.root, key); |
| 98 | + } |
| 99 | + |
| 100 | + protected insertNode(node: Node<T>, key: T) { |
| 101 | + if (node == null) { |
| 102 | + return new Node(key); |
| 103 | + } else if (this.compareFn(key, node.key) === Compare.LESS_THAN) { |
| 104 | + node.left = this.insertNode(node.left, key); |
| 105 | + } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) { |
| 106 | + node.right = this.insertNode(node.right, key); |
| 107 | + } else { |
| 108 | + return node; // duplicated key |
| 109 | + } |
| 110 | + |
| 111 | + // verify if tree is balanced |
| 112 | + const balanceState = this.getBalanceFactor(node); |
| 113 | + |
| 114 | + if (balanceState === BalanceFactor.UNBALANCED_LEFT) { |
| 115 | + if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) { |
| 116 | + // Left left case |
| 117 | + node = this.rotationLL(node); |
| 118 | + } else { |
| 119 | + // Left right case |
| 120 | + return this.rotationLR(node); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + if (balanceState === BalanceFactor.UNBALANCED_RIGHT) { |
| 125 | + if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) { |
| 126 | + // Right right case |
| 127 | + node = this.rotationRR(node); |
| 128 | + } else { |
| 129 | + // Right left case |
| 130 | + return this.rotationRL(node); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return node; |
| 135 | + } |
| 136 | + |
| 137 | + protected removeNode(node: Node<T>, key: T) { |
| 138 | + if (node == null) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + |
| 142 | + if (this.compareFn(key, node.key) === Compare.LESS_THAN) { |
| 143 | + // The key to be deleted is in the left sub-tree |
| 144 | + node.left = this.removeNode(node.left, key); |
| 145 | + } else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) { |
| 146 | + // The key to be deleted is in the right sub-tree |
| 147 | + node.right = this.removeNode(node.right, key); |
| 148 | + } else { |
| 149 | + // node is the node to be deleted |
| 150 | + if (node.left == null && node.right == null) { |
| 151 | + node = null; |
| 152 | + } else if (node.left == null && node.right != null) { |
| 153 | + node = node.right; |
| 154 | + } else if (node.left != null && node.right == null) { |
| 155 | + node = node.left; |
| 156 | + } else { |
| 157 | + // node has 2 children, get the in-order successor |
| 158 | + const inOrderSuccessor = this.minNode(node.right); |
| 159 | + node.key = inOrderSuccessor.key; |
| 160 | + node.right = this.removeNode(node.right, inOrderSuccessor.key); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + if (node == null) { |
| 165 | + return node; |
| 166 | + } |
| 167 | + |
| 168 | + // verify if tree is balanced |
| 169 | + const balanceState = this.getBalanceFactor(node); |
| 170 | + |
| 171 | + if (balanceState === BalanceFactor.UNBALANCED_LEFT) { |
| 172 | + // Left left case |
| 173 | + if ( |
| 174 | + this.getBalanceFactor(node.left) === BalanceFactor.BALANCED || |
| 175 | + this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT |
| 176 | + ) { |
| 177 | + return this.rotationLL(node); |
| 178 | + } |
| 179 | + // Left right case |
| 180 | + if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) { |
| 181 | + return this.rotationLR(node.left); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if (balanceState === BalanceFactor.UNBALANCED_RIGHT) { |
| 186 | + // Right right case |
| 187 | + if ( |
| 188 | + this.getBalanceFactor(node.right) === BalanceFactor.BALANCED || |
| 189 | + this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT |
| 190 | + ) { |
| 191 | + return this.rotationRR(node); |
| 192 | + } |
| 193 | + // Right left case |
| 194 | + if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) { |
| 195 | + return this.rotationRL(node.right); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + return node; |
| 200 | + } |
| 201 | +} |
0 commit comments