Professional Documents
Culture Documents
Lab 14
Heaps
Table of Contents
1. Introduction 160
1.1 Heap Trees 160
1.3 Relevant Lecture Readings 160
8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 165
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.
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.
#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
5.2.1 Task-1
Provide difference between a heap and an AVL tree.
6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.
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.
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.
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
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).