Upload
charlotte-peters
View
213
Download
0
Embed Size (px)
Citation preview
Foundation of Computing Systems
Lecture 6
Trees: Part III
05.08.09 IT 60101: Lecture #6 2
Heap Trees
• A heap tree, say H, is a complete binary tree. H will be termed as heap tree, if it satisfies the following properties:
• (i) For each node N in H, the value at N is greater than or equal to the value of each of the children of N.
• (ii) Or in other words, N has the value which is greater than or equal to the value of every successor of N.
• Such a heap tree is called max heap.
• Similarly, min heap is possible, where any node N has the value less than or equal to the value of any of the successors of N.
05.08.09 IT 60101: Lecture #6 3
Heap Trees: Example
95
85 45
75
55 65
2535 15
(a ) M ax heap
15
45 25
55
85 95
6535 75
(b ) M in heap
05.08.09 IT 60101: Lecture #6 4
Array Representation of Heap Trees
1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 45 25 55 65 35 75 85 95 . . . . . .
15
45 25
55
85 95
6535 75
(b ) M in heap
05.08.09 IT 60101: Lecture #6 5
Operations on Heap Trees
• Operations important to heap is
• Insertion of a node
• Deletion of a node
• Merging two heaps
• Other operations are same as in general binary trees
05.08.09 IT 60101: Lecture #6 6
Heap Trees: Insertion
95
85 45
75 25 35 15
55 65 19
(b ) Inc lus ion o f 19 in the fash ion o f com p le teb ina ry tree and it sa tis fy the p roperty o f heap
95
85 45
75
55 65
25 35 15
(a ) M ax heap
A trivial case
05.08.09 IT 60101: Lecture #6 7
Heap Trees: Insertion
A non-trivial case
95
85 45
75 25 15
65 11155 19
111
111 95
11185
25
35
111
95 45
75 85 35 15
55 65 19 25
W hen 111 is inse rted in to the heap tree
05.08.09 IT 60101: Lecture #6 8
Heap Trees: Deletion
• Any node can be deleted from a heap tree. But from the application point of view, deleting the root node has some special importance.
• Copy the root node into a temporary storage, say ITEM.
• Replace the root node by the last node in the heap tree. Then reheap the tree as stated below:
– Let newly modified root node be the current node. Compare its value with the value of its two child. Let X be the child whose value is the largest. Interchange the value of X with the value of the current node.
– Make X as the current node.
• Continue reheap, if the current node is not an empty node.
05.08.09 IT 60101: Lecture #6 9
Heap Trees: Deletion
45 63
27 12
293542
24
26
57
H eap tree after repalc ing 99 by 26
99
45 63
27 12
293542
24
26
57
26
D ele ting the node w ith data 99
05.08.09 IT 60101: Lecture #6 10
Heap Trees: Deletion
45
63
27 12
293542
24
26
57
H eap tree a fter rebu ild
45 63
27 12
293542
24
63
26
57
R ebuild ing a fter ad justing 63
05.08.09 IT 60101: Lecture #6 11
Heap Trees: Deletion
45
63
27 12
293542
24
26
57
H eap tree a fter rebu ild
45
27 12
293542
24
63
2657
5726
R ebuild the tree a t 26
05.08.09 IT 60101: Lecture #6 12
Heap Trees: Deletion
45
63
57
26 4235 29
27 12 24
A fter de le tion o f 99
99
45 63
27 12
293542
24
26 63
2657
57
26
26
D ele ting the node w ith data 99
05.08.09 IT 60101: Lecture #6 13
Heap Trees: Merging
92
45 67
5938
19
13
80
9692 93
96
93
67
80 9213 19
67
4538 59
H : A m ax heap H : A m in heap
R esultant m ax heap after m erg ing H and H
+1 2
1 2
05.08.09 IT 60101: Lecture #6 14
Application of Heap Trees
• Sorting
• Priority queue
05.08.09 IT 60101: Lecture #6 15
Application of Heap Trees: Sorting
• Step 1: Build a heap tree with the given set of data.
• Step 2:• (a) Delete the root node from the heap.
• (b) Rebuild the heap after the deletion.
• (c) Place the deleted node in the output.
• Step 3: Continue Step 2 until the heap tree is empty.
05.08.09 IT 60101: Lecture #6 16
Sorting with Heap Tree: Example
33, 14, 65, 02, 76, 69, 59, 85, 47, 99, 98
99
98 96
65 33 59
47 76
85
02 14
99 98 69 65 85 33 59 02 47 14 76
Building (max) heap tree from the given set of data
05.08.09 IT 60101: Lecture #6 17
Sorting with Heap Tree: Example
99
98 96
65 33 59
47 76
85
02 14
99 98 69 65 85 33 59 02 47 14 76
76
98 69
65 85 33 59
02 47 14 99
9976 98 69 65 85 33 59 02 47 14
Swapping the root and the last node
05.08.09 IT 60101: Lecture #6 18
Sorting with Heap Tree: Example
76
98 69
65 85 33 59
02 47 14 99
9976 98 69 65 85 33 59 02 47 14
98
85 69
65 76 33 59
02 47 14
9998 85 69 65 76 33 59 02 47 14
Rebuild the heap tree
05.08.09 IT 60101: Lecture #6 19
Sorting with Heap Tree: Example
98
85 69
65 76 33 59
02 47 14
9998 85 69 65 76 33 59 02 47 14
85
76 69
65 14 33 59
02 47
9985 76 69 65 14 33 59 02 47 98
Continuation of Step 2 after selecting 98
05.08.09 IT 60101: Lecture #6 20
Sorting with Heap Tree: Example
02 14 33 47 59 65 69 76 85 98 99
Sorted list when the heap is empty
05.08.09 IT 60101: Lecture #6 21
Priority Queue with Heap Tree
Consider the following processes with their priorities:
Process P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 Priority 5 4 3 4 5 5 3 2 1 5
Order of arrivals of processes are:
P1 P5 P6 P10 P2 P4 P3 P7 P8 P9
05.08.09 IT 60101: Lecture #6 22
Priority Queue with Heap Tree
P(5)
P(5) P (5)
P(4) P (5) P(3) P(3)
P(2) P(1) P(4)
1
2
34
5 6
7
8 9
10
(a ) P rio rity queue heap (subsc rip t ind ica tes o rde ro f p rocess w he reas num ber in pa ran theses
m eans its p rio rity)
P(5)
P(5) P (5)
P(4) P (4) P(3) P(3)
P(2) P(1)
1
2 34
5
6
7
8 9
10
(b ) A fte r the rem ova l o f P
05.08.09 IT 60101: Lecture #6 23
Threaded Binary Trees
• In a linked representation of binary trees with n (n > 0) nodes, n + 1 link fields contain null entries.
+
- *
A B C /
D E
05.08.09 IT 60101: Lecture #6 24
Threaded Binary Trees
• Issues:• How a threaded binary tree will be represented
and how can we distinguish links from threads.
• What advantages can be obtained from threaded binary trees.
• How the different operations like insertion, deletions, traversals, etc., can be carried out on it.
05.08.09 IT 60101: Lecture #6 25
Threaded Binary Trees
• Issues:• How a threaded binary tree will be represented
and how can we distinguish links from threads.
– Threading according to a tree traversal» Inorder Inorder threading» Preorder Preorder threading» Postorder Postorder threading
– Another a bit field is required to distinguish a thread and link
05.08.09 IT 60101: Lecture #6 26
Threaded Binary Trees: Example
A
B C
D
E F
G
Inorder T raversa l : B A G E D F C
Inorder threading of a binary tree
05.08.09 IT 60101: Lecture #6 27
Threaded Binary Trees: Example
A
B C
D
E F
G
LCHILD LTAG DATA RTAG RCHILD
05.08.09 IT 60101: Lecture #6 28
Advantages of a Threaded Binary Tree
• The traversal operation is more faster than that of its unthreaded version.
• Efficiently determine the predecessor and successor nodes starting from any node.
• Any node can be accessible from any other node.
• Insertions into and deletions from a threaded tree are although time consuming operations (since we have to manipulate both links and threads) but these are very easy to implement.
05.08.09 IT 60101: Lecture #6 29
Operations on Threaded Binary Tree
For various operations on threaded binary trees see the book
Classic Data StructuresChapter 7
PHI, 2nd Edn., 17th Reprint