Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help to complete two C++ functions-minHeap and sort.Please use follow the descriptions. Thanks! Code: #include #include using namespace std; class Node { public: int
Please help to complete two C++ functions-"minHeap" and "sort".Please use follow the descriptions. Thanks! Code: #include#include using namespace std; class Node { public: int value; Node* l_child; Node* r_child; Node() : l_child(nullptr), r_child(nullptr) {} Node(int i) :value(i), l_child(nullptr), r_child(nullptr) {} }; class Tree { //an n-level full binary tree of 2^n - 1 nodes public: Node* root; Tree() : root(nullptr) {} Node* makeTree(int n, int m); void printTree1(Node* p); //in-order printing void printTree2(Node* p); //pre-order printing void printTree3(Node* p); //post-order printing void mirror(Node* p); int sum(Node* p); /* Implement the following three member functions. */ void minHeap(Node* p); //Recall that in a Min Heap, at all nodes, //value(parent)<= value(l_child) and value(parent) <= value(r_child). //This function re-structures the tree, whose root is pointed by p, into a Min Heap. //You are required to use recursion. pair sort(Node* p); //sorting such that //ascending sequence in pre-order traversal //This function returns a pair of pointers, which point to the node with the smallest //value and the node with the largest values in the tree rooted at node pointed by p . //printTree2 will give a sorted sequence. //You are required to use recursion, but additional while loop is allowed. }; void Tree::minHeap(Node* p) { //Your code } pair Tree::sort(Node* p) { //Your code } Node* Tree::makeTree(int n, int m) { //Create an n-level full binary tree with //with random values between 0 ... m-1 //and returns a pointer to the root. Node* p = new Node(rand() % m); if (n == 1) return p; p->l_child = makeTree(n - 1, m); p->r_child = makeTree(n - 1, m); return p; } void Tree::printTree1(Node* p) { //in-order printing if (p == nullptr) return; printTree1(p->l_child); cout << p->value << " "; printTree1(p->r_child); } void Tree::printTree2(Node* p) { //pre-order printing if (p == nullptr) return; cout << p->value << " "; printTree2(p->l_child); printTree2(p->r_child); } void Tree::printTree3(Node* p) { //post-order printing if (p == nullptr) return; printTree3(p->l_child); printTree3(p->r_child); cout << p->value << " "; } int main() { Tree T1; T1.root = T1.makeTree(4, 20); T1.printTree1(T1.root); cout << endl; T1.printTree2(T1.root); cout << endl; T1.printTree3(T1.root); cout << endl; Tree T2 = T1; cout << endl; T2.minHeap(T2.root); T2.printTree1(T2.root); cout << endl; T2.printTree2(T2.root); cout << endl; T2.printTree3(T2.root); cout << endl; Tree T3; T3.root = T3.makeTree(5, 20); T3.sort(T3.root); T3.printTree1(T3.root); cout << endl; T3.printTree2(T3.root); cout << endl; T3.printTree3(T3.root); cout << endl; 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