Chien-Pin Hsu - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chien-Pin Hsu

Description:

Presented by: Chien-Pin Hsu CS146 Prof. Sin-Min Lee Outline Introduction Definitions Height-Balanced Trees Picking Out AVL Trees Why AVL Trees? Added Complexity ... – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 27
Provided by: BenH150
Learn more at: http://www.cs.sjsu.edu
Category:
Tags: chien | hsu | pin

less

Transcript and Presenter's Notes

Title: Chien-Pin Hsu


1
AVL Trees
  • Presented by
  • Chien-Pin Hsu
  • CS146
  • Prof. Sin-Min Lee

2
Outline
  • Introduction
  • Definitions
  • Height-Balanced Trees
  • Picking Out AVL Trees
  • Why AVL Trees?
  • Added Complexity
  • Violations
  • The Balance Factor
  • What is a rotation?
  • Single Rotations
  • Double Rotations

3
Introduction
  • AVL tree is the first balanced binary search tree
    (name after its discovers, Adelson-Velskii and
    Landis).
  • The AVL tree is a balanced search tree that has
    an additional balance condition.
  • The balance condition must be easy to maintain
    and ensures that the depth of the tree is O(logN)

4
Definitions
  • An AVL tree is a binary search tree that is
    height balanced meaning that it has these two
    properties..
  • 1. The sub-tree of every node must differ
    in height by at most one.
  • 2. Every sub-tree is an AVL tree

5
Height-Balanced Trees
  • Height of a tree is the length of the longest
    path from a root to a leaf.
  • A binary search tree T is a height-balanced
    k-tree or HBk-tree, if each node in the tree
    has the HBk property.
  • A node has the HBk property if the height of
    the left and right sub-trees of the node differ
    in height by at most k.
  • A HB1 tree is called an AVL tree.

6
Picking Out AVL Trees
  • Notation NODE Left-Ht, Right-Ht

Yes! 5 2, 1 4 1, 0 3 0, 0 7 0, 0
No! 10 3, 1
7
Picking Out AVL Trees (Contd)
  • Notation NODE Left-Ht, Right-Ht

No! 8 4, 2 4 3, 1 3 2, 0
8
Why AVL Trees?
  • The worst case for Binary Search Trees
  • Insert O(n).
  • Delete O(n).
  • Search O(n).
  • The worst case for AVL trees
  • Insert O(log n).
  • Delete O(log n).
  • SearchO(log n).
  • Recall when comparing the time complexity
  • O(log n) lt O(n)

9
Why AVL Trees? (Contd)
  • AVL trees allow for logarithmic Inserts and
    Deletes, and they allow for easy traversals, such
    as the In-order traversal if we want to sort
    our elements.

10
Added Complexity
  • Although we have made the Insertion, Deletion
    Searching better, an additional amount of
    complexity does arise during the Insertions and
    the Deletions.
  • After Inserting and Deleting for an AVL tree, the
    new tree might violate the two properties of the
    AVL tree.

11
Violations
Notation NODE Left-Ht, Right-Ht
No! 5 3,1 4 2,0
No! 5 2, 0
12
The Balance Factor
  • In order to detect when a violation of a AVL
    tree occurs, we need to have each node keep track
    of the difference in height between its right and
    left sub-trees.
  • Notation for the balance factor
  • bf (node) Left-Ht Right- Ht
  • To be a valid AVL Tree, -1 lt bf(x) lt 1 for all
    nodes.

13
The Balance Factor (Contd)
  • Notation for the balance factor
  • bf (node) Left-Ht Right- Ht

bf(8) 3 3 0 bf(4) 2 1 1 bf(10) 1
2 -1 bf(3) 1 0 1 bf(5) 0 0 0 bf(9)
0 0 0 bf(11) 0 1 -1 bf(1) 0 0
0 bf(12) 0 0 0
Valid Tree!!
14
The Balance Factor (Contd)
Notation for the balance factor bf
(node) Left-Ht Right- Ht
bf(8) 2 0 2 bf(4) 1 0 1 Bf(3) 0 0
0 Invalid Tree!!
15
What is a rotation?
  • A rotation is an operation to rearrange nodes to
    maintain balance of an AVL tree after adding and
    removing a node
  • There are two case to perform the rotation
  • 1) Single Rotation
  • 2) Double Rotation
  • When to perform rotations?
  • we need to perform a rotation anytime a nodes
    balance factor is 2 or -2

16
Single Rotations (Contd)
  • Algorithm for right rotation
  • Algorithm rotateRight(nodeN) nodeC left
    child of nodeN Set nodeNs left child to
    nodeCs right child Set nodeCs right child to
    nodeN

17
Single Rotations
  • After inserting (a) 60 (b) 50 (c) 20 into an
    initially empty binary search tree, the tree is
    not balanced d) a corresponding AVL tree rotates
    its nodes to restore balance.

18
Single Rotations (Contd)
  • Algorithm for left rotation
  • Algorithm rotateLeft(nodeN) nodeC right
    child of nodeN Set nodeNs right child to
    nodeCs left child Set nodeCs left child to
    nodeN

19
Single Rotations (Contd)
(a) Adding 80 to tree does not change the
balance of the tree (b) a subsequent
addition of 90 makes tree unbalanced (c)
left rotation restores balance.
20
Double Rotations
  • Before and after an addition to an AVL sub-tree
    that requires both a right rotation and a left
    rotation to maintain its balance.

21
Double Rotations (Contd)
Before and after an addition to an AVL sub-tree
that requires both a left and right rotation to
maintain its balance.
22
Double Rotations (Contd)
  • Algorithm rotateLeftRight(nodeN)
  • nodeC left child of nodeNSet nodeNs left
    child to the sub-tree produced by
  • rotateLeft(nodeC) rotateRight(nodeN)

23
Double Rotations (Contd)
  1. Inserting 10 ,40,and 55 maintain its balance
  2. Inserting 35 destroys the balance
  3. After a left rotation
  4. After a right rotation

24
Double Rotations (Contd)
  • Algorithm rotateRightLeft(nodeN)
  • nodeC right child of nodeNSet nodeNs
    right child to the sub-tree produced by
  • rotateRight(nodeC) rotateLeft(nodeN)

25
Double Rotations (Contd)
(a) Adding 70 to the tree destroy its balance.
To restore the balance, perform both (b) a
right rotation and (c) a left rotation.
26
The End
Write a Comment
User Comments (0)
About PowerShow.com