Question: This weeks labwork will be about Heap data structure. You are given a heap.h header file which contains a HeapNode class and Heap class. You
This weeks labwork will be about Heap data structure. You are given a heap.h header file which contains a HeapNode class and Heap class. You are going to implement a min-heap. 1. The insert function is given as two parts. The first part is using Heap property to find the suitable location to add the given data to the Heap. For this purpose the code given to you includes a stack to hold the 0 or 1 values to navigate through the Heap. For example, 1 4 2 7 6 8 In this heap, if insert is called the suitable location is the right child of the node 2. In order to find this, you can use the heap-size. In this example the heap size is 6, when a new node is added the size will become 7. In order to navigate through the heap and find the location of the right child of 2, you must start from root and go right and right again to arrive at the proper location. If you find the mod 2 of 7 it is 1 so it points to the right. Then you perform integer division of 7 by 2 to find 3. This points to right again. This process is given in the insert function. You must use this stack and add the given data to the heap. Then fix the heap if the added data breaks the min-heap property. (70 pts) insert(const T &new_data) insert(const T &new_data, HeapNode *node, stack &dir) 2. You must write the necessary code to navigate the heap using level order. Print the heap so that the min-heap structure can be verified. print_levelorder(HeapNode *node, int level)
heap.h
#include
using namespace std;
template
HeapNode(const T &new_data) { data = new_data; left = NULL; right = NULL; parent = NULL; } };
template
// These functions will be implemented recursively. The following are driver functions // for the programmer to use. void insert(const T&); void print_inorder() const; void print_levelorder() const;
private: HeapNode
template
template
template
/* * * * * * * */ return tmp; }
template
template
template
template
template
Main.cpp
#include "heap.h"
int main() {
Heap
tree.insert(5); tree.insert(2); tree.insert(7); tree.insert(8); tree.insert(6); tree.insert(4); tree.insert(1); tree.insert(9);
tree.print_levelorder(); tree.print_inorder();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
