Question: using c++ help me modify my code below so that it can Write a function with prototype void stars (int i); The function prints a
using c++ help me modify my code below so that it can Write a function with prototype void stars (int i); The function prints a line of i stars. How can you easily apply the stars function to every node in a binary tree using an in-order traversal?
please Test the function in main() and provide a running program and show me an out put too.
/**************BinaryTree.h******************/ #ifndef BINARYTREE_H #define BINARYTREE_H #includeusing namespace std; //Stack template template <class T> class BinaryTree { private: struct TreeNode { T value; //The value in the node TreeNode *left; //Pointer to left child node TreeNode *right; //Pointer to right child node }; TreeNode *root; //Pointer to the root node //Private member functions void insert(TreeNode*&, TreeNode *&); void destroySubTree(TreeNode *); void deleteNode(T, TreeNode *&); void makeDeletion(TreeNode *&); void displayInOrder(TreeNode *) const; void displayPreOrder(TreeNode *) const; void displayPostOrder(TreeNode *) const; int counter(TreeNode *); int leafCounter(TreeNode *); int getTreeHeight(TreeNode* nodePtr); int countNodes(TreeNode *&nodePtr) ; int countLeaves(TreeNode* nodePtr); public: //Constructor BinaryTree() {root = NULL;} //Destructor ~BinaryTree () {destroySubTree(root);} //Binary tree operations void insertNode(T); bool searchNode(T); void remove(T); int treeHeight(); int numNodes(); int numLeafNodes(); void displayInOrder() const {displayInOrder(root);} void displayPreOrder() const {displayPreOrder(root);} void displayPostOrder() const {displayPostOrder(root);} // Node counter int counter() { int n = counter(root); return n; } // Leaf counter int leafCounter() { int leaf = leafCounter(root); return leaf; } // Height of the tree int height() { int h = treeHeight(); return h; } }; //********************************************************** //Insert accepts a TreeNode pointer and a pointer to a node* //The function inserts the node into the tree pointed to by* //the TreeNode pointer. This function is called recursively* //********************************************************** template <class T> void BinaryTree ::insert(TreeNode *&nodePtr, TreeNode *&newNode) { if (nodePtr == NULL) nodePtr = newNode; // Insert the node else if (newNode->value < nodePtr->value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode);// Search the right branch } //******************************************************* //insertNode create as new node to hold num as its value* //and passes it to the insert function. * //******************************************************* template <class T> void BinaryTree ::insertNode(T item) { TreeNode *newNode; //Pointer to a new node //Create a new node and store num in it newNode = new TreeNode; newNode->value = item; newNode->left = newNode->right = NULL; //Insert the node insert(root, newNode); } //*********************************************** //destroySubTree is called by the destructor. It* //deletes all nodes in the tree * //*********************************************** template <class T> void BinaryTree ::destroySubTree(TreeNode *nodePtr) { if (nodePtr) { if (nodePtr->left) destroySubTree(nodePtr->left); if (nodePtr->right) destroySubTree(nodePtr->right); delete nodePtr; } } //*********************************************** //searchNode determines if a value is present in* //the tree. If so, the function returns true. * //Otherwise, it returns false. * //*********************************************** template <class T> bool BinaryTree ::searchNode(T item) { TreeNode *nodePtr = root; while (nodePtr) { if (nodePtr->value == item) return true; else if (item < nodePtr->value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; } return false; } //********************************************** //remove calls deleteNode to delete the * //node whose value member is thedame as num * //********************************************** template <class T> void BinaryTree ::remove(T item) { deleteNode(item, root); } //********************************************** //deleteNode deletes the node whose value * //memeber id the same as num * //********************************************** template <class T> void BinaryTree ::deleteNode(T item, TreeNode *&nodePtr) { if (item < nodePtr->value) deleteNode(item, nodePtr->left); else if (item > nodePtr->value) deleteNode(item, nodePtr->right); else makeDeletion(nodePtr); } //******************************************************* //makeDeletion takes a refrence to a pointer to the node* //that is to be deleted. The node is removed and the * //branches of the tree below the node are reattached * template <class T> void BinaryTree ::makeDeletion(TreeNode *&nodePtr) { //Define a temporary pointer to use in reattaching //the ledt subtree TreeNode *tempNodePtr; if (nodePtr == NULL) cout << "Cannot delete empty node. "; else if (nodePtr->right == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->left; //Reattach the ledt child delete tempNodePtr; } else if (nodePtr->left == NULL) { tempNodePtr = nodePtr; nodePtr = nodePtr->right; //Reattach the right child delete tempNodePtr; } //If the node has two children else { //Move one node to the right tempNodePtr = nodePtr->right; //Go to the end left node while (tempNodePtr->left) tempNodePtr = tempNodePtr->left; //Reattach the ledt subtree tempNodePtr->left = nodePtr->left; tempNodePtr = nodePtr; //Reattach the right subtree nodePtr = nodePtr->right; delete tempNodePtr; } } //************************************************************** //the desplayInOrder member function display the values * //in the subtree pointerd to by nodePtr, via inorder trancaesal* //************************************************************** template <class T> void BinaryTree ::displayInOrder(TreeNode *nodePtr) const { if (nodePtr) { displayInOrder(nodePtr->left); cout << nodePtr->value << endl; displayInOrder(nodePtr->right); } } //************************************************************** //the desplayPreOrder member function display the values * //in the subtree pointerd to by nodePtr, via inorder trancaesal* //************************************************************** template <class T> void BinaryTree ::displayPreOrder(TreeNode *nodePtr) const { if (nodePtr) { cout << nodePtr->value << endl; displayPreOrder(nodePtr->left); displayPreOrder(nodePtr->right); } } //************************************************************** //the desplayPostOrder member function display the values * //in the subtree pointerd to by nodePtr, via inorder trancaesal* //************************************************************** template <class T> void BinaryTree ::displayPostOrder(TreeNode *nodePtr) const { if (nodePtr) { displayPostOrder(nodePtr->left); displayPostOrder(nodePtr->right); cout << nodePtr->value << endl; } } //**************************************************************** //The numNodes function returns the number of nodes in the tree * //**************************************************************** template <class T> int BinaryTree ::numNodes() { return countNodes(root); } //******************************************************************* //the countNodes function uses recursion to count the nodes in * //the tree. This function is called by the public member function * //******************************************************************* template <class T> int BinaryTree ::countNodes(TreeNode *&nodePtr) { if (nodePtr == NULL) return 0; else return 1 + countNodes(nodePtr->left) + countNodes (nodePtr->right); } //*********************************************************************************** //Function numLeafNodes calls countLeaves() and displays nymber of leaf node in tree* //*********************************************************************************** template <class T> int BinaryTree ::numLeafNodes() { int leafCount = 0; //Reset to 0 each time a count is made leafCount=countLeaves(root); return leafCount; } //************************************************************** //Function countLeaves performs traversal and counts leaf nodes* //************************************************************** template <class T> int BinaryTree ::countLeaves(TreeNode* nodePtr) { static int leafCount=0; if (nodePtr) { if (nodePtr->left == NULL && nodePtr->right == NULL) { leafCount++; } countLeaves(nodePtr->left); countLeaves(nodePtr->right); } return leafCount; } //************************************************************** //Function getTreeHeight this function uses recursion to count * //the height of the tree * //************************************************************** template <class T> int BinaryTree ::getTreeHeight(TreeNode* nodePtr) { int leftHeight, rightHeight; if (nodePtr) { leftHeight = getTreeHeight(nodePtr->left); rightHeight = getTreeHeight(nodePtr->right); if (leftHeight > rightHeight) return leftHeight + 1; else return rightHeight + 1; } else { return 0; } } //******************************************************* //function TreeHeight calls getTreeHeight and display it* //******************************************************* template <class T> int BinaryTree ::treeHeight() { return getTreeHeight(root); } #endif
/***** Use header file BinaryTree.h yours have some errors which i have fixed in my BinaryTree.h file****/ #include "BinaryTree.h" using namespace std; int main(int argc, char *argv[]){ BinaryTree<int> bt; bt.insertNode(67); bt.insertNode(68); bt.insertNode(56); bt.insertNode(47); bt.insertNode(79); cout<<"Inoreder: "; bt.displayInOrder(); cout<<"PreOrder: "; bt.displayPreOrder(); cout<<"tree Height : "; cout<" Number of Leaf Nodes :"; cout<" Total Node Count: "; cout<" Serching 68 in Tree: "; if(bt.searchNode(68))cout<<" NODE FOUND"; else cout<<" NODE NOT FOUND"; cout<<" Searching 60 in Binary Tree: "; if(bt.searchNode(60))cout<<" NODE FOUND"; else cout<<" NODE NOT FOUND"; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
