cs350: data structures avl trees - github pages · cs350: data structures avl trees • a type of...
TRANSCRIPT
CS350: Data Structures © James Moscola
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATA
LOG
2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
College Catalog2009–2011
!"#$%&'())*+,-.)/.&01234546708.9:;*&<:(#.="#>&1015?26511??@A9/**/")*&<B!&C(>&1015?2D50633
05?3352775?30?EEEF+C:F(A;
!""#$%%&'$#()*$&+$,-$%.$"
'GHI<GJHK&L<MNK'GONJHK&P@JJHGMFIF&'<IJ@QH&'@OK
!<GR%&'@'HGPOJ&N<F&012
YO
RK
CO
LLEGE O
F PENN
SY
LVA
NIA
CO
LLEGE C
ATALO
G 2009–2011
!""#$%&'()*+,--.../ 012$1$"..."34#3$4.56
James MoscolaDepartment of Engineering & Computer ScienceYork College of Pennsylvania
CS350: Data StructuresAVL Trees
CS350: Data Structures
Balanced Search Trees
• Binary search trees are not guaranteed to be balanced given random insertions and deletions
- Inserting a sorted lists of elements into a BST produces the worst case -- O(N)- Performance of an unbalanced tree can degrade as more elements are inserted
• Balanced search tree operations, such as insert, insure that the a tree always remains balanced
- An operation is not complete until it returns the tree to a balanced state
2
Balanced tree
Unbalanced tree
CS350: Data Structures
AVL Trees
• A type of balanced binary search tree
• Named for its discoverers -- Adelson, Velskii, and Landis
• Definition:
3
An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.
CS350: Data Structures
AVL Trees
4
12
4 10
8 16
14
2 60 0
1 0 0
12
3 The height of each node is listed in blue
An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.
-1
CS350: Data Structures
AVL Trees
5
12
4 10
8 16
14
2 60 0
1 0 0
12
3 The height of each node is listed in blue
An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.
-1
height differs by at most 1
CS350: Data Structures
AVL Trees
6
12
4 10
8 16
14
2 60 0
1 0 0
12
3 The height of each node is listed in blue
An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1.
-1
height differs by at most 1
CS350: Data Structures
AVL Trees
7
12
4 10
8 16
14
2 6
1
0
1
-1
0
0
?
?
?
?
?
The binary tree after inserting node 1
What are the node heights?
CS350: Data Structures
AVL Trees
8
12
4 10
8 16
14
2 61 0
2 0 0
13
4
-1
10
The binary tree after inserting node 1
What are the node heights?
CS350: Data Structures
AVL Trees
9
12
4 10
8 16
14
2 61 0
2 0 0
13
4
-1
10
height differs bymore than 1
therefore, node 8 is invalid
CS350: Data Structures
AVL Trees
10
12
4 10
8 16
14
2 61 0
2 0 0
13
4
-1
10
height differs bymore than 1
therefore, node 12is unbalanced
height differs bymore than 1
therefore, node 8 is unbalanced
CS350: Data Structures
AVL Trees
• When a node in the tree no longer satisfies the invariant required to be an AVL tree the tree must be rebalanced around that node
• There are four ways in which an insertion into a tree may cause an imbalance to occur at some node X: (1) An insertion in the left subtree of the left child of X(2) An insertion in the right subtree of the left child of X(3) An insertion in the left subtree of the right child of X(4) An insertion in the right subtree of the right child of X
11
symmetric
CS350: Data Structures
Creating an Imbalance -- Case #1
12
A B
N1
C
N2
A
B
N1
C
N2
(1) An insertion in the left subtree of the left child of X
root of A subtree increases height
as does N1 and N2
Node N1 and root of C differ by more than 1 therefore N2
violates AVL rules
-1-10
-1-1 -10
1
21
CS350: Data Structures
Fixing the Imbalance -- Case #1
13
A
B
N1
C
Perform a single right rotation
N2
CB
N2
N1
A
N1 becomes new root node
N2 now right child of N1
B subtree now left child of N2
-1
-10
1
-1-1
0 0
12
CS350: Data Structures
Creating an Imbalance -- Case #4 (symmetric with 1)
14
CB
N1
A
N2
(4) An insertion in the right subtree of the right child of X
N1
C
B
N1
A
N2
root of C subtree increases height
as does N1 and N2
Node N1 and root of A differ by more than 1 therefore N2
violates AVL rules
CS350: Data Structures
Fixing the Imbalance -- Case #4 (symmetric with 1)
15
A B
N2
N1
C
N1 becomes new root node
N2 now left child of N1
B subtree now right child of N2
C
B
N1
A
N2
Perform a single left rotation
CS350: Data Structures
Creating an Imbalance -- Case #2
16
A
N1
C
N3
B
N1
C
N3
(2) An insertion in the right subtree of the left child of X
root of B subtree increases height
as does N1 and N3
B A
Node N1 and root of C differ by more than 1 therefore N3
violates AVL rules
-10
-1-1
-11
0-1
1 2
CS350: Data Structures
Fixing the Imbalance -- Case #2
17
N1
C
N3
We know the node N2 exists because a node was just inserted into the B subtree
A
N2
B1
B2
N1
N3
A
C
B
-1
0-1
1
0-1
-11
-1 -1
2 2
CS350: Data Structures
Fixing the Imbalance -- Case #2
18
N2
C
N3
B1
B2
N1
N3
A
CN2
B1
B2
Perform a left-right double rotation
A
N1
Step #1 - Perform a single left rotation between N1 and N2
tree is still unbalanced
N3
-10
-11
-1 -1
0-1
-11
-1 -1
22
CS350: Data Structures
Fixing the Imbalance -- Case #2
19
N2
C
N3
B1
B2
A
N1
N1
C
N2
B1
B2A
Step #2 - Perform a single right rotation between N2 and N3
N3
-10
-11
-1 -1
-1 -1 -1 -1
0 0
12
Perform a left-right double rotation
CS350: Data Structures
Another look at the left-right double rotation without the intermediate steps
Fixing the Imbalance -- Case #2
20
N1
C
N2
B1
B2A
N3N1
C
N3
A
N2
B1
B2
CS350: Data Structures
Here is a look at the right-left double rotation without the intermediate steps
Fixing the Imbalance -- Case #3 (symmetric with 2)
21
N3
C
N2
B1
B2A
N1N1
A
N3
C
N2
B2
B1
CS350: Data Structures
• Inserting node 1 - First, recursively search for the
location which to insert the node- Next, insert the node- Finally, unwind the recursion
update node heights along the way. Rebalance tree where necessary• To update node’s height,
check its children
AVL Tree Insertion Example
23
12
4 10
8 16
14
2 6
1
0
1
-1
0
0
?
?
?
?
?
CS350: Data Structures
AVL Tree Insertion Example
24
12
4 10
8 16
14
2 6
1
node 1 is a leaf node so its height is 0
0
1
-1
0
0
?
?
?
?
0
CS350: Data Structures
AVL Tree Insertion Example
25
12
4 10
8 16
14
2 6
1
node 2 - add 1 to the height of its highest child
0
1
-1
0
0
1
?
?
?
0
CS350: Data Structures
AVL Tree Insertion Example
26
12
4 10
8 16
14
2 6
1
node 4 - add 1 to the height of its highest child
0
1
-1
0
0
1
2
?
?
0
CS350: Data Structures
AVL Tree Insertion Example
27
12
4 10
8 16
14
2 6
1
node 8 violates AVL properties, so rebalance
height differs bymore than 1
0
1
-1
0
0
1
2
?
?
0
CS350: Data Structures
AVL Tree Insertion Example
28
12
4 10
8 16
14
2 6
1
Case #1: Must perform single right rotation between node 8
and node 4
0
1
-1
0
0
1
2
?
?
0
CS350: Data Structures
AVL Tree Insertion Example
29
12
2 8
4 16
14
1
After rotation, update height of node 8 Node 4 is balanced, so continue
unwinding recursion
8
6 10
0
1
-1
0
1
0
1
2
?
0
CS350: Data Structures
AVL Tree Insertion Example
30
12
2 8
4 16
14
1
8
6 10
node 12 - add 1 to the height of its highest child
0
1
-1
0
1
0
1
2
3
0
CS350: Data Structures
Other Operations: find / remove
• The find operation is the same as the unbalanced binary search tree
• The remove operation works similarly to the remove operation from the unbalanced binary search tree with a few modifications - When a node is removed, the heights of its ancestors may need to be
updated as the recursion is unwound -- fix imbalances as they are encountered just like with insertion
31