Question
code must be in c++ and use skeleton provided please. You are given a class TNode that contains one integer value, and three pointers one
code must be in c++ and use skeleton provided please.
You are given a class TNode that contains one integer value, and three pointers one to the parent, one to the left child, and one to the right child. You need to complete the class minHeal and other functions specified in the cpp file.
Task 1: Implement the constructors (default and copy) of Heap. You need to make sure that the copy constructor makes a separate copy of the heap.
Task 2: Implement in, removemin, getmin. Note: getmin returns the pointer to the min element, but do not modify the heap. On the other hand, removemin just deletes the min element from the heap. In this homework, I request that the in function takes input const TNode t, which means you cannot modify the input node t. You should create a new node (different from the input node t) and then add into the heap. It is highly recommended to write helper functions, such as bubble-up and bubble-down. If you dont know what they are, you should review heap.
Task 3: Implement Heapify that takes input a binary tree, and makes a heap from the binary tree. Here your binary tree is in the array form. You cannot modify the array.
Task 4: Implement Heapsort that takes input an array of size n, and returns a sorted array.
Task 5: Design a test function of your own design. Test everything!
#include
using namespace std;
class TNode
{
public:
int val;
TNode(){}
TNode(int v){val = v;}
TNode * left;
TNode * right;
TNode * parent;
};
class minHeap // binary heap
{
int size;
TNode *top;
public:
minHeap();
minHeap(const minHeap &h);
void in(const TNode &t);// you should new a new node and then add into the heap
void removemin();
Node* getmin();
void Heapify(const int n, const int *p);// p is an array of size n, representing a (full) binray tree of size n-1. (The tree starts at location 1). You should not modify the array p.
};
int * HeapSort(const int n, const int *arr);// arr is an integer array of numbers.
//You should output a pointer to a new array which is a sorted version of arr
void Test() // write a test for your implementation
{
}
int main(int argc, const char * argv[]) {
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started