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

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of.
Advertisements

Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Data Structures Lecture 7 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
The Priority Queue Abstract Data Type. Heaps. Adaptable Priority Queue. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich,
CSC401 – Analysis of Algorithms Lecture Notes 5 Heaps and Hash Tables Objectives: Introduce Heaps, Heap-sorting, and Heap- construction Analyze the performance.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
© 2004 Goodrich, Tamassia Priority Queues1 Heaps: Tree-based Implementation of a Priority Queue.
Chapter 8: Priority Queues
© 2004 Goodrich, Tamassia Heaps © 2004 Goodrich, Tamassia Heaps2 Priority Queue Sorting (§ 8.1.4) We can use a priority queue to sort a set.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Priority Queues1 Part-D1 Priority Queues. Priority Queues2 Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is.
Chapter 8: Priority Queues and Heaps Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided.
1 Priority Queues CPS212 Gordon College VIP. 2 Introduction to STL Priority Queues Adaptor container - underlying container may be either: – a template.
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heaps and Priority Queues Priority Queue ADT (§ 2.4.1) A priority queue stores a collection of items An item is a pair (key, element) Main.
Chapter 21 Binary Heap.
Priority Queues & Heaps Chapter 9. Iterable Collection Abstract Collection Queue List Abstract Queue Priority Queue Array List Abstract List Vector Stack.
CSC 213 – Large Scale Programming Lecture 15: Heap-based Priority Queue.
PRIORITY QUEUES AND HEAPS CS16: Introduction to Data Structures & Algorithms Tuesday, February 24,
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Priority Queues and Heaps. Outline and Reading PriorityQueue ADT (§8.1) Total order relation (§8.1.1) Comparator ADT (§8.1.2) Sorting with a Priority.
Chapter 2.4: Priority Queues and Heaps PriorityQueue ADT (§2.4.1) Total order relation (§2.4.1) Comparator ADT (§2.4.1) Sorting with a priority queue (§2.4.2)
HEAPS • Heaps • Properties of Heaps • HeapSort
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
CPSC 252 Binary Heaps Page 1 Binary Heaps A complete binary tree is a binary tree that satisfies the following properties: - every level, except possibly.
CS 2468: Assignment 2 (Due Week 9, Tuesday. Drop a hard copy in Mail Box 75 or hand in during the lecture) Use array representation (double a[]) to implement.
1 Heaps A heap is a binary tree. A heap is best implemented in sequential representation (using an array). Two important uses of heaps are: –(i) efficient.
Quotes “From each according to his ability, to each according to his needs” -- Karl Marx/Queue ADT “In America, first you get the sugar, then you get the.
Problem of the Day  You are trapped alone in a dark room with:  Candle;  Wood stove; and  Gas lamp (with full tank).  You only have one match; what.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Priority Queues Last Update: Oct 23, 2014 EECS2011: Priority Queues1.
CSC 213 – Large Scale Programming. Priority Queue ADT  Prioritizes Entry s using their keys  For Entry s with equal priorities, order not specified.
1 COMP9024: Data Structures and Algorithms Week Seven: Priority Queues Hui Wu Session 1, 2016
Heaps and Priority Queues What is a heap? A heap is a binary tree storing keys at its internal nodes and satisfying the following properties:
Priority Queues 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees v z Red-Black Trees Red-Black Trees
Heaps (8.3) CSE 2011 Winter May 2018.
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Priority Queues Chuan-Ming Liu
COMP9024: Data Structures and Algorithms
Part-D1 Priority Queues
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
CSCE 3100 Data Structures and Algorithm Analysis
Bohyung Han CSE, POSTECH
Heaps 9/13/2018 3:17 PM Heaps Heaps.
Chapter 2: Basic Data Structures
Heaps and Priority Queues
Priority Queues and Heaps
Part-D1 Priority Queues
Heaps and Priority Queues
Heaps 11/27/ :05 PM Heaps Heaps.
Tree Representation Heap.
Heaps A heap is a binary tree.
Heaps and Priority Queues
© 2013 Goodrich, Tamassia, Goldwasser
Copyright © Aiman Hanna All rights reserved
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Ch. 8 Priority Queues And Heaps
Heaps and Priority Queues
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
Lecture 9 CS2013.
Heaps and Priority Queues
1 Lecture 10 CS2013.
CS210- Lecture 14 July 5, 2005 Agenda Inserting into Heap
Heaps 9/29/2019 5:43 PM Heaps Heaps.
Heaps.
CS210- Lecture 20 July 19, 2005 Agenda Multiway Search Trees 2-4 Trees
Presentation transcript:

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

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

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

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

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

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  1 + 2 + 4 + … + 2h-1 + 1 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

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

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

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

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

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

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

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

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

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

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

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

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