AVL tree is another balanced binary search tree. Named after their inventors, Adelson-Velskii and Landis, they were the first dynamically balanced trees to be proposed. Like red-black trees, they are not perfectly balanced, but pairs of sub-trees differ in height by at most 1, maintaining an O(logn) search time. Addition and deletion operations also take O(logn) time.
前言 AVL树是最先发明的自平衡二叉查找树。在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树。增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。
平衡二叉树 平衡二叉树介绍:
1)平衡二叉树也叫平衡二叉搜索树(Self-balancing binary search tree)又被称为AVL树, 可以保证查询效率较高。 2)具有以下特点:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。平衡二叉树的常用实现方法有红黑树、AVL、替罪羊树、Treap、伸展树等。
平衡二叉树旋转图解:
平衡二叉树代码:
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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 public class AVLTreeDemo { public static void main (String[] args) { int [] arr = {10 , 11 , 7 , 6 , 8 , 9 }; AVLTree avlTree = new AVLTree(); for (int value : arr) { avlTree.addNode(new Node(value)); } Node avlTreeRoot = avlTree.getRoot(); System.out.println("树的根节点为:" + avlTreeRoot); System.out.println("树的高度为:" + avlTreeRoot.treeHeight()); System.out.println("树的左子树高度为:" + avlTreeRoot.leftTreeHeight()); System.out.println("树的右子树高度为:" + avlTreeRoot.rightTreeHeight()); } } class AVLTree { private Node root; public AVLTree () { } public Node getRoot () { return root; } public Node searchTargetNode (int value) { if (root == null ) { return null ; } else { return root.searchTargetNode(value); } } public Node searchParentNode (int value) { if (root == null ) { return null ; } else { return root.searchParentNode(value); } } public int delRightTreeMin (Node node) { Node target = node; while (target.leftNode != null ) { target = target.leftNode; } deleteNode(target.value); return target.value; } public void deleteNode (int value) { if (root == null ) { System.out.println("空树无法删除" ); return ; } else { Node targetNode = searchTargetNode(value); if (targetNode == null ) { System.out.println("该结点不存在,无法删除" ); return ; } if (root.leftNode == null && root.rightNode == null ) { root = null ; return ; } Node parentNode = searchParentNode(value); if (targetNode.leftNode == null && targetNode.rightNode == null ) { if (parentNode.leftNode != null && parentNode.leftNode.value == value) { parentNode.leftNode = null ; } else if (parentNode.rightNode != null && parentNode.rightNode.value == value) { parentNode.rightNode = null ; } } else if (targetNode.leftNode != null && targetNode.rightNode != null ) { int minVal = delRightTreeMin(targetNode.rightNode); targetNode.value = minVal; } else { if (targetNode.leftNode != null ) { if (parentNode != null ) { if (parentNode.leftNode.value == value) { parentNode.leftNode = targetNode.leftNode; } else { parentNode.rightNode = targetNode.leftNode; } } else { root = targetNode.leftNode; } } else { if (parentNode != null ) { if (parentNode.leftNode.value == value) { parentNode.leftNode = targetNode.rightNode; } else { parentNode.leftNode = targetNode.rightNode; } } else { root = targetNode.rightNode; } } } } } public void addNode (Node node) { if (node == null ) { return ; } if (root == null ) { root = node; } else { root.addNode(node); } } public void midOrder () { if (root == null ) { System.out.println("空树无法遍历" ); return ; } root.midOrder(); } } class Node { int value; Node leftNode; Node rightNode; public Node (int value) { this .value = value; } public int rightTreeHeight () { if (rightNode == null ) { return 0 ; } return rightNode.treeHeight(); } public int leftTreeHeight () { if (leftNode == null ) { return 0 ; } return leftNode.treeHeight(); } public int treeHeight () { return Math.max(leftNode == null ? 0 : leftNode.treeHeight(), rightNode == null ? 0 : rightNode.treeHeight()) + 1 ; } private void leftRotate () { Node newNode = new Node(value); newNode.leftNode = leftNode; newNode.rightNode = rightNode.leftNode; value = rightNode.value; rightNode = rightNode.rightNode; leftNode = newNode; } private void rightRotate () { Node newNode = new Node(value); newNode.rightNode = rightNode; newNode.leftNode = leftNode.rightNode; value = leftNode.value; leftNode = leftNode.leftNode; rightNode = newNode; } public Node searchParentNode (int value) { if ((this .leftNode != null && this .leftNode.value == value) || (this .rightNode != null && this .rightNode.value == value)) { return this ; } else { if (value > this .value && this .rightNode != null ) { return this .rightNode.searchParentNode(value); } else if (value < this .value && this .leftNode != null ) { return this .leftNode.searchParentNode(value); } else { return null ; } } } public Node searchTargetNode (int value) { if (this .value == value) { return this ; } else if (this .value > value) { if (this .leftNode == null ) { return null ; } else { return this .leftNode.searchTargetNode(value); } } else { if (this .rightNode == null ) { return null ; } else { return this .rightNode.searchTargetNode(value); } } } public void addNode (Node node) { if (this .value > node.value) { if (this .leftNode == null ) { this .leftNode = node; } else { this .leftNode.addNode(node); } } else { if (this .rightNode == null ) { this .rightNode = node; } else { this .rightNode.addNode(node); } } if (rightTreeHeight() - leftTreeHeight() > 1 ) { if (rightNode != null && rightNode.leftTreeHeight() > rightNode.rightTreeHeight()) { rightNode.rightRotate(); leftRotate(); } else { leftRotate(); } return ; } if (leftTreeHeight() - rightTreeHeight() > 1 ) { if (leftNode != null && leftNode.rightTreeHeight() > leftNode.leftTreeHeight()) { leftNode.leftRotate(); rightRotate(); } else { rightRotate(); } } } public void midOrder () { if (this .leftNode != null ) { this .leftNode.midOrder(); } System.out.println(this ); if (this .rightNode != null ) { this .rightNode.midOrder(); } } @Override public String toString () { return "Node{" + "value=" + value + '}' ; } }
延伸 AVL树的Java实现 详细图文——AVL树 韩顺平数据结构和算法 AVL树,怎么维持平衡性? Data Structure and Algorithms - AVL Trees
<
Data Structure Graph
Data Structure Binary Sort Tree
>