Question

Problem 5. (20 pts total) Write the erase functions for the Mtree class that deletes every node in the Mtree. Note there should be two erase functions. The irst is in the e second is a recursive function in the private section that is called by the function in the public ar Root.) ree-

Please use template <typename T>. This is a C++ Data Structures Question.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Screenshot

// public function void erase () /I root points to the root node of the tree erase util (this->root); /MNode is the class tha

Code

// public function

void erase()

{

    // root points to the root node of the tree

    erase_util(this->root);

}

// MNode is the class that defines the node of the tree

// please change the name accordingly

void erase_util(MNode *node)

{

    // if the tree is not empty

    if(!node)

    {

        // traverse the left subtree

        // getLeft() return pointer to left child

        erase_util(node->getLeft());

       

        // traverse the right subtree

        // getLeft() return pointer to right child

        erase_util(node->getRight());

       

        // delete the current node

        delete node;

    }

}

Add a comment
Know the answer?
Add Answer to:
Please use template <typename T>. This is a C++ Data Structures Question. Problem 5. (20 pts...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following template class: template <typename T> class ArrayOfTen { public: ArrayOfTen(); ~ArrayOfTen(); void insert(T);...

    Consider the following template class: template <typename T> class ArrayOfTen { public: ArrayOfTen(); ~ArrayOfTen(); void insert(T); void printAll(); private: T * array; int size; } a. When the array is first created, it should allocate memory for 10 items. Size should be zero. Write the constructor: b. When ArrayOfTen::insert(T) is called, the new item should be added to the array, and size should increase by one. If the array already has 10 items, then it should throw an exception. Write...

  • #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

    #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...

  • v. 15 points) implementing data structure operations C++ code for a new public member function to...

    v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • Create a template function printResult which prints a value of type T void printResult(T val); C++...

    Create a template function printResult which prints a value of type T void printResult(T val); C++ Write a program to create two Mathematic type (template below) objects a(10, 5) and b(3.4, 5.5) which prints using the template function printResult the result of a.addition(), a.subtraction(), a.multiplciation(), a.division() b.addition(), b.subtraction(), b.multiplication(), b.division() (Template) template <typename T> class Mathematics{ private: T val1, val2; public: Mathematics(T v1, T v2) { val1 = v1; val2 = v2; } T addition() { return val1 + val2;...

  • C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions...

    C++ Data Structures and Algorithms Binary Trees: Implementation Answer the following question(s) concerning implementing recursive functions to perform operations on linked lists of nodes arranged as binary trees. For these questions, use the following struct definition for the nodes of the tree (we will not templatize the node here, so you do not have to write template functions for these questions, just assume trees of <int>values): struct BinaryTreeNode { int item; BinaryTreeNode* left; BinaryTreeNode* right; }; Write a recursive function...

  • Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp....

    Please use the linked approach implement the BST ADT, implement all the functions in the BSTree.cpp. Add the ouput of the implementation. use recursive functions to traverse the tree - read the implementation notes on using helper functions. Please use C++ programming ////////////////////////////////////////////////////////////// #include "BSTree.h" template <typename DataType, class KeyType> BSTree<DataType, KeyType>::BSTreeNode::BSTreeNode ( const DataType &nodeDataItem, BSTreeNode *leftPtr, BSTreeNode *rightPtr ) { } template < typename DataType, class KeyType > BSTree<DataType, KeyType>::BSTree () {    root = 0; } template...

  • Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns...

    Data Structures, algorithm, c++. Please explain if possible. Thank you! Complete the following function that returns the largest element of the binary tree rooted at root. template <typename T> T max (Node<T>root) if (root nullptr) return TO; T max1 = root->x; T max2 root->x; if (root->left != nullptr) maxlmax (root->left); if (root->right != nullptr) max2 max ( root->right); - if (root->x >= max1 && root->x >= max2) return __.-; if (max1 >= root->x && max1 >= max2) return-_-> return___--> Let...

  • #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int...

    #include <iostream> using namespace std; template <typename Item> class MyArray{ private:    Item *myarray;    int size;    int used;    void doubleSize(); public:    MyArray();    ~MyArray();    int length();    void insertHead(Item i);    void insertTail(Item i);    void deleteHead();    void deleteTail();    void sortAscending();    void sortDescending();    Item operator [](int i){        return myarray[i];    } }; template <typename Item> MyArray<Item>::MyArray(){    size = 5;    used = 0;    myarray = new Item[size];...

  • Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public...

    Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT