Download presentation
Presentation is loading. Please wait.
Published byCharlotte Peters Modified over 9 years ago
1
Foundation of Computing Systems Lecture 6 Trees: Part III
2
05.08.09IT 60101: Lecture #62 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.
3
05.08.09IT 60101: Lecture #63 Heap Trees: Example
4
05.08.09IT 60101: Lecture #64 Array Representation of Heap Trees
5
05.08.09IT 60101: Lecture #65 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
6
05.08.09IT 60101: Lecture #66 Heap Trees: Insertion A trivial case
7
05.08.09IT 60101: Lecture #67 Heap Trees: Insertion A non-trivial case
8
05.08.09IT 60101: Lecture #68 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.
9
05.08.09IT 60101: Lecture #69 Heap Trees: Deletion
10
05.08.09IT 60101: Lecture #610 Heap Trees: Deletion
11
05.08.09IT 60101: Lecture #611 Heap Trees: Deletion
12
05.08.09IT 60101: Lecture #612 Heap Trees: Deletion
13
05.08.09IT 60101: Lecture #613 Heap Trees: Merging
14
05.08.09IT 60101: Lecture #614 Application of Heap Trees Sorting Priority queue
15
05.08.09IT 60101: Lecture #615 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.
16
05.08.09IT 60101: Lecture #616 Sorting with Heap Tree: Example 33, 14, 65, 02, 76, 69, 59, 85, 47, 99, 98 Building (max) heap tree from the given set of data
17
05.08.09IT 60101: Lecture #617 Sorting with Heap Tree: Example Swapping the root and the last node
18
05.08.09IT 60101: Lecture #618 Sorting with Heap Tree: Example Rebuild the heap tree
19
05.08.09IT 60101: Lecture #619 Sorting with Heap Tree: Example Continuation of Step 2 after selecting 98
20
05.08.09IT 60101: Lecture #620 Sorting with Heap Tree: Example Sorted list when the heap is empty
21
05.08.09IT 60101: Lecture #621 Priority Queue with Heap Tree Consider the following processes with their priorities: Process P 1 P 2 P 3 P 4 P 5 P 6 P 7 P 8 P 9 P 10 Priority 5 4 3 4 5 5 3 2 1 5 Order of arrivals of processes are: P 1 P 5 P 6 P 10 P 2 P 4 P 3 P 7 P 8 P 9
22
05.08.09IT 60101: Lecture #622 Priority Queue with Heap Tree
23
05.08.09IT 60101: Lecture #623 Threaded Binary Trees In a linked representation of binary trees with n (n > 0) nodes, n + 1 link fields contain null entries.
24
05.08.09IT 60101: Lecture #624 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.
25
05.08.09IT 60101: Lecture #625 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
26
05.08.09IT 60101: Lecture #626 Threaded Binary Trees: Example Inorder threading of a binary tree
27
05.08.09IT 60101: Lecture #627 Threaded Binary Trees: Example
28
05.08.09IT 60101: Lecture #628 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.
29
05.08.09IT 60101: Lecture #629 Operations on Threaded Binary Tree For various operations on threaded binary trees see the book Classic Data Structures Chapter 7 PHI, 2nd Edn., 17th Reprint
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.