AVL Balanced Trees - PowerPoint PPT Presentation

About This Presentation
Title:

AVL Balanced Trees

Description:

There is no reason why a Knuth binary tree should balance. ... imbalances through localised rearrangements following insertion on the recursive ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 27
Provided by: richar219
Category:

less

Transcript and Presenter's Notes

Title: AVL Balanced Trees


1
AVL Balanced Trees
  • Introduction
  • Data structure
  • AVL tree balance condition
  • AVL node level
  • AVL rotations
  • Choosing the rotation
  • Performing a balanced insertion
  • Outputting data
  • Testing and results

2
Introduction 1
  • There is no reason why a Knuth binary tree should
    balance. Average performance for random input
    should not be greatly below balanced performance,
    but if the tree is made from sorted input, it
    will degrade to a linked list with access time of
    O(N) where there are N nodes. Access time for a
    node within a balanced tree will be log2N or
    slightly less.
  • Operations which change the balance of a tree are
    insertion and deletion. Insertion might increase
    the maximum depth of a tree, but deletion can't
    do this.
  • Therefore, if effort is going to be expended in
    balancing an unbalanced tree, by rearranging
    nodes, this can initially be most usefully
    applied following insertion of nodes.

3
Introduction 2
  • A perfectly balanced tree can only exist if N is
    2x1 where x is an integer, i.e. if N is a value
    the series 1, 3, 7, 15, 31, 63, 127, 255 ... .
    The need for perfect balance could be relaxed by
    allowing the bottom level of the tree to be
    partially filled. This is difficult to achieve,
    as the entire tree might need rearranging after
    every insertion and deletion.
  • The imbalance problem only arises within the path
    through the tree on which a node is inserted. A
    recursive insertion function will return upwards
    through the insertion path. If information about
    the maximum depths of nodes is maintained as part
    of each nodes structure, it is possible to
    correct imbalances through localised
    rearrangements following insertion on the
    recursive return path upwards to the root.

4
Data Structure
5
AVL Tree Balance Condition
  • A particular approach to performing the tree
    rearrangements needed to keep a tree balanced was
    designed by AdelsonVelskii and Landis and named
    as the AVL tree.
  • An AVL tree has a balance condition such that
    each node stores the maximum depth of nodes below
    it. A NULL tree has a level of 1 and a leaf node
    has a level of 0. A node having 1 or 2 subtrees
    will have as its own level the maximum of left or
    right subtree levels 1. Within an AVL tree, the
    maximum difference allowed between left and right
    subtree levels is 1. When an insertion or
    deletion operation results in this difference
    increasing to 2, rearrangements called rotations
    are performed to reduce the imbalance for an AVL
    tree to retain the AVL balance condition.

6
AVL Node Level
  • Following an insertion or deletion which may
    influence the levels of
  • subtrees, the level of nodes affected will need
    to be recalculated. This can be achieved using a
    function operating on the above data structure as
    follows

7
AVL Rotations
  • A difference of levels between left and right
    subtrees can be increased, through insertion of a
    new node into the tree, in one of 4 possible
    ways
  • 1. Insertion into left subtree of left
    branch.
  • 2. Insertion into right subtree of left
    branch.
  • 3. Insertion into left subtree of right
    branch.
  • 4. Insertion into right subtree of right
    branch.
  • The function prototype for a rotation will be of
    theform
  • void rotN(TOP t) / N is rotation type 1-4 /

8
Rotation case 1
9
Rotation case 1
10
Rotation case 2
11
Rotation case 2
12
Rotation case 3
13
Rotation case 3
14
Rotation case 4
15
Rotation case 4
16
Choosing the rotation slide 1
The rotation type 1 4 can be determined by
comparing 3 keys, the key inserted and the child
and parent key. It is useful to have a 2 way
comparison function similar to the strcmp()
function in string.h.
17
Choosing the rotation slide 2
18
Calling the rotation
19
Performing an AVL balanced insertion 1 of 3
20
Performing an AVL balanced insertion 2 of 3
21
Performing an AVL balanced insertion 3 of 3
22
Output of AVL organised data 1
  • Printing the entire tree can be done recursively
    in search order. It is useful when debugging to
    be able to identify the immediate left and right
    child nodes for each record printed.
  • define DEBUG 1 / or 0 for less noisy output /
  • Using a printdata function enables the printall
    function to be data independent.
  • void printdata(DATA d) / prints DATA item d /
  • printf("c ",d)

23
Output of AVL organised data 2
24
Testing code
25
Test Results
  • AVL imbalance -2 AVL type 4
  • AVL imbalance -2 AVL type 3
  • AVL imbalance 2 AVL type 2 ...
  • o insert duplicate key error
  • ... (run the program avltree.c to see full o/p)?
  • a 0 a b 2 c c 1 d d 0
  • b e 3 g f 0 f g 1 h h 0
  • e i 4 k j 0 j k 2 m l 0
  • l m 1 i n 5 u o 1 p p 0
  • o q 2 s r 0 r s 1 t t 0
  • q u 3 w v 0 v w 2 y x 0
  • x y 1 z z 0
  • These results show in 4 columns per node
  • i. The left child key or blank. ii. The node key.
  • iii. The node level. iv. The right key or
    blank.

26
Recommended Reading
  • "Data Structures and Algorithms in C",
  • Mark Allen Weiss, Addison Wesley Chapter 4.
Write a Comment
User Comments (0)
About PowerShow.com