AVL Trees
Back to Binary Search Trees
buildTree for BST
Let’s consider buildTree (insert values starting from an empty tree)
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
• If inserted in given order,
what is the tree?
buildTree for BST
Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST
What we if could somehow re-arrange them
• median first, then left median, right median, etc.
5, 3, 7, 2, 1, 4, 8, 6, 9
• What tree does that give us?
Balancing Binary Search Trees
How can we make a BST efficient?
Observation
Solution: Require a Balance Condition that
• When we build the tree, make sure it’s balanced.
• BUT…Balancing a tree only at build time is insufficient.
• We also need to keep the tree balanced as we perform operations.
AVL Tree
Invented by Georgy Adelson-Velsky and Evgenii Landis in 1962
The AVL Tree Data Structure
An AVL tree is a self-balancing binary search tree.
Structural properties
1. Binary tree property (same as BST)
2. Order property (same as for BST)
3. Balance condition:
balance of every node is between -1 and 1
where balance(node) = height(node.left) – height(node.right)
Example #1: Is this an AVL Tree?
Balance Condition:
balance of every node is between -1 and 1
where balance(node) =
height(node.left) – height(node.right)
11
1
8
4
6
10 12
7
Example #2: Is this an AVL Tree?
3
11
7
1
8
4
6
2
5
Balance Condition:
balance of every node is between -1 and 1
where balance(node) =
height(node.left) – height(node.right)
AVL Trees – Showing maximum height / Depth
for each node
20
9
2 15
5
10
30
17
7
0
0 0
0
1 ( RC)
1
2 (RC) 2
3
AVL tree operations
• AVL find:
• Same as usual BST find
• AVL insert:
• AVL delete:
• The “easy way” is lazy deletion
• Otherwise, do the deletion and then check for several imbalance cases (we will skip
this)
An insertion into the left subtree of left child
Example - An insertion into the left subtree of
left child
An insertion into the right subtree of right
child
Example - An insertion into the left subtree of
left child
An insertion into the left subtree of right child
An insertion into the right subtree of left child
First insert example
Insert(6)
Insert(3)
Insert(1)
Third insertion
What’s the only way to fix it?
Fix: Apply “Single Rotation”
• Single rotation: The basic operation we’ll use to rebalance
• Move child of unbalanced node into parent position
• Parent becomes the “other” child (always okay in a BST!)
• Other subtrees move in only way BST allows (we’ll see in generalized example)
3
1 6
6
3
AVL Property
violated at node 6
1
AVL Tree insert (more specific):
1. Insert the new node as in our generic BST (a new leaf)
2. For each node on the path from the root to the new leaf, the
insertion may (or may not) have changed the node’s height
3. So after insertion in a subtree,
Practice time! Example of Case #4
A)
50
10
80
30
60
10
30
42
80
50
60
10
30
50
80
42
60
10
30
50
80
42
60
42
10
30
80
60
50
B) C) D)
Which of the following
is the updated AVL tree
after inserting 42?
Starting with this
AVL tree:
Practice time! Example of Case #4
50
10
80
30
60 Which of the following
is the updated AVL tree
after inserting 42?
Starting with this
AVL tree:
What rotations did we do?
What’s the name of this case?
Insert, summarized
• Insert as in our generic BST
• Check back up path for imbalance, which will be 1 of 4 cases:
• Node’s left-left grandchild is too tall
• Node’s left-right grandchild is too tall
• Node’s right-left grandchild is too tall
• Node’s right-right grandchild is too tall
• Only occurs because
• After the appropriate single or double rotation, the smallest-unbalanced subtree
has the same height as before the insertion
• So all ancestors are now balanced
25
Pros and Cons of AVL Trees
Arguments for AVL trees:
1. All operations logarithmic worst-case because trees are always balanced
2. Height balancing adds no more than a constant factor to the speed of
insert and delete
Arguments against AVL trees:
1. Difficult to program & debug [but done once in a library!]
2. More space for height field
3. Asymptotically faster but rebalancing takes a little time
4. If amortized logarithmic time is enough, use splay trees (also in the text,
not covered in this class)

AVL Trees.pptx DATA STRUCTURES AND ALGORITHMS

  • 1.
  • 2.
    Back to BinarySearch Trees
  • 3.
    buildTree for BST Let’sconsider buildTree (insert values starting from an empty tree) Insert values 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST • If inserted in given order, what is the tree?
  • 4.
    buildTree for BST Insertvalues 1, 2, 3, 4, 5, 6, 7, 8, 9 into an empty BST What we if could somehow re-arrange them • median first, then left median, right median, etc. 5, 3, 7, 2, 1, 4, 8, 6, 9 • What tree does that give us?
  • 5.
  • 6.
    How can wemake a BST efficient? Observation Solution: Require a Balance Condition that • When we build the tree, make sure it’s balanced. • BUT…Balancing a tree only at build time is insufficient. • We also need to keep the tree balanced as we perform operations.
  • 7.
    AVL Tree Invented byGeorgy Adelson-Velsky and Evgenii Landis in 1962
  • 8.
    The AVL TreeData Structure An AVL tree is a self-balancing binary search tree. Structural properties 1. Binary tree property (same as BST) 2. Order property (same as for BST) 3. Balance condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right)
  • 9.
    Example #1: Isthis an AVL Tree? Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right) 11 1 8 4 6 10 12 7
  • 10.
    Example #2: Isthis an AVL Tree? 3 11 7 1 8 4 6 2 5 Balance Condition: balance of every node is between -1 and 1 where balance(node) = height(node.left) – height(node.right)
  • 11.
    AVL Trees –Showing maximum height / Depth for each node 20 9 2 15 5 10 30 17 7 0 0 0 0 1 ( RC) 1 2 (RC) 2 3
  • 12.
    AVL tree operations •AVL find: • Same as usual BST find • AVL insert: • AVL delete: • The “easy way” is lazy deletion • Otherwise, do the deletion and then check for several imbalance cases (we will skip this)
  • 14.
    An insertion intothe left subtree of left child
  • 15.
    Example - Aninsertion into the left subtree of left child
  • 16.
    An insertion intothe right subtree of right child
  • 17.
    Example - Aninsertion into the left subtree of left child
  • 18.
    An insertion intothe left subtree of right child
  • 19.
    An insertion intothe right subtree of left child
  • 20.
    First insert example Insert(6) Insert(3) Insert(1) Thirdinsertion What’s the only way to fix it?
  • 21.
    Fix: Apply “SingleRotation” • Single rotation: The basic operation we’ll use to rebalance • Move child of unbalanced node into parent position • Parent becomes the “other” child (always okay in a BST!) • Other subtrees move in only way BST allows (we’ll see in generalized example) 3 1 6 6 3 AVL Property violated at node 6 1
  • 22.
    AVL Tree insert(more specific): 1. Insert the new node as in our generic BST (a new leaf) 2. For each node on the path from the root to the new leaf, the insertion may (or may not) have changed the node’s height 3. So after insertion in a subtree,
  • 23.
    Practice time! Exampleof Case #4 A) 50 10 80 30 60 10 30 42 80 50 60 10 30 50 80 42 60 10 30 50 80 42 60 42 10 30 80 60 50 B) C) D) Which of the following is the updated AVL tree after inserting 42? Starting with this AVL tree:
  • 24.
    Practice time! Exampleof Case #4 50 10 80 30 60 Which of the following is the updated AVL tree after inserting 42? Starting with this AVL tree: What rotations did we do? What’s the name of this case?
  • 25.
    Insert, summarized • Insertas in our generic BST • Check back up path for imbalance, which will be 1 of 4 cases: • Node’s left-left grandchild is too tall • Node’s left-right grandchild is too tall • Node’s right-left grandchild is too tall • Node’s right-right grandchild is too tall • Only occurs because • After the appropriate single or double rotation, the smallest-unbalanced subtree has the same height as before the insertion • So all ancestors are now balanced 25
  • 26.
    Pros and Consof AVL Trees Arguments for AVL trees: 1. All operations logarithmic worst-case because trees are always balanced 2. Height balancing adds no more than a constant factor to the speed of insert and delete Arguments against AVL trees: 1. Difficult to program & debug [but done once in a library!] 2. More space for height field 3. Asymptotically faster but rebalancing takes a little time 4. If amortized logarithmic time is enough, use splay trees (also in the text, not covered in this class)