Title: AVL Tree and Heap
1AVL Tree and Heap
- Data Structures
- Rutgers University
2Binary 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
3Height 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
4AVL 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.
5Examples
6Examples
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.
7Height Imbalance During Insertion or Deletion
8Rotation
- Locally rearrange the structure of an AVL tree
9Heaps
- 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
10Heaps
- 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.
11Deletion 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).
12Addition 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
13Storage 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