Question

For this lab, you will need to write the following functions in table.cpp, and add function...

For this lab, you will need to write the following functions in table.cpp,
and add function prototypes for them to table.h and invoke the functions in
main.cpp.

* int countNodes(node * root)

Recursively count the number of the nodes in the tree.

* int sumLeaves(node * root)

Recursively sum all of the leaf nodes in the tree.

You must use the functions with these exact function prototypes.
******************************************************************************

main.cpp

#include "table.h"

#include <iostream>

using namespace std;

int main()
{
node * root = NULL;
build(root);
display(root);

int count = countNodes(root);
cout << "Count nodes: " << count << endl;

int sum = sumLeaves(root);
cout << "Sum leaves: " << sum << endl;

display(root);
destroy(root);
return 0;
}

**********************************

table.cpp

#include "table.h"

//Please put the impelementation of the required functions here

*************************************************

table.h

#ifndef TABLE_H
#define TABLE_H
//table.h
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

struct node
{
int data;
node * left;
node * right;;
};

void build(node * & root); //supplied
void display(node * root); //supplied
void destroy(node * & root); //supplied

int countNodes(node* root);
int sumLeaves(node* root);

#endif

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

//table.cpp

#include "table.h"

int countNodes(node* root){
   if(root == NULL) return 0;
   return 1 + countNodes(root->left) + countNodes(root->right);
}
int sumLeaves(node* root){
   if (root == NULL) return 0;
  
   if (root->left == NULL && root->right == NULL)
            return root->data;
    else
        return sumLeaves(root->left) + sumLeaves(root->right);
}

Add a comment
Know the answer?
Add Answer to:
For this lab, you will need to write the following functions in table.cpp, and add function...
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
  • 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...

  • 3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that...

    3. (Gaddis Exercises 20.4) Tree Height Write a recursive member function for the BinaryTree class that returns the height of the tree. The height of the tree is the number of levels it contains. Demonstrate the function in a driver program. CPP FILE CODE: #include "BinaryTree.h" #include <iostream> using namespace std; BinaryTree::BinaryTree() { root = NULL; } BinaryTree::~BinaryTree() { destroy(root); } bool BinaryTree::search(int data) { return search(data, root); } void BinaryTree::insert(int data) { insert(data, root); } void BinaryTree::traverseInOrder() { traverseInOrder(root);...

  • C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== //...

    C++, implement the bst.cpp file without adding any additional methods or functions to it ======================================================================== // bst.cpp #include <iostream> #include "bst.h" using namespace std; BinarySearchTree::BinarySearchTree() { root = nullptr; } void BinarySearchTree::insert(int key, string val) { Node* new_node = new Node; new_node->key = key; new_node->val = val; new_node->left = nullptr; new_node->right = nullptr; if (root == nullptr) { root = new_node; } else { insertHelper(root, new_node); } } void BinarySearchTree::insertHelper(Node* parent, Node* new_node) { if (new_node->key < parent->key) { if...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • class AVLTree The following functions are the minimum requirements for the AVL class. You can add...

    class AVLTree The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You should modify the BSTree insert function so that the tree remains balanced after each insertion. Required Public Member Functions void insert(const string &): Insert an item to the binary search tree and perform rotation if necessary. int balanceFactor(Node*): Return the balance factor of a given node. void printBalanceFactors(): Traverse and print the tree in inorder...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • Add this four functions to the program:   push_back, pop_back   push_front, pop_front The functions should add or...

    Add this four functions to the program:   push_back, pop_back   push_front, pop_front The functions should add or delete from the front or back. The array will need to be resized and copied. Program: main.cpp: #include <iostream> //Input/Output Library using namespace std; //User Libraries #include "SimpleObject.h" //Global Constants, no Global Variables are allowed //Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc... //Function Prototypes Simple *fillSmp(int); void prntSmp(Simple *,int); void dstrSmp(Simple *); void push_front(Simple *,int&n,int); //Execution Begins Here! int main(int argc, char** argv) {...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use...

    C++ Binary tree, help pls #pragma once // include this library to use NULL, otherwise use nullptr instead #include <cstddef> #include <iostream> #include "node.hpp" template<class T> class BST{ public: // Constructor for the BST class, creates an empty tree BST(void); // Destructor for the BST class, destroys the tree ~BST(void); // Inserts data into the tree // param: The data to be inserted into the tree void insert(T); // Removes data from the tree // param: The data to be...

  • Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This...

    Using C++ 11, Create the following function based on the criteria mentioned below: node.hpp             ○This will contain your ​node struct○value ​is type int             ○next​ is type Node*             ○(optional) ​prev​ is type Node* nodeFunctions.hpp           ○This will contain the headers for all the functions you need to display, add, and remove                nodes from your list             ■displayList ­ displays the entire list from beginning to end. If no elements let the user       know the list is...

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