Download AVL tree

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CO4301 – Advanced Games
Development
Week 11
AVL Trees
Gareth Bellaby
1
AVL tree
•
Georgy Adelson-Velsky and Evgenii Landis,
who published it in their 1962 paper "An
algorithm for the organization of information"
2
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
3
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.
4
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
5
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 “searchand-repair” strategy.
6
Comparison to Red-Black
•
•
•
•
O(log n) time for the main set of operations.
AVL tree is more strictly balanced than a RedBlack 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.
7
Example
•
Insert 60, 40, 70, 20, 50, 80
8
Example
•
If 90 was inserted then the height-balance
property will be broken and the tree will need to
be re-balanced
9
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
10
Insert right of right
11
Insert right of right
•
Single left rotation
12
Insert left of left
A
B
C
13
Insert left of left
A
B
B
C
A
C
•
Single right rotation
14
Insert right of left
A
B
C
15
Insert right of left
•
•
Double rotation: left, followed by right
First single left rotation around central node
A
A
B
C
C
B
16
Insert right of left
A
C
C
B
A
B
•
Right rotation
17
Insert left of right
A
B
C
18
Insert left of right
•
•
Double rotation: right, followed by left
First single right rotation around central node
A
A
B
C
C
B
19
Insert left of right
A
C
C
A
B
B
•
Left rotation
20
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.
21
Deletion
•
Balanced tree
3
40
1
20
2
60
1
80
22
Deletion
•
Delete 20. Unbalanced.
3
40
C
2
60
B
1
80
A
23
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
24
Deletion Left-Left
•
Right rotation on C
C
B
B
A
C
A
25
Deletion Left-Right
•
•
Left rotation on B
Right rotation on C
C
C
B
A
A
•
B
Left rotation on B
26
Deletion Left-Right
C
A
A
B
C
B
•
Right rotation on C
27
Deletion Right-Right
•
Left rotation on C
C
B
B
C
A
A
28
Deletion Right-Left
•
•
Right rotation on B
Left rotation on C
C
C
B
A
A
B
•
Right rotation on B
29
Deletion Right-Left
C
A
A
C
B
B
•
Left rotation on C
30
AVL tree
•
•
Nice visualisation:
https://www.cs.usfca.edu/~galles/visualization/
AVLtree.html
31
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
32
Next Week
•
•
•
•
•
•
Computational geometry
Cormen, Chapter 33
Goodrich & Tamassia, Chapter 22
Convex Hulls
Segment intersection
Closest pair of points
33
Related documents