1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
public class AVLTree<E> extends BinarySearchTree<E> implements Tree<E>, BinaryTreeInfo { public AVLTree() { this(null); }
public AVLTree(Comparator<E> comparator) { super(comparator); }
@Override protected void afterAdd(Node<E> node) { while ((node = node.parent) != null) { if (isBalanced(node)) { updateHeight(node); } else { rebalance(node); break; } } }
@Override protected void afterRemove(Node<E> node) { while ((node = node.parent) != null) { if (isBalanced(node)) { updateHeight(node); } else { rebalance(node); } } }
@Override protected Node<E> createNode(E element, Node<E> parent) { return new AVLNode<>(element, parent); }
@SuppressWarnings("unused") private void rebalance2(Node<E> grand) { Node<E> parent = ((AVLNode<E>)grand).tallerChild(); Node<E> node = ((AVLNode<E>)parent).tallerChild(); if (parent.isLeftChild()) { if (node.isLeftChild()) { rotateRight(grand); } else { rotateLeft(parent); rotateRight(grand); } } else { if (node.isLeftChild()) { rotateRight(parent); rotateLeft(grand); } else { rotateLeft(grand); } } }
private void rebalance(Node<E> grand) { Node<E> parent = ((AVLNode<E>)grand).tallerChild(); Node<E> node = ((AVLNode<E>)parent).tallerChild(); if (parent.isLeftChild()) { if (node.isLeftChild()) { rotate(grand, node, node.right, parent, parent.right, grand); } else { rotate(grand, parent, node.left, node, node.right, grand); } } else { if (node.isLeftChild()) { rotate(grand, grand, node.left, node, node.right, parent); } else { rotate(grand, grand, parent.left, parent, node.left, node); } } }
private void rotate( Node<E> r, // 子树的根节点 Node<E> b, Node<E> c, Node<E> d, Node<E> e, Node<E> f) { d.parent = r.parent; if (r.isLeftChild()) { r.parent.left = d; } else if (r.isRightChild()) { r.parent.right = d; } else { root = d; }
b.right = c; if (c != null) { c.parent = b; } updateHeight(b);
f.left = e; if (e != null) { e.parent = f; } updateHeight(f);
d.left = b; d.right = f; b.parent = d; f.parent = d; updateHeight(d); }
private void rotateLeft(Node<E> grand) { Node<E> parent = grand.right; Node<E> child = parent.left; grand.right = child; parent.left = grand; afterRotate(grand, parent, child); }
private void rotateRight(Node<E> grand) { Node<E> parent = grand.left; Node<E> child = parent.right; grand.left = child; parent.right = grand; afterRotate(grand, parent, child); }
private void afterRotate(Node<E> grand, Node<E> parent, Node<E> child) { parent.parent = grand.parent; if (grand.isLeftChild()) { grand.parent.left = parent; } else if (grand.isRightChild()) { grand.parent.right = parent; } else { root = parent; }
if (child != null) { child.parent = grand; }
grand.parent = parent;
updateHeight(grand); updateHeight(parent); }
private boolean isBalanced(Node<E> node) { return Math.abs(((AVLNode<E>)node).balanceFactor()) <= 1; }
private void updateHeight(Node<E> node) { ((AVLNode<E>)node).updateHeight(); }
private static class AVLNode<E> extends Node<E> { int height = 1;
public AVLNode(E element, Node<E> parent) { super(element, parent); }
public int balanceFactor() { int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height; int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height; return leftHeight - rightHeight; }
public void updateHeight() { int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height; int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height; height = 1 + Math.max(leftHeight, rightHeight); }
public Node<E> tallerChild() { int leftHeight = left == null ? 0 : ((AVLNode<E>)left).height; int rightHeight = right == null ? 0 : ((AVLNode<E>)right).height; if (leftHeight > rightHeight) return left; if (leftHeight < rightHeight) return right; return isLeftChild() ? left : right; }
@Override public String toString() { String parentString = "null"; if (parent != null) { parentString = parent.element.toString(); } return element + "_p(" + parentString + ")_h(" + height + ")"; } } }
|