AVL Trees - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

AVL Trees

Description:

For every node the left and right side differ in height by less than two. ... Play the game http://www.seanet.com/users/arsen/avltree.html. The Insertion Algorithm ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 18
Provided by: math130
Category:
Tags: avl | com | double | ended | play | trees

less

Transcript and Presenter's Notes

Title: AVL Trees


1
AVL Trees
  • And the pain you must suffer to learn them

2
The AVL Property
  • For every node the left and right side differ in
    height by less than two.
  • The left and right side are themselves AVL trees.

3
How Balanced is That?
  • AVL trees might end up kinda 'sparse'.
  • The worst case height of an AVL tree with n nodes
    is about 1.44log(n).
  • Discussion at http//ciips.ee.uwa.edu.au/morris/Y
    ear2/PLDS210/AVL.html

4
Rotations
  • Rotations are to rearrange nodes to maintain
    AVLness.
  • They are needed on insert and delete.

5
Names of Rotations
  • There are four of them
  • Single Left (LL) and Single Right (RR)
  • Double Left (LR) and Double Right (RL)
  • The names describe which way the node moves. For
    example, a left rotation moves the node down and
    left.
  • Nodes always move down when rotated.

6
How To Do Single Rotations
7
A Left Rotation via Pointers
  • tempp-gtright
  • p-gtrighttemp-gtleft
  • temp-gtleftp
  • ptemp

8
Double Rotation
  • A LR double rotation is to rotate something to
    the left, and then its former parent to the
    right.
  • A RL double rotation is to rotate something to
    the right, and then its former parent to the left.

9
Rotation Examples
  • All the rotations in picture form
    http//sky.fit.qut.edu.au/maire/avl/System/AVLTre
    e.html
  • All the rotations in pictures and text
    http//www.cs.jcu.edu.au/Subjects/cp2001/1997/foil
    s/balancedTrees/balancedTrees.html
  • Sample single and double rotationshttp//ironbark
    .bendigo.latrobe.edu.au/courses/subjects/DataStruc
    tures/mal/session200/lecture.html

10
The AVL Insertion Algorithm
  • An Intuitive description can be found at
    http//www.ucfv.bc.ca/cis/watkissc/200009/200009Co
    mp175/notes/avlTrees.html

11
The AVL Game
  • Play the game http//www.seanet.com/users/arsen/av
    ltree.html

12
The Insertion Algorithm
  • Recursively call down to find the insertion
    point.
  • Insert there (it will always be a leaf node).
  • Call rebalance at the end.
  • Will rebalance from the bottup up.
  • Will rebalance only the path from the change to
    the root.

13
Insertion Code
  • insert_node (inData)
  • if (this NULL)
  • return new NodeltTgt (inData)
  • if (inData lt data)
  • left left -gt insert_node (inData)
  • else
  • right right -gt insert_node
    (inData)
  • return balance ()

14
Notes About the Code
  • Calls balance on the way up.
  • Only calls balance on the path between the change
    and the root.
  • Call balance too many times for an insert.
  • In this example ....We never change the parent
    node (or root).Instead we return what that
    parent node should be. Neat trick!

15
The Deletion Algorithm
  • Find the place to delete.
  • Swap with the in order successor.
  • Delete that node
  • Remember, he might have ONE kid.
  • Rebalance on the way up from the node you just
    deleted.

16
Balancing
  • Let d difference in height between kids.
  • If (abs(d) lt 2) return
  • If (d lt 0) // Heavy on the left
  • if (right-gtdifference gt 0) // kid heavy on
    right
  • right right-gtrotate_right()
  • return rotate_left()
  • .... // same stuff other side
  • recompute my height
  • return

17
Computing the Height
  • Each node's height is just max(left-gtheight,
    right-gtheight) 1
  • This only changes along the path from
    insertion/deletion to the root.
  • The obvious recursive code searches the entire
    tree ... don't do that!
Write a Comment
User Comments (0)
About PowerShow.com