CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
COL 106 Shweta Agrawal and Amit Kumar
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CS 206 Introduction to Computer Science II 03 / 23 / 2009 Instructor: Michael Eckmann.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 15 Trees and Tree Traversals Instructor: Prof. Zhigang Zhu Department of Computer Science City College of New.
Trees II Kruse and Ryba Ch 10.1,10.2,10.4 and 11.3.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CS 206 Introduction to Computer Science II 11 / 04 / 2009 Instructor: Michael Eckmann.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Trees Main and Savitch Chapter 10. Binary Trees A binary tree has nodes, similar to nodes in a linked list structure. Data of one sort or another may.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 17 B-Trees and the Set Class Instructor: Zhigang Zhu Department of Computer Science.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
CSC212 Data Structure - Section AB
CSC212 Data Structure - Section AB
Using a Stack Chapter 6 introduces the stack data type.
i206: Lecture 14: Heaps, Graphs intro.
Binary Search Trees One of the tree applications in Chapter 10 is binary search trees. In Chapter 10, binary search trees are used to implement bags.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
B-Trees This presentation shows you the potential problem of unbalanced tree and show one way to fix it This lecture introduces heaps, which are used.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Heaps.
Presentation transcript:

CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College

p BST and B-Tree p The BST rules and The B-Tree Rules p Search for an Item in a BST and B-Tree p Insert an Item in BST and a B-Tree (*) p Remove an Item from a BST and B-Tree (*) Review: BST & B-Tree

Example: Is it a BST? ¶How to search an item? ¶Iowa ¶How to add an item? ¶Vermont ¶How to remove an item? ¶Florida Arizona Arkansas Washington Oklahoma Colorado Florida West Virginia Mass. New Hampshire Iowa

Examples: is it a B-Tree? 2 and and The 6 B-tree rules:  Entries in a node (3 rules): (minimum, maximum, storage )  Subtrees: (2 rules) (the number of subtrees, entry at index i and entries at subtree I or subtree i+1 )  Tree-leaves: (1 rule) (same depth)

p p Chapter 11 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates two of the important heap algorithms. Heaps Data Structures and Other Objects Using C++

Topics p Heap Definition p Heap Applications p priority queues (chapter 8), sorting (chapter 13) p Two Heap Operations – add, remove p reheapification upward and downward p why is a heap good for implementing a priority queue? p Heap Implementation p using binary_tree_node class p using fixed size or dynamic arrays

Heaps Definition A heap is a certain kind of complete binary tree.

Heaps A heap is a certain kind of complete binary tree. When a complete binary tree is built, its first node must be the root. When a complete binary tree is built, its first node must be the root. Root

Heaps Complete binary tree. Left child of the root The second node is always the left child of the root. The second node is always the left child of the root.

Heaps Complete binary tree. Right child of the root The third node is always the right child of the root. The third node is always the right child of the root.

Heaps Complete binary tree. The next nodes always fill the next. level from left-to-right. The next nodes always fill the next. level from left-to-right.

Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

Heaps Complete binary tree. The next nodes always fill the next level from left-to-right. The next nodes always fill the next level from left-to-right.

Heaps Complete binary tree.

Heaps A heap is a certain kind of complete binary tree. Each node in a heap contains a key that can be compared to other nodes' keys. Each node in a heap contains a key that can be compared to other nodes' keys

Heaps A heap is a certain kind of complete binary tree. The "heap property" requires that each node's key is >= the keys of its children The "heap property" requires that each node's key is >= the keys of its children

What it is not: It is not a BST p In a binary search tree, the entries of the nodes can be compared with a strict weak ordering. Two rules are followed for every node n: p The entry in node n is NEVER less than an entry in its left subtree p The entry in the node n is less than every entry in its right subtree. p BST is not necessarily a complete tree

What it is: Heap Definition p A heap is a binary tree where the entries of the nodes can be compared with the less than operator of a strict weak ordering. In addition, two rules are followed: p The entry contained by the node is NEVER less than the entries of the node’s children p The tree is a COMPLETE tree. p Q: where is the largest entry?  for what....

Application : Priority Queues p A priority queue is a container class that allows entries to be retrieved according to some specific priority levels p The highest priority entry is removed first p If there are several entries with equally high priorities, then the priority queue’s implementation determines which will come out first (e.g. FIFO) p Heap is suitable for a priority queue

The Priority Queue ADT with Heaps p The entry with the highest priority is always at the root node p Focus on two priority queue operations p adding a new entry p remove the entry with the highest priority p In both cases, we must ensure the tree structure remains to be a heap p we are going to work on a conceptual heap without worrying about the precise implementation p later I am going to show you how to implement...

Adding a Node to a Heap ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location

Adding a Node to a Heap 4 4The parent has a key that is >= new node, or 4 4The node reaches the root. Ú reheapification ÚThe process of pushing the new node upward is called reheapification upward Note: Note: we need to easily go from child to parent as well as parent to child.

Removing the Top of a Heap ¶ ¶Move the last node onto the root

Removing the Top of a Heap ¶ ¶Move the last node onto the root

Removing the Top of a Heap ¶ ¶Move the last node onto the root. · ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap ¶ ¶Move the last node onto the root. · ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap ¶ ¶Move the last node onto the root. · ·Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location

Removing the Top of a Heap 4 4The children all have keys <= the out-of-place node, or 4 4The node reaches the leaf. Ø reheapification ØThe process of pushing the new node downward is called reheapification downward

Priority Queues Revisited p A priority queue is a container class that allows entries to be retrieved according to some specific priority levels p The highest priority entry is removed first p If there are several entries with equally high priorities, then the priority queue’s implementation determines which will come out first (e.g. FIFO) p Heap is suitable for a priority queue

Adding a Node: same priority ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location *

Adding a Node : same priority ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location *

Adding a Node : same priority ¶ ¶Put the new node in the next available spot. · ·Push the new node upward, swapping with its parent until the new node reaches an acceptable location * 27

Adding a Node : same priority 4 4The parent has a key that is >= new node, or 4 4The node reaches the root. Ú reheapification ÚThe process of pushing the new node upward is called reheapification upward * 27 Note: Implementation determines which 45 will be in the root, and will come out first when popping.

Removing the Top of a Heap 4 4The children all have keys <= the out-of-place node, or 4 4The node reaches the leaf. Ø reheapification ØThe process of pushing the new node downward is called reheapification downward * 35 Note: Implementation determines which 45 will be in the root, and will come out first when popping.

Heap Implementation (how?) p Use binary_tree_node class p node implementation is for a general binary tree p but we may need to have doubly linked node p Use arrays (page 475) p A heap is a complete binary tree p which can be implemented more easily with an array than with the node class p and do two-way links

Implementing a Heap p We will store the data from the nodes in a partially-filled array. An array of data

Implementing a Heap p Data from the root goes in the first location of the array. An array of data

Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data

Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data

Implementing a Heap p Data from the next row goesin the p Data from the next row goes in the next two array locations. An array of data We don't care what's in this part of the array.

Important Points about the Implementation p The links between the tree's nodes are not actually stored as pointers, or in any other way. p The only way we "know" that "the array is a tree" is from the way we manipulate the data. An array of data

Important Points about the Implementation p If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. Formulas are given in the book. [0] [0] [1] [2] [3] [4]

Formulas for location children and parents in an array representation p Root at location [?] p Parent of the node in [i] is at [?] p Children of the node in [i] (if exist) is at [ ? ] and [ ? ]

Formulas for location children and parents in an array representation p Root at location [0] p Parent of the node in [i] is at [(i-1)/2] p Children of the node in [i] (if exist) is at [2i+1] and [2i+2] p Test: p complete tree of 10, 000 nodes p parent of 4999 is at p children of 4999 is at _____ and _____

Can you implement the heap class now? p Private variables: p Public functions:

Solutions... template class heap { public: heap () void push(const Item& entry); // add Item& pop(); // remove the highest size_t parent (size_t k) const ; size_t l_child (size_t k) const ; size_t r_child (size_t k) const ; private: Item data[CAPACITY]; size_type used; }

Solutions... template class heap { public: heap () { used = 0;} void push(const Item& entry); // add Item& pop(); // remove the highest size_t parent (size_t k) const { return (k-1)/2;} size_t l_child (size_t k) const { return 2*k+1;} size_t r_child (size_t k) const { return 2*k+2;} private: Item data[CAPACITY]; size_type used; }

Solutions...more p Can you implement the add and remove with the knowledge of these formulas? p Add p Remove template class heap { public: heap () { used = 0;} void push(const Item& entry); // add Item& pop(); // remove the highest size_t parent (size_t k) const { return (k-1)/2;} size_t l_child (size_t k) const { return 2*k+1;} size_t r_child (size_t k) const { return 2*k+2;} private: Item data[CAPACITY]; size_type used; }

Solutions...more p Can you implement the add and remove with the knowledge of these formulas? p Add p put the new entry in the last location p p Push the new node upward, swapping with its parent until the new node reaches an acceptable location p Remove p move the last node to the root p p Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location template class heap { public: heap () { used = 0;} void push(const Item& entry); // add Item& pop(); // remove the highest size_t parent (size_t k) const { return (k-1)/2;} size_t l_child (size_t k) const { return 2*k+1;} size_t r_child (size_t k) const { return 2*k+2;} private: Item data[CAPACITY]; size_type used; }

template void heap ::push(const Item& entry) { size_type i; data[used++] = entry; i = used –1; while ( ( i != 0 && (data[i] > data[( i-1)/2]) { swap(data[i], data[( i-1)/2]); i = ( i-1)/2; }

p p A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children. p p To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward. p p To remove the biggest entry, move the last node onto the root, and perform a reheapification downward. Summary

T HE E ND Presentation copyright 1997 Addison Wesley Longman, For use with Data Structures and Other Objects Using C++ by Michael Main and Walter Savitch. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using C++ are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.