AVL Trees - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

AVL Trees

Description:

In fact it is less efficient than a linked list because of the additional ... An AVL tree with n nodes can be searched in O(log n) time ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 29
Provided by: patric190
Category:
Tags: avl | fact | trees

less

Transcript and Presenter's Notes

Title: AVL Trees


1
AVL Trees
2
Problems with Binary Search Trees
  • Adding the sorted sequence A, B, C, ..., G to a
    binary search trees leads gives a tree that is no
    better than a linear list...

In fact it is less efficient than a linked list
because of the additional overhead associated
with each node.
A
3
AVL trees Basic Concepts
  • Named after its two inventors, who published it
    in 1962.
  • G.M. Adelson-Velskii
  • E.M. Landis
  • Self-balancing BST
  • Balance factor the height of its right subtree
    minus the height of its left subtree
  • A node with balance factor equal to -1, 0 or 1
    considered balanced
  • Whenever a tree is not balanced then rotate
    around the root
  • Rotate the root into the lighter subtree

4
AVL trees some properties
  • The height of an AVL tree with n nodes is O(log
    n)
  • The height balance property
  • An AVL tree with n nodes can be searched in O(log
    n) time
  • Any subtree of an AVL tree is an AVL tree
  • Searching, insertion, and deletion can be done in
    O(log n) time

5
AVL trees examples
  • Examples AVL trees
  • Examples non-AVL trees

6
Rotation
  • Definition
  • To switch children and parents among two or three
    adjacent nodes to restore balance of a tree.
  • A rotation may change the depth of some nodes,
    but does not change their relative ordering.
  • Types
  • Left rotation
  • Right rotation
  • Left right rotation
  • Right left rotation

7
Right Rotation
  • Definition
  • In a binary search tree, pushing a node A down
    and to the right to balance the tree.
  • Three steps (1) get the left child of node A
    node B (2) Set the right child of node B as the
    left child of node A and (3) Set node A as the
    right child of node B.

8
Left Rotation
  • Definition
  • In a binary search tree, pushing a node A down
    and to the left to balance the tree.
  • Three steps (1) get the right child of node A
    node B (2) Set the left child of node B as the
    right child of node A and (3) Set node A as the
    left child of node B.

9
Left Right Rotation
  • Definition
  • Sometimes a single left rotation is not
    sufficient to balance an unbalanced tree.
  • Defined to be a right rotation at the right child
    of the node followed by a left rotation at the
    node

10
Right Left Rotation
  • Definition
  • Similarly, sometimes a single right rotation is
    not sufficient to balance an unbalanced tree.
  • Defined to be a left rotation at the left child
    of the node followed by a right rotation at the
    node

11
Rotations when and How
  • To decide when you need a tree rotation is
    usually easy, but determining which type of
    rotation requires a little thought.
  • General guidelines
  • IF a tree is right heavy (balance factorgt1)
  • IF trees right subtree is left heavy (balance
    factor lt-1)
  • Perform Left Right rotation
  • ELSE
  • Perform single Left rotation
  • IF a tree is left heavy (balance factorlt-1)
  • IF trees left subtree is right heavy (balance
    factor gt1)
  • Perform Right Left rotation
  • ELSE
  • Perform single Right rotation

12
Building an AVL tree added A
A
13
Building an AVL tree added B
A
B
14
Building an AVL tree adding C
Right hand side subtree has height of 2
Tree A unbalanced (Balance factor 2)
15
Building an AVL tree adding C
Left rotation
B
C
A
16
Building an AVL tree added D
B
C
A
D
17
Building an AVL tree adding E
B
C
A
D
E
Note Both B and C have a balance factor equal to
2. We start with the deepest node that is
unbalanced.
18
Building an AVL tree added E
It is a balanced tree
B
D
A
E
C
19
Building an AVL tree adding F
Oh, tree B is unbalanced!
B
D
A
E
C
F
20
Building an AVL tree adding F
B
D
A
E
C
F
21
Building an AVL tree added F
  • Note
  • D becomes Bs parent (and B becomes Ds left
    child)
  • B becomes parent of Ds left child
  • C becomes Bs right child

22
Building an AVL tree adding G
Oh, E becomes unbalanced!
D
E
B
F
A
C
G
23
Building an AVL tree added G
D
F
B
G
A
C
E
24
Building an AVL tree added T
D
F
B
G
A
C
E
T
Checking balance factors for every node
everything is OK!
25
Building an AVL tree adding K
D
F
B
G
A
C
E
T
Oh, G becomes unbalanced and its right subtree is
left heavy!!!
K
26
Building an AVL tree adding K
D
F
B
G
A
C
E
K
Step 1. Rotate this subtree right to make it
heavier on the right.
T
27
Building an AVL tree adding K
D
F
B
G
A
C
E
K
Step 2. Rotate this subtree left to make it to
balance it.
T
28
Building an AVL tree added K
D
F
B
K
A
C
E
T
G
Write a Comment
User Comments (0)
About PowerShow.com