Question: What is the definition of the public method setRootData for binary tre
ID: 3751970 • Letter: Q
Question
Question: What is the definition of the public method setRootData for binary tree? Implement it using a recursive helper in a way that the resulting binary tree stayed balanced.
Programming Language: c++
Explanation / Answer
Definition of public method setRoot(Data) for binary tree -> void SetRoot(BinTreeNode * ptr); complete cpp code-> ------------------------------------------------------ #include #include /* A class to maintain an ordered list of integers. ** The following methods are supported: ** -OrdIntTree::Add // Add an int to the tree at an appropriate pos ** -OrdIntTree::Delete // Delete the entire tree ** -OrdIntTree::PrintTree // Print the tree structure on its side ** -OrdIntTree::Remove // Remove an element from the tree ** -OrdIntTree::MinData // The minimum data value in the tree ** -OrdIntTree::PrintPaths // Print the paths from root to each leaf */ class BinTreeNode { // A very simple class describing each node of the binary tree public: int data; // data held at each node BinTreeNode * left; // pointer to left subtree BinTreeNode * right; // pointer to right subtree }; class OrdIntTree { BinTreeNode * root; // a pointer to the root of the tree public: OrdIntTree(); // constructor ~OrdIntTree(); // destructor void SetRoot(BinTreeNode * ptr); BinTreeNode * GetRoot(); // Wrapper methods that take // care of special cases or extra tasks before calling the // private recursive methods that do the real work) void Delete(); // delete the entire tree void Add(int newdata); // add a new element to the tree such that children // to the left of a parent have lower integers and // children to the right have higher or equal integers. void PrintTree(bool info); // Print the tree structure on its side void Remove(int sdata); // Remove a given element from the tree whilst // maintaining an ordered list of integers in the tree. int MinData(BinTreeNode * current); // Return the min data element in a subtree void PrintPaths(); // Print the paths from root to each leaf private: // recursive implementations of the tasks void DeleteRec(BinTreeNode * current); void AddRec(BinTreeNode * current, int newdata); void PrintTreeRec(BinTreeNode * current, int depth, bool info); int Height(BinTreeNode * current); int Max(int a, int b); bool RemoveRec(BinTreeNode ** current, int sdata); void RemoveMinRec(BinTreeNode ** current); void PrintPathsRec(BinTreeNode * current, string path); }; OrdIntTree::OrdIntTree(){ root = NULL; // Initialise the tree (set root to NULL). } OrdIntTree::~OrdIntTree(){ Delete(); } void OrdIntTree::SetRoot(BinTreeNode * ptr){ root = ptr; } BinTreeNode * OrdIntTree::GetRoot(){ return root; } void OrdIntTree::Delete(){ // Wrapper method to delete a tree. // Although all memory (including that at the root of the tree) will // be deallocated correctly, we should also point the root pointer // away from this memory location. This means we can safely add new // nodes after deleting the tree DeleteRec(GetRoot()); SetRoot(NULL); } void OrdIntTree::DeleteRec(BinTreeNode * current){ // Recursive method to delete a tree. Deletes nodes from the leaves // to the root. Starts at rightmost node of lowest leftmost subtree if (current != NULL) { DeleteRec(current->left); DeleteRec(current->right); delete current; } } void OrdIntTree::Add(int newdata){ // Wrapper method for adding a node to the tree. The new integer is // positioned such that children to the left of a parent have lower // int values and children to the right have higher or equal int values. // This wrapper takes account of the special case where root==NULL before // calling the recursive method BinTreeNode * temp; if (GetRoot() == NULL) { temp = new BinTreeNode; temp->data = newdata; temp->left = NULL; temp->right = NULL; SetRoot(temp); } else { AddRec(GetRoot(), newdata); } } void OrdIntTree::AddRec(BinTreeNode * current, int newdata){ // Recursive method for adding a node to the tree. The new integer is // positioned such that children to the left of a parent have lower // int values and children to the right have higher or equal int values. // A wrapper method ensures that current is not NULL BinTreeNode * temp; if (newdata data) { // position to the left of current if (current->left == NULL) { temp = new BinTreeNode; temp->data = newdata; temp->left = NULL; temp->right = NULL; current->left = temp; } else { AddRec(current->left, newdata); } } else { //position to the right of current if (current->right == NULL) { temp = new BinTreeNode; temp->data = newdata; temp->left = NULL; temp->right = NULL; current->right = temp; } else { AddRec(current->right, newdata); } } } void OrdIntTree::PrintTree(bool info){ // Wrapper method for printing the tree structure on its side. Option // to include node depths & heights cout right, depth + 1, info); cout right; delete temp; } else { RemoveMinRec(&(*current)->left); } } void OrdIntTree::PrintPaths(){ cout left == NULL) && (current->right == NULL)) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.