0% found this document useful (0 votes)
28 views

DS Lab 13 - Heaps

The document provides information about implementing heap data structures in C++. It discusses heap trees, relevant readings, objectives of the lab experiment, concepts of heap trees in C++, homework tasks to complete before the lab, and procedures and tools used in the lab.

Uploaded by

Asim Shareef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

DS Lab 13 - Heaps

The document provides information about implementing heap data structures in C++. It discusses heap trees, relevant readings, objectives of the lab experiment, concepts of heap trees in C++, homework tasks to complete before the lab, and procedures and tools used in the lab.

Uploaded by

Asim Shareef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Computer Science, Faculty of Computing

International Islamic University, Islamabad

Lab Manual for Data


Structures
Lab-13
Heaps
Lab 13: Heaps

Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Heap Trees.............................................................................................................................................................3
1.3 Relevant Lecture Readings.......................................................................................................................................3
2. Activity Time boxing.............................................................................................................................................4
3. Objectives of the experiment.................................................................................................................................4
4. Concept Map..........................................................................................................................................................4
4.1 Heap Trees in C++......................................................................................................................................................4
5. Homework before Lab.........................................................................................................................................................5
5.1 Problem Solution Modeling........................................................................................................................................5
5.2 Practices from home....................................................................................................................................................5
5.2.1 Task-1......................................................................................................................................................................5
6. Procedure & Tools...............................................................................................................................................................5
6.1 Tools............................................................................................................................................................................5
6.2 Walk through Tasks [Expected time = 20 mins]......................................................................................5
7. Practice Tasks......................................................................................................................................................................7
7.1 Practice Task 1 [Expected time = 20 mins]............................................................................................7
7.2 Practice Task 2............................................................................................................................................................7
7.3 Out comes....................................................................................................................................................................8
7.3 Testing.........................................................................................................................................................................8
8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks]...............................................................8
9. Evaluation criteria...............................................................................................................................................................8
10. Further Readings...............................................................................................................................................................9
10.1 Web sites related to C++ tutorials about AVL and Heap trees.................................................................................9
10.2 Web sites containing supporting material.................................................................................................................9

Department of Computer Page 2


IIU, Islamabad
Lab 13: Heaps

Lab 13: 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 Page 3


IIU, Islamabad
Lab 13: 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 30mins
7 Practice tasks 30 mins for task 1 and 30 mins 60mins
for task 2
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);
Department of Computer Page 4
IIU, Islamabad
Lab 13: Heaps

void heapifyup(int index);


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 2017 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 2017.

Figure 2: Implementation of a heap tree

Department of Computer Page 5


IIU, Islamabad
Lab 13: Heaps

Figure 3: Implementation of a heap tree.

Figure 4: Implementation of a heap tree.

Department of Computer Page 6


IIU, Islamabad
Lab 13: 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.
Lab instructors are guided to help students learn how ACM problems work and provide
students with certain practice/home tasks on the pattern of ACM Problems.

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 Practice Task 2


Implement a program in which you have to implement a minimum heap tree using arrays. Your
program should contain following functions:
 Insert
 Display
 Find minimum value
Department of Computer Page 7
IIU, Islamabad
Lab 13: Heaps

 Delete
7.3 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 2017 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
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
Department of Computer Page 8
IIU, Islamabad
Lab 13: Heaps

2. 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

Department of Computer Page 9


IIU, Islamabad

You might also like