Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree

Similar presentations


Presentation on theme: "CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree"— Presentation transcript:

1 CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Heap Priority Queue Inserting into Heap Removing from Heap Heap Sort 10/30/2019 CS210-Summer 2005, Lecture 13

2 Heaps An efficient realization of a priority queue uses a data structure called a heap. Heap allows us to perform both insertions and removals in logarithmic time. Heap stores entries in a binary tree rather than in a list. 10/30/2019 CS210-Summer 2005, Lecture 13

3 Heaps A heap is a binary tree storing entries at its nodes and satisfying the following properties: Heap-Order: for every internal node v other than the root, key(v)  key(parent(v)) Complete Binary Tree: Heap must be complete binary tree. (2, C) (5, A) (6, Z) (15,K) (9, F) last node 10/30/2019 CS210-Summer 2005, Lecture 13

4 Heap Order Property For every internal node v other than the root, key(v)  key(parent(v)) As a consequence of the heap order property, the keys encountered on a path from the root to an external node of T are in nondecreasing order. Minimum key is always stored at the root. 10/30/2019 CS210-Summer 2005, Lecture 13

5 Structural Property For efficiency, we want the heap T to have as small a height as possible. We enforce this by saying that T must be complete. A binary tree is called complete if all levels are completely filled, except possibly the last level, which is filled from left to right. The last node of a heap is the rightmost node at last level. 10/30/2019 CS210-Summer 2005, Lecture 13

6 Height of a Heap Theorem: A heap storing n entries has height O(log n)
Proof: (we apply the complete binary tree property) Let h be the height of a heap storing n keys Since there are 2i keys at depth i = 0, … , h - 1 and at least one key at depth h, we have n  … + 2h Thus, n  2h , i.e., h  log n depth keys 1 1 2 h-1 2h-1 h 1 10/30/2019 CS210-Summer 2005, Lecture 13

7 Complete Binary Trees and Their Representation
The Complete BinaryTree ADT: A complete binary Tree T supports all the methods of binary tree ADT plus the following two methods: add(o): Add to T and return a new external node v storing element o such that the resulting tree is a complete binary tree with last node v. remove(): Remove the last node of T and return its element. 10/30/2019 CS210-Summer 2005, Lecture 13

8 Adding to Binary Tree If the bottom level of T is not full, then add inserts a new node on the bottom level of T, immediately after the rightmost node of this level. (height remains same) If the bottom level is full, then add inserts a new node as the left child of the leftmost node of the bottom level of T (height increases by one). 10/30/2019 CS210-Summer 2005, Lecture 13

9 A Vector based representation of a complete binary tree
A Simple structure for representing a binary tree T is based on a way of numbering the nodes of T. For every node v of T, let p(v) be the integer defined as follows: If v is the root of T, then p(v) = 1 If v is the left child of node u, then p(v) = 2p(u). If v is the right child of node u, then p(v) = 2p(u) + 1. 10/30/2019 CS210-Summer 2005, Lecture 13

10 A Vector based representation of a complete binary tree
With this implementation, the nodes of T have contiguous ranks in the range [1.n] and the last node of T has always rank n, where is the number of nodes of T. 10/30/2019 CS210-Summer 2005, Lecture 13

11 A Vector based structure of a Complete Binary Tree
1 2 3 7 5 6 4 13 14 15 9 10 11 12 8 10/30/2019 CS210-Summer 2005, Lecture 13

12 A Vector based structure of a Complete Binary Tree
1 3 2 7 5 6 4 13 14 15 9 10 11 12 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 10/30/2019 CS210-Summer 2005, Lecture 13

13 Heaps and Priority Queues
We can use a heap to implement a priority queue We store a (key, element) item at each node. We keep track of the position of the last node (2, Sue) (5, Pat) (6, Mark) (9, Jeff) (7, Anna) 10/30/2019 CS210-Summer 2005, Lecture 13

14 Insertion into a Heap (2, C) Method insert of the priority queue ADT corresponds to the insertion of an entry (k,x) to the heap The insertion algorithm consists of three steps Add a new node z (the new last node) Store e at z Restore the heap-order property (discussed next) (5, A) (6, Z) z (15,K) (9, F) insertion node (2, C) (5, A) (6, Z) z (15,K) (9, F) (1, D) 10/30/2019 CS210-Summer 2005, Lecture 13

15 Upheap After the insertion of a new entry e, the heap-order property may be violated Algorithm upheap restores the heap-order property by swapping e along an upward path from the insertion node Upheap terminates when the entry e reaches the root or a node whose parent has a key smaller than or equal to key k of e. Since a heap has height O(log n), upheap runs in O(log n) time (2, C) (1, D) (5, A) (1,D) (5, A) (2, C) z z (15,K) (9, F) (6, Z) (9, F) (6, Z) (15,K) 10/30/2019 CS210-Summer 2005, Lecture 13

16 Removal from a Heap Method removeMin of the priority queue ADT corresponds to the removal of the root entry from the heap The removal algorithm consists of three steps Replace the root entry with the entry of the last node w Remove w Restore the heap-order property (discussed next) (2, C) (5, A) (6, Z) w (15,K) (9, F) last node (9, F) (5, A) (6, Z) w (15,K) new last node 10/30/2019 CS210-Summer 2005, Lecture 13

17 Downheap After replacing the root entry with the entry e of the last node, the heap-order property may be violated Algorithm downheap restores the heap-order property by swapping entry e along a downward path from the root Upheap terminates when key k of entry e reaches a leaf or a node whose children have keys greater than or equal to k Since a heap has height O(log n), downheap runs in O(log n) time (9, F) (5, A) (5, A) (6, Z) (9, F) (6, Z) w w (15,K) (15,K) 10/30/2019 CS210-Summer 2005, Lecture 13

18 Heap-Sort Consider a priority queue with n items implemented by means of a heap methods insert and removeMin take O(log n) time methods size, isEmpty, and min take time O(1) time Using a heap-based priority queue, we can sort a sequence of n elements in O(n log n) time The resulting algorithm is called heap-sort Heap-sort is much faster than quadratic sorting algorithms, such as insertion-sort and selection-sort 10/30/2019 CS210-Summer 2005, Lecture 13


Download ppt "CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree"

Similar presentations


Ads by Google