Question: Can anyone help to solve this C plus plus program? Thanks! #include using namespace std; int m = 50; class Node { public: int value;
Can anyone help to solve this C plus plus program? Thanks!
#includeusing namespace std; int m = 50; class Node { public: int value; Node* l_child; Node* r_child; Node* parent; Node() : l_child(nullptr), r_child(nullptr), parent(nullptr) {} Node(int i) :value(i), l_child(nullptr), r_child(nullptr), parent(nullptr) {} }; class Tree { //an n-level full binary tree of 2^n - 1 nodes public: Node* root; int level; Tree() : root(nullptr), level(0) {} Node* makeTree(int n); void printTree1(Node* p); void printTree2(Node* p); void printTree3(Node* p); //************* //Implement the following 4 member functions using recursion. int numOdds(Node* p);//Return the number of odd numbers in the subtree rooted at node pointed by p Node* SymCopy(Node* p);//Create a tree symmetric to the subtree rooted at node pointed by p bool Find(Node* p, int k);//Return true if value k exists in the subtree rooted at node pointed by p //else return false int LeftHeavy(Node* p);//For all nodes in the subtree rooted by a node pointed by p, //re-structure the tree such that //sum of left child branch is >= sum of right child branch. //In addition, return the sum of all nodes in the subtree rooted by the node pointed by p. //*********** //Implement the following function. //You are not required to use recursion for this implementation. void makeMaxHeap(Node* p); //Re-structure the subtree rooted at a node pointed by p such that //the subtree is a max heap. //Remember that a tree is a max heap if all nodes in the tree satisfy the property //that (value of left child <= value of parent) and (value of right child <= value of parent). }; void Tree::makeMaxHeap(Node* p) { //Need to implement this function } int Tree::LeftHeavy(Node* p) { //Need to implement this funciton. } bool Tree::Find(Node* p, int k) { //Need to implement this funciton. } Node* Tree::SymCopy(Node* p) { //Need to implement this funciton. } int Tree::numOdds(Node* p) { //Need to implement this funciton. } Node* Tree::makeTree(int n) { //Create an n-level full binary tree with //with random values between 0 ... m-1 Node* p = new Node(rand() % m); if (n == 1) return p; p->l_child = makeTree(n - 1); p->l_child->parent = p; p->r_child = makeTree(n - 1); p->r_child->parent = p; 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); T1.printTree1(T1.root); cout << endl; T1.printTree2(T1.root); cout << endl; T1.printTree3(T1.root); cout << endl; cout << T1.numOdds(T1.root) << endl; Tree T2; T2.root = T1.SymCopy(T1.root); T2.printTree1(T2.root); cout << endl; T2.printTree2(T2.root); cout << endl; T2.printTree3(T2.root); cout << endl; cout << T2.Find(T2.root, 19) << endl; cout << T2.LeftHeavy(T2.root) << endl; T2.printTree1(T2.root); cout << endl; T2.printTree2(T2.root); cout << endl; T2.printTree3(T2.root); cout << endl; T2.makeMaxHeap(T2.root); T2.printTree1(T2.root); cout << endl; T2.printTree2(T2.root); cout << endl; T2.printTree3(T2.root); return 0; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
