AVL Trees - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

AVL Trees

Description:

AVL Trees Drozdek Section 6.7.2 pages 257 -262 * – PowerPoint PPT presentation

Number of Views:191
Avg rating:3.0/5.0
Slides: 44
Provided by: Roll61
Category:

less

Transcript and Presenter's Notes

Title: AVL Trees


1
AVL Trees
  • Drozdek Section 6.7.2
  • pages 257 -262

2
Objectives
  • You will be able to
  • Describe an AVL tree.
  • Describe the operations by which an AVL tree is
    maintained as nearly balanced as items are added.
  • Add a new node to the diagram of an AVL tree.

3
Tree Balancing
  • Consider a BST of state abbreviations.
  • If the states are entered in the order
  • NY, IL, GA, RI, MA, PA, DE, IN, VT, TX, OH, WY.
  • the tree will be balanced.
  • When tree is nicely balanced, search time is
    O(log2n)

4
Tree Balancing AVL Trees
  • If abbreviations entered in increasing order
  • DE, GA, IL, IN, MA, MI, NY, OH, PA, RI, TX, VT,
    WY
  • BST degenerates into linked list with search time
    O(n)

5
AVL Tree
  • A height balanced tree
  • Tree is rebalanced after additions and deletions
    make it skewed beyond a certain limit.
  • Search time is O(log2n)
  • http//en.wikipedia.org/wiki/AVL_tree
  • Named after its two inventors
  • G.M. Adelson-Velskii and E.M. Landis
  • Adelson-Velskii, G. E. M. Landis (1962). "An
    algorithm for the organization of information".
    Proceedings of the USSR Academy of Sciences 146
    263266. (Russian) English translation by Myron
    J. Ricci in Soviet Math. Doklady, 312591263,
    1962.

6
Balance Factor
  • The Balance Factor of a node is defined as height
    of its left subtree minus height of its right
    subtree.
  • Note Some authors define balance factor as right
    - left.
  • Drozdek defines balance factor as right - left.
  • Examples in this presentation are from a
    different book.
  • A balance factor with absolute value greater than
    1 is unacceptable for an AVL tree.

7
AVL Tree Definition
  • Defined as
  • Binary search tree
  • Balance factor of each node is 0, 1, or -1
  • Key is to rebalance the tree whenever an
    insertion or a deletion creates a balance factor
    with absolute value greater than 1 at any node.

8
AVL Tree as an ADT
  • The differences between an AVL tree and the
    Binary Search Tree that we have studied
    previously are purely internal.
  • As seen from the outside they are identical.

9
Examples AVL Trees
Numbers in the circles are the balance factors,
not the values stored in the nodes.
Nyhoff page 841
10
Examples Not AVL Trees
Nyhoff page 841
11
AVL Trees
  • Insertions may require adjustment of the tree to
    maintain balance.
  • Given tree
  • Becomes unbalanced
  • Requires a right rotation on subtree RI
  • Producing balanced tree

12
Observation
  • Inserting a new item into an AVL tree can result
    in a subtree having a balance factor (absolute
    value) greater than 1.
  • But no more than 2.
  • Inserting a new item does not necessarily
    increase the balance factor.
  • May leave it unchanged.
  • May decrease it.
  • If the balance factor is unacceptable after an
    insertion, it can be fixed by one of four
    possible rebalancing rotations.

13
Examples
  • Lets look at how we can rebalance the tree while
    adding states.
  • We will add states to the tree in the order RI,
    PA, DE, GA, OH, MA, IL, MI, IN, NY, VT, TX, WY
  • Start by adding RI to an empty tree.

14
Add PA
Tree is still balanced.
15
Add DE
Tree is now unbalanced. A right rotation of RI
will restore balance.
16
Rotations
  • AVL tree rotations rearrange links so as to
    reduce the height of the larger subtree at an
    unbalanced node.
  • They always preserve the BST invariants
  • Everything in a node's left subtree is less than
    or equal to it.
  • Everything in a node's right subtree is greater
    than or equal to it.

17
Add GA
Tree is still balanced.
18
Add OH
The subtree rooted at DE is now unbalanced. A
left rotation of DE will restore balance.
19
Add MA
The subtree rooted at PA is now unbalanced. We
need to do a right rotation at PA. But we cant
do that immediately with a simple right rotation.
We need to do a left rotation at GA first. This
is a left-right rotation.
20
After Left Rotation at GA
Now we can do a right rotation at PA
obtaining a balanced tree.
21
Insert IL and MI
Tree is still balanced.
22
Insert IN
The subtree rooted at GA is now unbalanced. Again
we need a double rotation to restore
balance. Start with a right rotation at MA.
23
After Right Rotation at MA
Now we can do a left rotation at GA, restoring
balance.
24
Insert NY
The subtree rooted at OH is now unbalanced. We
will need a left-right double rotation in the
left subtree of OH. Notice what happens to IN!
25
After Left Rotation at IL
Now we can do a right rotation at OH, restoring
balance.
26
Insert VT
Produces an inbalance at PA, which can be removed
with a simple left rotation to yield
27
Insert TX and WY
This does not unbalance the tree, resulting in
the final version.
28
Observations
  • When an unacceptable imbalance was produced, we
    always did a rotation at the first ancestor of
    the added node having an unacceptable imbalance.
  • We always rotated that node away from side with
    the larger subtree height.
  • Its child with the larger subtree height takes
    its place and it becomes a new child of that
    node.
  • If the first two steps from the first unblanced
    ancestor toward the added node were in the same
    direction, a single rotation restored balance.
  • Otherwise, a double rotation was necessary.

29
Rebalancing Rotations
  1. Single right rotationUse when first unacceptably
    unbalanced ancestor has a balance factor of 2
    and the inserted item is in the left subtree of
    its left child.
  2. Single left rotationUse when first unacceptably
    unbalanced ancestor has a balance factor of -2
    and the inserted item is in right subtree of its
    right child.
  3. Left-right rotationUse when first unacceptably
    unbalanced ancestor has a balance factor of 2
    and the inserted item is in the right subtree of
    its left child.
  4. Right-left rotationUse when first unacceptably
    unbalanced ancestor has a balance factor of -2
    and the inserted item is in the left subtree of
    its right child.

30
Single Right Rotation
  • Used when the nearest out of balance ancestor of
    the inserted node has a balance factor of 2
    (heavy to the left) and the inserted node is in
    the left subree of its left child.
  • First two steps from ancestor to inserted node
    are both to the left.
  • Shown graphically on next slide.
  • Described in detail on the slide following that.

31
Single Right Rotation
Note that the right subtree of B R(B) flips over
to become the left subtree of A.
32
Single Right Rotation
  • Used when the nearest out of balance ancestor of
    the inserted node has a balance factor of 2
    (heavy to the left) and the inserted node is in
    the left subree of its left child.
  • Let A nearest out of balance ancestor of
    inserted item
  • B left child of A
  • Set link from parent of A to point to B.
  • Set left link of A to point to the right child of
    B.
  • Set right link of B to point to A

33
Single Left Rotation
  • Symmetrical with single right rotation.
  • Exchange right and left in description of
    single right rotation.

34
Single Left Rotation
  • Used when the nearest out of balance ancestor of
    the inserted node has a balance factor of -2
    (heavy to the right) and the inserted node is in
    the right subree of its rightchild.
  • Let A nearest out of balance ancestor of
    inserted item
  • B right child of A
  • Set link from parent of A to point to B.
  • Set right link of A to point to the left child of
    B.
  • Set left link of B to point to A

35
Left-Right Rotation
  • Used when the nearest out of balance ancestor of
    the inserted node has a balance factor of 2
    (heavy to the left) and the inserted node is in
    the right subree of its left child.
  • First two steps from ancestor to inserted node
    are in opposite directions (left then right).
  • Shown graphically on following slides
  • Described in detail on slides following them.

36
Left-Right Rotation
Suppose we insert a new node into the left
subtree of C, increasing the height of that
subtree.
Nyhoff page 851
37
Left-Right Rotation
Do a left rotation at B (left child of first
out-of-balance ancestor of added node.)
38
Left-Right Rotation
Note that the left subtree of C L(C) flips over
to become the right subtree of B.
Now do a right rotation at A.
39
Left-Right Rotation
The right subtree of C R(C) has become the left
subtree of A.
40
Left-Right Rotation
  • Used when the nearest out of balance ancestor of
    the inserted node has a balance factor of 2
    (heavy to the left) and the inserted node is in
    the right subree of its left child.
  • Let A nearest ancestor of the inserted item.
  • B left child of A
  • C right child of B
  • New item added to subtree rooted at C.
  • First do a left rotation of B and C.
  • Set left link of A to point to C.
  • Set right link of B equal to left link of C
  • Set left link of C to point to B
  • Now a right rotation of A and C.
  • Reset link from parent of A to point to C
  • Set left link of A equal to right link of C
  • Set right link of C to A

41
Right-Left Rotation
  • The Right-Left Rotation is symmetrical.
  • In the description of the Left-Right rotation,
    exchange right and left .

42
Summary
  • Adding nodes to a binary tree can result in an
    unbalanced tree.
  • But we can restore balance with one of four kinds
    of rotations.
  • Single Left
  • Single Right
  • Left-Right
  • Right-Left

43
Exercise
  • Draw by hand, showing the tree after each step
    with the balance factor at each node, as the
    following values are added to an AVL tree of
    integers
  • 10 5 1 20 8 15 25
  • The resulting tree should be complete and
    balanced.
  • Repeat the exercise with the integers added in
    the following order
  • 5 1 10 20 15 8 25
  • The final result should be the same, but some
    intermediate results will be different.
Write a Comment
User Comments (0)
About PowerShow.com