AVL Tree and Heap - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

AVL Tree and Heap

Description:

A binary tree has the heap property iff. It is empty or ... which must become empty is occupied by M (complete tree property) M ... 5. Tree is now a heap again ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 14
Provided by: venkat3
Category:
Tags: avl | heap | tree

less

Transcript and Presenter's Notes

Title: AVL Tree and Heap


1
AVL Tree and Heap
  • Data Structures
  • Rutgers University

2
Binary search tree
  • Binary search trees The run times can be far
    worse than O(log n) for search, insert and delete
  • Worst case when completely lopsided, or skewed on
    one side or the other. ? O(n) time for
    search/insert/delete.
  • To maintain the O(log n) time, the height of the
    tree would have to be maintained at O(log n) ?
    Need to be in balanced condition
  • To achieve this ? rebalance at every insert or
    delete

3
Height balanced binary search trees
  • AVL tree
  • Named after the inventors, (Russina
    mathematicians) G.M. Adelson-Velskii and Em.M.
    Landis (1962)
  • Red-black trees
  • We will not cover them in this course

4
AVL Tree Structure
  • AVL Tree is Height balanced binary search tree
  • Ideally, for every node, the heights of its left
    and right subtrees are equal.
  • AVL definition of height balance
  • An AVL tree is a binary search tree in which the
    heights of the left and right subtrees of every
    node differ by at most 1.

5
Examples
6
Examples
Recursive definition An AVL tree is a binary
search tree in which the left and right subtrees
of the root are AVL trees whose heights differ by
at most 1.
7
Height Imbalance During Insertion or Deletion
8
Rotation
  • Locally rearrange the structure of an AVL tree

9
Heaps
  • Heaps or based on the notion of a complete tree
  • A complete tree is filled from the left
  • All the leaver are on the same level or two
    adjacent ones
  • All nodes at the lowest level are as far to the
    left as possible
  • A binary tree is completely full ?? 2h1-1 nodes
    (h is height)
  • A binary tree is complete iff
  • It is empty or
  • Its left subtree is complete of height h-1 and
    its right subtree is completely full of height
    h-2 or
  • Its left subtree is completely full of height h-1
    and its right subtree is complete of height h-1

10
Heaps
  • A binary tree has the heap property iff
  • It is empty or
  • The key in the root is larger than that in either
    child and both subtrees have the heap property
  • Heap can be used as a priority queue
  • Highest priority item is at the root and is
    trivially extracted
  • If root is deleted, we are left with two
    sub-trees and we must recreate a single tree with
    heap property
  • We can both extract the highest priority item and
    insert a new item in O(log n) time.

11
Deletion from a Heap
T
S
P
G
R
O
N
A
M
C
I
A
E
5. Tree is now a heap again We need to make at
most h interchanges of a root of a subtree with
one of its children to restore the heap
property. Thus deletion from a heap is O(h) or
O(log n).
12
Addition to a Heap
Follow the reverse procedure Place it in the next
leaf position and move it up
T
S
P
G
R
O
N
A
M
C
I
A
E
We need O(h) or O(log n) exchanges
13
Storage of complete trees
  • Use n sequential locations in an array
  • Number the nodes from 1 at the root
  • and place
  • The left child of node k is at 2k
  • The right child of node k is at 2k1

1
2
3
4
5
6
7
11
13
10
12
8
9
Viewed as an array, we can see that the nth node
is always in index position n
Delete extract the root, swap the last item to
its place, and call MoveDown recursively Insertion
similar and use MoveUp recursively
Write a Comment
User Comments (0)
About PowerShow.com