csc211 data structures lecture 18 heaps and priority queues instructor: prof. xiaoyan li department...

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

Upload: hilary-evelyn-burns

Post on 14-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

CSC211 Data Structures CSC211 Data Structures

Lecture 18Lecture 18Heaps and Priority QueuesHeaps and Priority Queues

Instructor: Prof. Xiaoyan LiInstructor: Prof. Xiaoyan LiDepartment of Computer Science Department of Computer Science

Mount Holyoke CollegeMount Holyoke College

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

BST and B-TreeBST and B-Tree The BST rules and The B-Tree RulesThe BST rules and The B-Tree Rules Search for an Item in a BST and B-TreeSearch for an Item in a BST and B-Tree Insert an Item in BST and a B-Tree (*)Insert an Item in BST and a B-Tree (*) Remove an Item from a BST and B-Tree (*)Remove an Item from a BST and B-Tree (*)

Review: BST & B-TreeReview: BST & B-Tree

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

Example: Is it a BST?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

OklahomaColorado

Florida

Wes

tV

irgin

ia

Mass.

New

Ham

pshi

reIowa

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

Examples: is it a B-Tree?Examples: is it a B-Tree?

2 and 42 and 4

66

7 and 87 and 8

99

1010553311

The 6 B-tree rules:The 6 B-tree rules:

Entries in a node (3 rules): (minimum, maximum, storage )Entries in a node (3 rules): (minimum, maximum, storage )

Subtrees: (2 rules) (the number of subtrees, entry at index i Subtrees: (2 rules) (the number of subtrees, entry at index i and entries at subtree I or subtree i+1 )and entries at subtree I or subtree i+1 )

Tree-leaves: (1 rule) (same depth)Tree-leaves: (1 rule) (same depth)

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

Chapter 11 has several programming projects, including a project that uses heaps.

This presentation shows you what a heap is, and demonstrates two of the important heap algorithms.

HeapsHeaps

Data StructuresData Structuresand Other Objectsand Other ObjectsUsing C++Using C++

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

TopicsTopics

Heap DefinitionHeap Definition Heap ApplicationsHeap Applications

priority queues (chapter 8), sorting (chapter 13) priority queues (chapter 8), sorting (chapter 13) Two Heap Operations – add, removeTwo Heap Operations – add, remove

reheapification upward and downwardreheapification upward and downward why is a heap good for implementing a priority queue?why is a heap good for implementing a priority queue?

Heap Implementation Heap Implementation using binary_tree_node classusing binary_tree_node class using fixed size or dynamic arraysusing fixed size or dynamic arrays

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

Heaps DefinitionHeaps Definition

A heap is a certain kind of complete binary tree.

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

HeapsHeaps

A heap is a certain kind of complete binary tree.

When a completebinary tree is built,

its first node must bethe root.

When a completebinary tree is built,

its first node must bethe root.

Root

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

HeapsHeaps

Complete binary tree.

Left childof theroot

The second node isalways the left child

of the root.

The second node isalways the left child

of the root.

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

HeapsHeaps

Complete binary tree.

Right childof the

root

The third node isalways the right child

of the root.

The third node isalways the right child

of the root.

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

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right..

The next nodesalways fill the next

level from left-to-right..

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

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

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

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

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

HeapsHeaps

Complete binary tree.

The next nodesalways fill the next

level from left-to-right.

The next nodesalways fill the next

level from left-to-right.

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

HeapsHeaps

Complete binary tree.

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

HeapsHeaps

A heap is a certain kind of complete binary tree.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

Each node in a heapcontains a key thatcan be compared toother nodes' keys.

19

4222127

23

45

35

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

HeapsHeaps

A heap is a certain kind of complete binary tree.

The "heap property"requires that each

node's key is >= thekeys of its children

The "heap property"requires that each

node's key is >= thekeys of its children

19

4222127

23

45

35

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

What it is not: It is not a BSTWhat it is not: It is not a BST

In a binary search tree, the entries of the In a binary search tree, the entries of the nodes can be compared with a strict weak nodes can be compared with a strict weak ordering. Two rules are followed for every ordering. Two rules are followed for every node n:node n: The entry in node n is NEVER The entry in node n is NEVER less thanless than an an

entry in its left subtreeentry in its left subtree The entry in the node n is The entry in the node n is less thanless than every entry every entry

in its right subtree.in its right subtree. BST is not necessarily a complete treeBST is not necessarily a complete tree

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

What it is: Heap DefinitionWhat it is: Heap Definition

A heap is a binary tree where the entries of A heap is a binary tree where the entries of the nodes can be compared with the the nodes can be compared with the less less thanthan operator of a strict weak ordering. In operator of a strict weak ordering. In addition, two rules are followed:addition, two rules are followed: The entry contained by the node is NEVER The entry contained by the node is NEVER less less

thanthan the entries of the node’s children the entries of the node’s children The tree is a COMPLETE tree.The tree is a COMPLETE tree.

Q: where is the largest entry? Q: where is the largest entry? for what.... for what....

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

Application : Priority QueuesApplication : Priority Queues

A priority queue is a container class that A priority queue is a container class that allows entries to be retrieved according to allows entries to be retrieved according to some specific priority levelssome specific priority levels The highest priority entry is removed firstThe highest priority entry is removed first If there are several entries with equally high If there are several entries with equally high

priorities, then the priority queue’s priorities, then the priority queue’s implementation determines which will come implementation determines which will come out first (e.g. FIFO)out first (e.g. FIFO)

Heap is suitable for a priority queueHeap is suitable for a priority queue

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

The Priority Queue ADT with HeapsThe Priority Queue ADT with Heaps

The entry with the highest priority is always at the The entry with the highest priority is always at the root noderoot node

Focus on two priority queue operationsFocus on two priority queue operations adding a new entryadding a new entry remove the entry with the highest priorityremove the entry with the highest priority

In both cases, we must ensure the tree structure In both cases, we must ensure the tree structure remains to be a heapremains to be a heap we are going to work on a conceptual heap without we are going to work on a conceptual heap without

worrying about the precise implementationworrying about the precise implementation later I am going to show you how to implement...later I am going to show you how to implement...

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

Adding a Node to a HeapAdding 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. 19

4222127

23

45

35

42

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

Adding a Node to a HeapAdding 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. 19

4222142

23

45

35

27

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

Adding a Node to a HeapAdding 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. 19

4222135

23

45

42

27

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

Adding a Node to a HeapAdding a Node to a Heap

The parent has a key that is >= new node, or

The node reaches the root. The process of pushing the

new node upward is called reheapificationreheapification upward. 19

4222135

23

45

42

27

Note: Note: we need to easily go from child to parent as well as parent to child.

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

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

19

4222135

23

45

42

27

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

Removing the Top of a HeapRemoving the Top of a Heap

Move the last node onto the root.

19

4222135

23

27

42

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

Removing the Top of a HeapRemoving 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.

19

4222135

23

27

42

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

Removing the Top of a HeapRemoving 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.

19

4222135

23

42

27

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

Removing the Top of a HeapRemoving 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.

19

4222127

23

42

35

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

Removing the Top of a HeapRemoving the Top of a Heap

The children all have keys <= the out-of-place node, or

The node reaches the leaf.

The process of pushing the new node downward is called reheapificationreheapification downward.

19

4222127

23

42

35

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

Priority Queues RevisitedPriority Queues Revisited

A priority queue is a container class that A priority queue is a container class that allows entries to be retrieved according to allows entries to be retrieved according to some specific priority levelssome specific priority levels The highest priority entry is removed firstThe highest priority entry is removed first If there are several entries with equally high If there are several entries with equally high

priorities, then the priority queue’s priorities, then the priority queue’s implementation determines which will come implementation determines which will come out first (e.g. FIFO)out first (e.g. FIFO)

Heap is suitable for a priority queueHeap is suitable for a priority queue

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

Adding a Node: same priority 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. 19

4222127

23

45

35

45*

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

Adding a Node : same priority 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. 19

4222145*

23

45

35

27

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

Adding a Node : same priority 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. 19

4222135

23

45

45*

27

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

Adding a Node : same priority Adding a Node : same priority

The parent has a key that is >= new node, or

The node reaches the root. The process of pushing

the new node upward is called reheapificationreheapification upward. 19

4222135

23

45

45*

27

Note: Implementation determines which 45 will be in the root, and will come out first when popping.

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

Removing the Top of a HeapRemoving the Top of a Heap

The children all have keys <= the out-of-place node, or

The node reaches the leaf. The process of pushing the

new node downward is called reheapificationreheapification downward.

19

4222127

23

45*

35

Note: Implementation determines which 45 will be in the root, and will come out first when popping.

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

Heap Implementation (how?)Heap Implementation (how?)

Use binary_tree_node class Use binary_tree_node class node implementation is for a general binary treenode implementation is for a general binary tree but we may need to have doubly linked nodebut we may need to have doubly linked node

Use arrays (page 475)Use arrays (page 475) A heap is a complete binary treeA heap is a complete binary tree which can be implemented more easily with an which can be implemented more easily with an

array than with the node classarray than with the node class and do two-way linksand do two-way links

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

Implementing a HeapImplementing a Heap

We will store the We will store the data from the data from the nodes in a nodes in a partially-filled partially-filled array.array.

An array of dataAn array of data

2127

23

42

35

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

Implementing a HeapImplementing a Heap

Data from the root Data from the root goes in thegoes in the first first location location of the of the array.array.

An array of dataAn array of data

2127

23

42

35

42

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

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23

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

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

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

Implementing a HeapImplementing a Heap

Data from the next Data from the next row goesrow goes in the in the next two array locations.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

We don't care what's inWe don't care what's inthis part of the array.this part of the array.

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

Important Points about the ImplementationImportant Points about the Implementation

The links between the tree's The links between the tree's nodes are nodes are notnot actually stored as actually stored as pointers, or in any other way.pointers, or in any other way.

The only way we "know" that The only way we "know" that "the array is a tree" is from the "the array is a tree" is from the way we manipulate the data.way we manipulate the data.

An array of dataAn array of data

2127

23

42

35

42 35 23 27 21

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

Important Points about the ImplementationImportant Points about the Implementation

If you know the index of a If you know the index of a node, then it is easy to figure node, then it is easy to figure out the indexes of that node's out the indexes of that node's parent and children. Formulas parent and children. Formulas are given in the book.are given in the book.

[0][0] [1] [2] [3] [4]

2127

23

42

35

42 35 23 27 21

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

Formulas for location children and parents in an array representation Formulas for location children and parents in an array representation

Root at location [?]Root at location [?] Parent of the node in [i] is at [?]Parent of the node in [i] is at [?] Children of the node in [i] (if exist) is at Children of the node in [i] (if exist) is at

[ ? ] and [ ? ][ ? ] and [ ? ]

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

Formulas for location children and parents in an array representation Formulas for location children and parents in an array representation

Root at location [0]Root at location [0] Parent of the node in [i] is at [(i-1)/2]Parent of the node in [i] is at [(i-1)/2] Children of the node in [i] (if exist) is at Children of the node in [i] (if exist) is at

[2i+1] and [2i+2][2i+1] and [2i+2] Test:Test:

complete tree of 10, 000 nodescomplete tree of 10, 000 nodes parent of 4999 is at parent of 4999 is at children of 4999 is at _____ and _____children of 4999 is at _____ and _____

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

Can you implement the heap class now?Can you implement the heap class now?

Private variables:Private variables:

Public functions:Public functions:

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

Solutions...Solutions...

template <class Item>

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;

}

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

Solutions...Solutions...

template <class Item>

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;

}

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

Solutions...moreSolutions...more

Can you implement the add and Can you implement the add and remove with the knowledge of these remove with the knowledge of these formulas?formulas?

Add Add

RemoveRemove

template <class Item>

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;

}

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

Solutions...moreSolutions...more

Can you implement the add and remove Can you implement the add and remove with the knowledge of these formulas?with the knowledge of these formulas?

Add Add put the new entry in the last locationput the new entry in the last location Push the new node upward, swapping with its parent until the new

node reaches an acceptable location RemoveRemove

move the last node to the rootmove the last node to the root Push the out-of-place node downward, swapping with its larger

child until the new node reaches an acceptable location

template <class Item>

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;

}

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

template <class Item>void heap<Item>::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;

}}

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

A heap is a complete binary tree, where the entry at each node is greater than or equal to the entries in its children.

To add an entry to a heap, place the new entry at the next available spot, and perform a reheapification upward.

To remove the biggest entry, move the last node onto the root, and perform a reheapification downward.

Summary Summary

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

THE ENDTHE END

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 (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using C++ are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.