You are on page 1of 8

Lab Manual for Data Structures

Lab 14
Heaps

Department of Computer Science Page 158


MAJU, Islamabad
_________________ Lab 14: Heaps

Table of Contents
1. Introduction 160
1.1 Heap Trees 160
1.3 Relevant Lecture Readings 160

2. Activity Time boxing 161

3. Objectives of the experiment 161

4. Concept Map 161


4.1 Heap Trees in C++ 161

5. Homework before Lab 162


5.1 Problem Solution Modeling 162
5.2 Practices from home 162
5.2.1 Task-1 162

6. Procedure & Tools 162


6.1 Tools 162
6.2 Walk through Tasks [Expected time = 20 mins] 162

7. Practice Tasks 164


7.1 Practice Task 1 [Expected time = 20 mins] 164
7.2 Out comes 164
7.3 Testing 164

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 165

9. Evaluation criteria 165

10. Further Readings 165


10.1 Web sites related to C++ tutorials about AVL and Heap trees 165
10.2 Web sites containing supporting material 165

Department of Computer Science Page 159


MAJU, Islamabad
_________________ Lab 14: Heaps

Lab 14: Heaps

1. Introduction

In this lab, we will discuss the usage of heap data structures. Students will be able to
grasp the concept of heap trees. We will discuss the different operations to be performed on heap
trees.

1.1 Heap Trees

In computer science, a heap is a particular tree-based data structure that satisfies the heap
property: If A is a parent node of B then key (A) is ordered with respect to key (B) with the same
ordering apply across the heap. Either the keys of parent nodes are always greater than or equal
to those of the children and the highest key is in the root node (this kind of heap is called max
heap) or the keys of parent nodes are less than or equal to those of the children and the lowest
key is in the root node (min heap). Heaps are critical in several efficient graph algorithms such as
Dijkstra's algorithm, and in the sorting algorithm heap sort.

Note that there is no implied ordering between siblings or cousins and no implied sequence for
an in-order traversal (as there would be in, e.g., a binary search tree). The heap relation mention
above applies only between nodes and their immediate parents. The maximum number of
children each node can have depends on the type of heap, but in many types it is at most two,
which is known as a "binary heap". Figure 2 represents a complete binary max heap tree.

Figure 1: A complete binary max heap tree.

1.3 Relevant Lecture Readings

a) Revise Lecture No. 29 and 30 available at \\dataserver\jinnah$\ in instructor’s


folder.
b) From books: C++ Data Structures by Nell Dale (Page 535 - 546) and Data
structures using C++ by D. S. Malik (Page 635 - 653).

Department of Computer Science Page 160


MAJU, Islamabad
_________________ Lab 14: Heaps

2. Activity Time boxing


Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 20 mins for task 1 and 2, 30 70mins
mins for task 3
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

 To understand and implement the heap trees with their operations in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
lab.

4.1 Heap Trees in C++


Heap is a binary tree that stores priorities (or priority-element pairs) at the nodes. It has
the following properties:
 All levels except last level are full. Last level is left filled.
 Priority of a node is at least as large as that of its parent (min-heap) (or) vice-versa (max-
heap).
 If the smallest element is in the root node, it results in a min-heap.
 If the largest element is in the root node, it results in a max-heap.

Following is a class definition of heap tree:

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
class Heap {
public:
Heap();
~Heap();
void insert(int element);
int deletemin();
void print();
int size() { return heap.size(); }
private:
int left(int parent);
int right(int parent);
int parent(int child);
void heapifyup(int index);
Department of Computer Science Page 161
MAJU, Islamabad
_________________ Lab 14: Heaps

void heapifydown(int index);


private:
vector<int> heap;
};

5. Homework before Lab


This homework will help students to study the concepts of topic before start of lab.

5.1 Problem Solution Modeling


After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

5.2 Practices from home

5.2.1 Task-1
Provide difference between a heap and an AVL tree.

6. Procedure & Tools


This section provides information about tools and programming procedures used for the
lab.

6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.

6.2 Walk through Tasks [Expected time = 20 mins]

Following screens in figure 2 to 5 represent the implementation of a heap tree. You are
required to code and execute this program in Microsoft Visual Studio 2008.

Figure 2: Implementation of a heap tree


Department of Computer Science Page 162
MAJU, Islamabad
_________________ Lab 14: Heaps

Figure 3: Implementation of a heap tree.

Figure 4: Implementation of a heap tree.


Department of Computer Science Page 163
MAJU, Islamabad
_________________ Lab 14: Heaps

Figure 5: Implementation of a heap tree.

7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and place solution code in a
folder specified by your lab instructor.

7.1 Practice Task 1 [Expected time = 20 mins]


Write a program which should implement a max heap tree containing floating point values as its
elements. Program should allow user to insert a value in tree and program should perform search
a value input by user to check whether it exists in tree or not?

7.2 Out comes

After completing this lab, student will be able to understand and develop programs related to and
heap trees in C++ using Microsoft Visual Studio 2008 environment.

7.3 Testing
Test Cases for Practice Task-1
Sample Input Sample Output
Enter the values of elements for max
heap tree:
2.3
3.4
1.5
5.6
2.1

Department of Computer Science Page 164


MAJU, Islamabad
_________________ Lab 14: Heaps

1.9
Enter a value to exist in tree: 2.1 Value found in tree

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks]

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks and Testing 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings


10.1 Web sites related to C++ tutorials about AVL and Heap trees
1. http://www.cs.uah.edu/~rcoleman/Common/CodeVault/Code/Code203_Tree.html
2. http://login2win.blogspot.com/2011/06/c-heaps.html
3. http://www.cplusplus.com/reference/algorithm/make_heap/

10.2 Web sites containing supporting material


1. http://courses.csail.mit.edu/6.006/fall09/lecture_notes/lecture04.pdf
2. http://oopweb.com/Algorithms/Files/Algorithms.html

Department of Computer Science Page 165


MAJU, Islamabad

You might also like