DS Lab 13 - Heaps
DS 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
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.
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.
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.
#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
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.
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
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).
2. http://www.cplusplus.com/reference/algorithm/make_heap/