FULL UNIT 5 CSE 205
FULL UNIT 5 CSE 205
FULL UNIT 5 CSE 205
➢Insertion
➢Deletion
Balance Binary Search Tree
Worst case height of binary search tree: N
Insertion, deletion can be O(N) in the worst case
We want a tree with small height
Height of a binary tree with N node is at least (log N)
Goal: keep the height of a binary search tree O(log N)
Balanced binary search trees
Examples: AVL tree, red-black tree
Balanced Tree?
Suggestion 1: the left and right subtrees of root have the
same height
Suggestion 2: every node must have left and right
subtrees of the same height
Our choice: for each node, the height of the left and
right subtrees can differ at most 1,-1,0.
AVL Tree
An AVL (Adelson-Velskii and Landis 1962) tree is a
binary search tree in which
for every node in the tree, the height of the left and
right subtrees differ by at most 1.
N1 = 1 N2 = 2 N3 =4 N4 = N2+N3+1=7
Height of AVL Tree
Denote Nh the minimum number of nodes in an AVL tree
of height h
N1=1, N2 =2 (base)
Nh= Nh-1 + Nh-2 +1 (recursive relation)
6 8
6
Insert 6
Original AVL tree Restore AVL property
Property violated
Some Observations
After an insertion, only nodes that are on the path from
the insertion point to the root might have their balance
altered
Because only those nodes have their subtrees altered
Rebalance the tree at the deepest such node guarantees
that the entire tree satisfies the AVL property
6 8
6
Node 5,8,7 might Rebalance node 7
have balance altered guarantees the whole tree be AVL
Different Rebalancing Rotations
The rebalancing rotations are classified as LL, LR, RR
and RL as illustrated below, based on the position of the
inserted node with reference to α.
LL rotation: Inserted node is in the left subtree of the left
subtree of node α
RR rotation: Inserted node is in the right subtree of the
right subtree of node α
LR rotation: Inserted node is in the right subtree of the
left subtree of node α
RL rotation: Inserted node is in the left subtree of the
right subtree of node α
Insertion Analysis
Insert the new key as a new leaf just as in ordinary
binary search tree: O(logN)
Then trace the path from the new leaf towards the
root, for each node x encountered: O(logN)
Check height difference: O(1)
If satisfies AVL property, proceed to next node: O(1)
If not, perform a rotation: O(1)
The insertion stops when
A single rotation is performed logN
Or, we’ve checked all nodes in the path
Time complexity for insertion O(logN)
Deletion from AVL Tree
Basically follows deletion strategy of binary search tree
But may cause violation of AVL tree property
Restore the destroyed balance condition if needed
20 20
10 35 15 35
5 15 25 40 10 18 25 40
18 30 38 45 30 38 45
50 50
15 35 20 40
10 18 25 40 15 25 38 45
30 38 45 10 18 30 50
E e r i space
y s n a r l k .
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What is the frequency of each character in the text?
•CS 102
Building a Tree
Prioritize characters
Uses binary tree nodes
public class HuffNode
{
public char myChar;
public int myFrequency;
public HuffNode myLeft, myRight;
}
priorityQueue myQueue;
Building a Tree
The queue after inserting all nodes
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
•CS 102
Building a Tree
While priority queue contains two or more nodes
Create new node
Dequeue node and make it left subtree
Dequeue next node and make it right subtree
Frequency of new node equals sum of frequency of
left and right children
Enqueue new node back into queue
Building a Tree
E i y l k . r s n a sp e
1 1 1 1 1 1 2 2 2 2 4 8
Building a Tree
y l k . r s n a sp e
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
y l k . r s n a sp e
2
1 1 1 1 2 2 2 2 4 8
E i
1 1
Building a Tree
k . r s n a sp e
2
1 1 2 2 2 2 4 8
E i
1 1
y l
1 1
Building a Tree
2
k . r s n a 2 sp e
1 1 2 2 2 2 4 8
y l
E i 1 1
1 1
Building a Tree
r s n a 2 2 sp e
2 2 2 2 4 8
y l
E i 1 1
1 1
k .
1 1
Building a Tree
r s n a 2 2 sp e
2
2 2 2 2 4 8
E i y l k .
1 1 1 1 1 1
Building a Tree
n a 2 sp e
2 2
2 2 4 8
E i y l k .
1 1 1 1 1 1
r s
2 2
Building a Tree
n a 2 sp e
2 2 4
2 2 4 8
E i y l k . r s
1 1 1 1 1 1 2 2
Building a Tree
2 4 e
2 2 sp
8
4
y l k . r s
E i 1 1 1 1 2 2
1 1
n a
2 2
Building a Tree
2 4 4 e
2 2 sp
8
4
y l k . r s n a
E i 1 1 1 1 2 2 2 2
1 1
Building a Tree
4 4 e
2 sp
8
4
k . r s n a
1 1 2 2 2 2
2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
2 sp e
4 2 2 8
k . r s n a
1 1 2 2 2 2
E i y l
1 1 1 1
Building a Tree
4 4 4
e
2 2 8
r s n a
2 2 2 2
E i y l
1 1 1 1
2 sp
4
k .
1 1
Building a Tree
4 4 4 6 e
2 sp 8
r s n a 2 2
4
2 2 2 2 k .
E i y l 1 1
1 1 1 1
4 4
r s n a
2 2 2 2
Building a Tree
4 6 e 8
2 2 2 8
sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
8
e
8
4 4
10
r s n a
2 2 2 2 4
6
2 2 2 sp
4
E i y l k .
1 1 1 1 1 1
Building a Tree
8 10
e
8 4
4 4
6
2 2
r s n a 2 sp
2 2 2 2 4
E i y l k .
1 1 1 1 1 1
Building a Tree
10
16
4
6
2 2 e 8
2 sp 8
4
E i y l k . 4 4
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
10 16
4
6
e 8
2 2 8
2 sp
4 4 4
E i y l k .
1 1 1 1 1 1
r s n a
2 2 2 2
Building a Tree
26
16
10
4 e 8
6 8
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree •After
enqueueing
26 this node
there is only
16
10 one node left
4 e 8
in priority
6 8 queue.
2 2 2 sp 4 4
4
E i y l k .
1 1 1 1 1 1 r s n a
2 2 2 2
Building a Tree
Dequeue the single node
left in the queue.
26
16
This tree contains the 10
new code words for each
4 e 8
character. 6 8
2 2 2 sp 4 4
4
Frequency of root node E i y l k .
1 1 1 1 1 1 r s n a
should equal number of 2 2 2 2
characters in text.
Eerie eyes seen near lake. 26 characters
Encoding the File
Traverse Tree for Codes
Perform a traversal of the
tree to obtain new code
words 26
0 go left 16
10
1 go right
4 e 8
6 8
0000101100000110011 2 2 2 sp 4 4
1000101011011010011 4
E i y l k .
1110101111110001100 1 1 1 1 1 1 r s n a
2 2 2 2
1111110100100101
Summary
Huffman coding is a technique used to compress
files for transmission
Uses statistical coding
more frequently used symbols have shorter code words
Works well for text and fax transmissions
An application that uses several data structures
Thank You
➢insertion,
➢deletion,
➢sorting and
➢complexity analysis
Heap
❑ A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
❑ A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
❑ Operations on heaps
❑ creation of an empty heap
❑ insertion of a new element into the heap;
❑ deletion of the largest element from the heap
2
Figure : Sample max heaps
Property:
The root of max heap (min heap) contains
the largest (smallest).
Figure : Sample min heaps
20 20 21
15 2 15 5 15 20
14 10 14 10 2 14 10 2
initial location of new node insert 5 into heap insert 21 into heap
5
Insertion into a Max Heap
INSHEAP(TREE, N, ITEM)
1. Set N=N+1 and PTR=N.
2. Repeat Steps 3 to 6 while PTR>1.
3. Set PAR= └PTR/2┘
4. If ITEM<= TREE[PAR], then:
Set TREE[PTR]=ITEM, and Return.
[End of If structure]
5. Set TREE[PTR]=TREE[PAR]
6. Set PTR=PAR.
[End of step 2]
7. Set TREE[1]=ITEM
8. Return.
Example of Deletion from Max Heap
remove
20 10 15
15 2 15 2 14 2
14 10 14 10
7
Deletion from a Max Heap
DELHEAP(TREE, N, ITEM)
1. Set ITEM= TREE[1].
2. Set LAST= TREE[N] and N= N-1.
3. Set PTR=1, LEFT=2 and RIGHT=3.
4. Repeat Steps 5 to 7 while RIGHT<=N:
5. If LAST>=TREE[LEFT] and LAST>= TREE[RIGHT], then:
Set TREE[PTR]=LAST and Return.
[End of If structure.]
6. If TREE[RIGHT]<= TREE[LEFT], then:
Set TREE[PTR]= TREE[LEFT] and PTR=LEFT.
Else:
Set TREE[PTR]= TREE[RIGHT] and PTR=RIGHT.
[End of If structure.]
7. Set LEFT=2*PTR and RIGHT=LEFT+1.
[End of Step 4 loop.]
8. If LEFT=N and if LAST < TREE[LEFT] set TREE[PTR]= TREE[LEFT]
Set PTR=LEFT.
9. Set TREE[PTR]= LAST.
10. Return.
HeapSort
HEAPSORT(A, N)
1. Repeat for J=1 to N-1:
Call INSHEAP(A, J, A[J+1]).
[End of loop.]
2. Repeat while N>1:
a) Call DELHEAP(A, N, ITEM).
b) A[N+1]=ITEM.
[End of loop.]
3. Exit.
Thank You