Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In c++, create a MinHeap of a Persons class using the following code: Show the following operations in the main: Insert,Delete, HeapSort(Print after) class Person

In c++, create a MinHeap of a Persons class using the following code: Show the following operations in the main: Insert,Delete, HeapSort(Print after)

class Person

{

public:

string name;

int age;

int height;

int weight;

Person() {}

Person(string n, int a, int h, int w)

{

name = n;

age = a;

height = h;

weight = w;

}

};

#include using namespace std; class Heap { public: int* A; int last = -1; Heap(int asize) { A = new int[asize]; } int getParentIndex(int childIndex) { if (childIndex % 2 == 0) return childIndex / 2 - 1; else return childIndex / 2; } int getChildL(int parentIndex) { return 2 * parentIndex + 1; } int getChildR(int parentIndex) { return 2 * parentIndex + 2; } void Insert(int x) { last++; A[last] = x; Heapify(last); } void Heapify(int childIndex) { int parentIndex = getParentIndex(childIndex); while(parentIndex>=0 && childIndex >0) { if (A[childIndex] > A[parentIndex]) { int temp = A[parentIndex]; A[parentIndex] = A[childIndex]; A[childIndex] = temp; } childIndex = parentIndex; parentIndex = getParentIndex(parentIndex); } } void Delete(int x) { int index = -1; for (int i = 0; i <= last; i++) { if (A[i] == x) { index = i; break; } } if (index == -1) { cout << "Element not found" << endl; } //A[index] = A[last]; int temp = A[index]; A[index] = A[last]; A[last] = temp; last--; HeapifyDown(index); } void HeapifyDown(int parentIndex) { while (true) { int largest = parentIndex; int childLindex = getChildL(parentIndex); int childRindex = getChildR(parentIndex); if (childLindex <= last && A[childLindex] > A[largest]) { largest = childLindex; } if (childRindex <= last && A[childRindex] > A[largest]) { largest = childRindex; } if (largest != parentIndex) { int temp = A[parentIndex]; A[parentIndex] = A[largest]; A[largest] = temp; parentIndex = largest; } else { break; } } } void Print() { for (int i = 0; i <= last; i++) { cout << A[i] << " "; } cout << endl; } void HeapSort() { for (int i = 0; i < 11; i++) { Delete(A[0]); } } void PrintAll() { for (int i = 0; i < 11; i++) { cout << A[i] << " "; } cout << endl; } }; int main() { 

Person* people = new Person[10];

people[0] = { "Becca", 25, 69, 202 };

people[1] = { "Tom", 19, 59, 200 };

people[2] = { "Mike", 21,67,250 };

people[3] = { "Amanda", 23, 50, 140 };

people[4] = { "Katie", 15, 74, 150 };

people[5] = { "Scott", 35, 58, 145 };

people[6] = { "Jen", 45, 62, 165 };

people[7] = { "Carol", 75, 52, 157 };

people[8] = { "George", 77, 54, 167 };

people[9] = { "Zach", 20, 60, 190 };

Heap* heap = new Heap(10);

heap->Insert(people[0]); heap->Print();

heap->Insert(people[1]); heap->Print();

heap->Insert(people[2]); heap->Print();

heap->Insert(people[3]); heap->Print();

heap->Insert(people[4]); heap->Print();

heap->Insert(people[5]); heap->Print();

heap->Insert(people[6]); heap->Print();

heap->Insert(people[7]); heap->Print();

heap->Insert(people[8]); heap->Print();

heap->Insert(people[9]); heap->Print();

heap->HeapSort();

heap->PrintAll();

getchar();

return 0;

 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Define the nature of interviews

Answered: 1 week ago

Question

2. Outline the different types of interviews

Answered: 1 week ago