Download presentation
Presentation is loading. Please wait.
Published byAlbert Flowers Modified over 6 years ago
1
CO4301 – Advanced Games Development Week 11 AVL Trees
Gareth Bellaby
2
AVL tree Georgy Adelson-Velsky and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information"
3
Self-balancing tree Another balanced tree.
As with the others, foundation is a Binary Search Tree (BST) A height balanced BST. An example of a Self-balancing tree
4
AVL tree Any subtree of an AVL tree will itself be an AVL tree.
Height of the tree is O(logn). Implemented by maintaining an extra attribute in each node which records the height of the node. Rule to maintain the height-balance property.
5
AVL tree The height of a node v in a tree is the length of the longest path from v to an external node. Height-Balance Property: For every internal node v of T, the heights of the children of v differ by at most 1. Note, not every node is within a distance of 1 compared with every other node, but the children of each internal node
6
Operations Insertion and removal operations for AVL trees are like those for a BST tree (or Red-Black tree) Like the Red-Black tree an additional step is needed in order to maintain the property of the tree, in this case the Height-Balance Property. If an insertion operation results in the the Height-Balance Property being broken the balance of the tree is repaired by a “search-and-repair” strategy.
7
Comparison to Red-Black
O(log n) time for the main set of operations. AVL tree is more strictly balanced than a Red-Black Tree. Because of this characteristic they are faster for lookup. However, they are therefore also slower in practice for insertion and deletion because of the need to remain strictly balanced.
8
Example Insert 60, 40, 70, 20, 50, 80
9
Example If 90 was inserted then the height-balance property will be broken and the tree will need to be re-balanced
10
Insert There are 4 cases: Insert into right subtree of a right node
Insert into left subtree of a left node Insert into right subtree of a left node Insert into left subtree of a right node In every case, the tree is rebalanced by a rotation, or by two rotations
11
Insert right of right
12
Insert right of right Single left rotation
13
Insert left of left
14
Insert left of left Single right rotation
15
Insert right of left
16
Insert right of left Double rotation: left, followed by right
First single left rotation around central node
17
Insert right of left Right rotation
18
Insert left of right
19
Insert left of right Double rotation: right, followed by left
First single right rotation around central node
20
Insert left of right Left rotation
21
Deletion Delete the node.
Travel up from the node and find the first unbalanced node. Let C be the first unbalanced node Let B be the larger height child of C Let A be the larger height child of B.
22
Deletion Balanced tree
23
Deletion Delete 20. Unbalanced.
24
Deletion 4 cases Left-Left: B is left child of C and A is left child of B Left-Right: B is left child of C and A is right child of B Right-Right: B is right child of C and A is right child of B Right-Left: B is right child of C and A is left child of B
25
Deletion Left-Left Right rotation on C
26
Deletion Left-Right Left rotation on B Right rotation on C
27
Deletion Left-Right Right rotation on C
28
Deletion Right-Right Left rotation on C
29
Deletion Right-Left Right rotation on B Left rotation on C
30
Deletion Right-Left Left rotation on C
31
AVL tree Nice visualisation:
32
References Leonidas J. Guibas and Robert Sedgewick, "A Dichromatic Framework for Balanced Trees“ Frank M. Carrano, Timothy Henry, Data Abstraction & Problem Solving with C++: Walls and Mirrors Paperback Goodrich, Tamassia & Mount, Data Structures and Algorithms
33
Next Week Computational geometry Cormen, Chapter 33
Goodrich & Tamassia, Chapter 22 Convex Hulls Segment intersection Closest pair of points
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.