Question

In: Computer Science

Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...

Write a complete C++ program to implements a Min-heap.

Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42.

The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap.

The program should allow the user to output data in Preorder, Inorder and Postorder.

The program should loop with menu items for each of the above objectives and the choice to quit

Here's the code:

**EDIT: Need help in outputting data into Preorder, Inorder and Postorder, plus ensuring the nodes are implemented correctly (I think I overlooked this part). **

#include studio.h
#include isotream

int array[100]; // variable for array to store up to 100 elements
int n;


using namespace std;
void display();
void delete_elem(int num);


void heapify(int index,int n){
if(index >= n)return;
  

int left = 2*index;
int right = 2*index + 1;
int mx_ind = index;
if(left < n and array[left] > array[mx_ind]){
mx_ind = left;
}
if(right < n and array[right] > array[mx_ind]){
mx_ind = right;
}
if(mx_ind != index){
swap(array[mx_ind],array[index]);
heapify(mx_ind,n);
}

}
int insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= array[parentnode])
{
array[location] = num;
return location;
}
array[location] = array[parentnode];
location = parentnode;
}
array[0] = num;
return 0;
}

int main()
{
int choice, num;
n = 0;
while(1)
{
printf("1.Insert the element \n");
printf("2.Delete the element \n");
printf("3.Display all elements \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete_elem(num);
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}
}
}

void display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("\n");
}


void delete_elem(int num)
{
int left, right, i, temp, parentnode;

for (i = 0; i < num; i++) {
if (num == array[i])
break;
}
if (num != array[i])
{
printf("%d not found in heap list\n", num);
return;
}
array[i] = array[n - 1];
n = n - 1;
parentnode =(i - 1) / 2;
if (array[i] > array[parentnode])
{
insert(array[i], i);
return;
}
left = 2 * i + 1;
right = 2 * i + 2;
while (right < n)
{
if (array[i] >= array[left] && array[i] >= array[right])
return;
if (array[right] <= array[left])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
i = left;
}
else
{
temp = array[i];
array[i] = array[right];
array[right] = temp;
i = right;
}
left = 2 * i + 1;
right = 2 * i + 2;
}
if (left == n - 1 && array[i])
{
temp = array[i];
array[i] = array[left];
array[left] = temp;
}
}

Solutions

Expert Solution

The above doesn't implement heapify function correctly and chances have been made to that function, go through the source code below to see the changes made. Implementation of a preorder,inorder, postorder has been made in below source code. Compare the above source code with the below source code to see the changes made to make the program work correctly. Moreover the above function created max-heap but we require min-heap and below implementation has made the required changes for that.

#include<bits/stdc++.h>
#include<stdio.h>
int arr[100]; // variable for array to store up to 100 elements
int n;


using namespace std;
void display();
void delete_elem(int num);
//Definition for preorder,postorder,inorder
void inorder(int idx){
if(idx>=n)return ;
int left=2*idx+1;
int right=2*idx+2;
inorder(left);
cout<<arr[idx]<<" ";
inorder(right);
}
void preorder(int idx){
if(idx>=n)return;
int left=2*idx+1;
int right=2*idx+2;
cout<<arr[idx]<<" ";
preorder(left);
preorder(right);
}
void postorder(int idx){
if(idx>=n)return;
int left=2*idx+1;
int right=2*idx+2;
postorder(left);
postorder(right);
cout<<arr[idx]<<" ";
}

void heapify(int index,int n){
if(index >= n)return;


int left = 2*index+1;// add 1 to get left child index
int right = 2*index + 2;// add two to get right child index
int mx_ind = index;
if(left < n and arr[left] < arr[mx_ind]){//use arr[left]<arr[mx_ind],we are creating min heap
mx_ind = left;
}
if(right < n and arr[right] < arr[mx_ind]){
mx_ind = right;
}
if(mx_ind != index){
swap(arr[mx_ind],arr[index]);
heapify(mx_ind,n);
}

}
int insert(int num, int location)
{
int parentnode;
while (location > 0)
{
parentnode =(location - 1)/2;
if (num <= arr[parentnode])
{
arr[location] = arr[parentnode];
}
else break;
location = parentnode;
}
arr[location] = num;
return 0;
}

int main()
{
int choice, num;
n = 0;
// To insert 5 elements in the heap make use of heapify function
arr[0]=8,arr[1]=12,arr[2]=24,arr[3]=32,arr[4]=42;
n=5;
for(int i=n/2-1;i>=0;i--){// Initializing our heap with these 5 values
heapify(n,i);
}
while(1)
{
printf("1.Insert the element \n");
printf("2.Delete the element \n");
printf("3.Display all elements \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the element to be inserted to the list : ");
scanf("%d", &num);
insert(num, n);
n = n + 1;
break;
case 2:
printf("Enter the elements to be deleted from the list: ");
scanf("%d", &num);
delete_elem(num);
break;
case 3:
display();
cout<<"Inorder display ";
inorder(0);cout<<endl;
cout<<"Preorder display ";
preorder(0);cout<<endl;
cout<<"Postorder display ";
postorder(0);cout<<endl;
break;
case 4:
exit(0);
default:
printf("Invalid choice \n");
}
}
}

void display()
{
int i;
if (n == 0)
{
printf("Heap is empty \n");
return;
}
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}


void delete_elem(int num)
{
int left, right, i, temp, parentnode;

for (i = 0; i < num; i++) {
if (num == arr[i])
break;
}
if (num != arr[i])
{
printf("%d not found in heap list\n", num);
return;
}
arr[i] = arr[n - 1];
n = n - 1;
// simply use heapify at index i and the subtree will follow heap property after running function
heapify(i,n);

}


Related Solutions

Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit.
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Print the complete list using toString() method. Delete head note from both...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT