Question
PLEASE CODE IN C++, PLEASE CODE THE void determineDepth in the tree.hpp section!!! DO NOT WRITE A NEW BINARY TREE JUST CODE THE FUNCTION void
PLEASE CODE IN C++, PLEASE CODE THE void determineDepth in the tree.hpp section!!!
DO NOT WRITE A NEW BINARY TREE JUST CODE THE FUNCTION void determinDepth!
----------------------------------------------------------------
For this assignment, you will complete the binary search tree class by writing the member functions binaryTreeSearch and determineDepth. Search attempts to locate a specified value in a binary search tree object. Depth calculates the depth of the tree. The search function should take as arguments a pointer to the root node of the binary tree and a search key to be located. If the node containing the search key is found, the function should return a pointer to that node; otherwise, the function should return a null pointer. The depth function should take as arguments a pointer to the root node of the binary tree and two pointers, the total depth pointer, and the current depth pointer.
For this assignment you will need to complete the member functions that perform the following tasks:
determineDepth this member function calculate the depth of the tree.
binarySearchHelper this member function will perform the binary search on the tree. This function should have display logic exactly as shown below (in yellow).
Treemain.cpp
#include #include #include #include "Tree.hpp"
int main() { srand(time(nullptr)); // randomize the random number generator
Tree intTree; int intVal;
std::cout << "The values being placed in the tree are: ";
// generate a tree with values for (int i{1}; i <= 15; ++i) { intVal = rand() % 100; std::cout << intVal << ' '; intTree.insertNode(intVal); }
std::cout << " Enter a value to search for: "; std::cin >> intVal;
// create a pointer with the user value TreeNode* ptr{intTree.binaryTreeSearch(intVal)};
// a value is found if (ptr != nullptr) { std::cout << ptr->getData() << " was found "; } else { // value not found std::cout << "Element was not found "; }
std::cout << std::endl; }
tree.hpp // PLEASE CODE THE FUNCTION DETERMINEDEPTH!!!!
#ifndef TREE_HPP
#define TREE_HPP
#include
#include "Treenode.hpp"
// Tree class-template definition
template class Tree {
public:
// insert node in Tree
void insertNode(const NODETYPE& value) {
insertNodeHelper(&rootPtr, value);
}
// begin preorder traversal of Tree
void preOrderTraversal() const {
preOrderHelper(rootPtr);
}
// begin inorder traversal of Tree
void inOrderTraversal() const {
inOrderHelper(rootPtr);
}
// begin postorder traversal of Tree
void postOrderTraversal() const {
postOrderHelper(rootPtr);
}
// get the depth of the tree
int getDepth() const {
int totalDepth{0};
int currentDepth{0};
determineDepth(rootPtr, &totalDepth, tDepth);
return totalDepth;
}
// begin binary search
TreeNode* binaryTreeSearch(int val) const {
return binarySearchHelper(rootPtr, val);
}
private:
TreeNode* rootPtr{nullptr};
// utility function called by insertNode; receives a pointer
// to a pointer so that the function can modify pointer's value
void insertNodeHelper(
TreeNode** ptr, const NODETYPE& value) {
// subtree is empty; create new TreeNode containing value
if (*ptr == nullptr) {
*ptr = new TreeNode(value);
}
else { // subtree is not empty
// data to insert is less than data in current node
if (value <= (*ptr)->data) {
insertNodeHelper(&((*ptr)->leftPtr), value);
}
else {
insertNodeHelper(&((*ptr)->rightPtr), value);
}
}
}
// utility function to perform preorder traversal of Tree
void preOrderHelper(TreeNode* ptr) const {
if (ptr != nullptr) {
std::cout << ptr->data << ' '; // process node
preOrderHelper(ptr->leftPtr); // traverse left subtree
preOrderHelper(ptr->rightPtr); // traverse right subtree
}
}
// utility function to perform inorder traversal of Tree
void inOrderHelper(TreeNode* ptr) const {
if (ptr != nullptr) {
inOrderHelper(ptr->leftPtr); // traverse left subtree
std::cout << ptr->data << ' '; // process node
inOrderHelper(ptr->rightPtr); // traverse right subtree
}
}
// utility function to perform postorder traversal of Tree
void postOrderHelper(TreeNode* ptr) const {
if (ptr != nullptr) {
postOrderHelper(ptr->leftPtr); // traverse left subtree
postOrderHelper(ptr->rightPtr); // traverse right subtree
std::cout << ptr->data << ' '; // process node
}
}
// calculate the depth of the tree
void determineDepth(.....) {
}
}
// do a binary search on the Tree
TreeNode* binarySearchHelper(.....) {
}
};
#endif // TREE_HPP
#ifndef TREENODE_HPP #define TREENODE_HPP
// TreeNode class-template definition.
// forward declaration of class Tree template class Tree;
// TreeNode class-template definition template class TreeNode { friend class Tree; public: // constructor TreeNode(const NODETYPE& d) : data{d} {}
// return copy of node's data NODETYPE getData() const {return data;}
// return a leftPtr TreeNode* getLeftPtr() const { return leftPtr; }
// return a rightPtr TreeNode* getRightPtr() const { return rightPtr; }
// set value for leftPtr void setLeftPtr(TreeNode* ptr) { leftPtr = ptr; }
// set value for rightPtr void setRightPtr(TreeNode* ptr) { rightPtr = ptr; } private: TreeNode* leftPtr{nullptr}; // pointer to left subtree NODETYPE data; TreeNode* rightPtr{nullptr}; // pointer to right subtree };
#endif // TREENODE_HPP
The specification files have been created for you, along with the driver program as well. The code can be downloaded and is named treemain.cpp (to test with), treenode.hpp which contains the TreeNode class-template definition, and the tree.hpp file which contains the Tree class-template definition. This is where you will find the member functions that you are to complete. You need only to update this file and submit that as the final assignment (only submit tree.hpp file). Please use the driver and make sure your program can execute any random test cases successfully.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started