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
//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);
}
For this lab, you will need to write the following functions in table.cpp, and add function...
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);...
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 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 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)...
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++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....
in C++ creat a DynamicQueue class, add a new data member called count to trace the total number of node you have in current queue (you need to modify some member functions for adding count). Add a member function called displayQueue() to display values stored in each node in the current queue, also the total number of nodes in the queue. You also need to have a driver program (refer to Tester) to test your modified new class and new...
Given list.h, main.cpp, i need to write list.cpp. having a lot of problems. please help. list.h: ////////////////////////////////////////////////////////////////////////// #ifndef LIST_H #define LIST_H ////////////////////////////////////////////////////////////////////////// namespace CS170 { struct node { int value; node *next; }; class list { public: // Constructor for list. Creates an empty list list(); /* Destructor for list. Empty the list and release the allocated memory */ ~list(); // Prints out the values contained in the list void print_list() const; // Returns the current size of the list...
I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public: // constructor with left and right NULL nodes Node(char charTemp, int c) { ch = charTemp; count = c; left...
I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...