Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Plz use the flies that I provided, and photo Thank you! // main.cpp #include #include #include #include Tree.hpp int main() { srand(time_t(nullptr)); // randomize the
Plz use the flies that I provided, and photo Thank you! // main.cpp #include #include #include #include "Tree.hpp" int main() { srand(time_t(nullptr)); // randomize the random number generator Tree intTree; int intVal; std::cout // generate a tree with values for (int i{1}; i { intVal = rand() % 100; std::cout intTree.insertNode(intVal); } std::cout std::cin >> intVal; // create a pointer with the user value TreeNode* ptr{intTree.binaryTreeSearch(intVal)}; // a value is found if (ptr != nullptr) { std::cout getData() } else { // value not found std::cout } std::cout } ********************************************************************************** //Tree.hpp #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); } //optimize the tree to make the depth of the tree to be (Log2(n+1))-1 void Optimize() {} // get the depth of the tree int getDepth() { int totalDepth{0}; int currentDepth{0}; determineDepth(rootPtr, totalDepth, currentDepth); 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 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 data 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 data 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 data } } // calculate the depth of the tree void determineDepth(TreeNode *ptr, int *totalPtr, int *currentPtr) const { if (ptr != 0) { ++*currentPtr; if (*currentPtr > *totalPtr) *totalPtr = *currentPtr; determineDepth(ptr->leftPtr, totalPtr, currentPtr); determineDepth(ptr->rightPtr, totalPtr, currentPtr); --*currentPtr; } } // do a binary serch on the Tree TreeNode *binarySearchHelper(TreeNode *ptr, int Value) const { if (ptr == 0) return 0; // compare one value to another value... std::cout data; if ( Value == ptr->data) { // this should match the same value std::cout return ptr; } else if (Value data) { // search value less than current data std::cout return binarySearchHelper(ptr->leftPtr, Value); } else { // search value greater than current data std::cout return binarySearchHelper(ptr->rightPtr, Value); } } }; #endif /* Tree_hpp */ ***************************************************************************************** // Treenode.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 */ Project 3: Optimizing a tree Description: As you know, when you insert values to a tree, the values greater will go to the right side and the values smaller go to the left side. The tree will be extremely skewed if you insert values in the order from the smallest to the largest or from the largest to the smallest. For example, if you insert 31 values 1, 2, 3, 31 in the listed order, the tree will look like: The tree becomes a list (with the depth of 30) and it is not optimized for the search Your task is to write a member function to optimize the tree to make the depth of the tree to be (Login.-1 (n isthe number of nodes). For example, the above tree will be optimized to You can see the depth of the tree becomes 4. The tree of this structure is the best for the search. Please keep it in mind: your optimizing algorithm should not be hardcoded and should be able to apply to all binary trees regardless the content of the trees Instruction and Test: Use the 3 files (main.cpp, Tree hpp, and TreeNode.hpp iven to build this project. Develop an algorithm and implement it in a public member function called Optimize) with void return. To test your code, insert following code into your main function: Treecint optTree:; int depth; forlint in1: K32:i+//insert values 1,2,3 31 optTree inOrder Traversal)://you will output 1 23 31 std y Xc"The depth of the tree is:-
Plz use the flies that I provided, and photo
Thank you!
// main.cpp
#include
#include
#include
#include "Tree.hpp"
int main()
{
srand(time_t(nullptr)); // randomize the random number generator
Tree intTree;
int intVal;
std::cout
// generate a tree with values
for (int i{1}; i
{
intVal = rand() % 100;
std::cout
intTree.insertNode(intVal);
}
std::cout
std::cin >> intVal;
// create a pointer with the user value
TreeNode* ptr{intTree.binaryTreeSearch(intVal)};
// a value is found
if (ptr != nullptr) {
std::cout getData()
}
else { // value not found
std::cout
}
std::cout
}
**********************************************************************************
//Tree.hpp
#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);
}
//optimize the tree to make the depth of the tree to be (Log2(n+1))-1
void Optimize()
{}
// get the depth of the tree
int getDepth()
{
int totalDepth{0};
int currentDepth{0};
determineDepth(rootPtr, totalDepth, currentDepth);
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 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 data
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 data
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 data
}
}
// calculate the depth of the tree
void determineDepth(TreeNode *ptr, int *totalPtr, int *currentPtr) const
{
if (ptr != 0)
{
++*currentPtr;
if (*currentPtr > *totalPtr)
*totalPtr = *currentPtr;
determineDepth(ptr->leftPtr, totalPtr, currentPtr);
determineDepth(ptr->rightPtr, totalPtr, currentPtr);
--*currentPtr;
}
}
// do a binary serch on the Tree
TreeNode *binarySearchHelper(TreeNode *ptr, int Value) const
{
if (ptr == 0)
return 0;
// compare one value to another value...
std::cout data;
if ( Value == ptr->data)
{
// this should match the same value
std::cout
return ptr;
}
else if (Value data)
{
// search value less than current data
std::cout
return binarySearchHelper(ptr->leftPtr, Value);
}
else
{
// search value greater than current data
std::cout
Project 3: Optimizing a tree Description: As you know, when you insert values to a tree, the values greater will go to the right side and the values smaller go to the left side. The tree will be extremely skewed if you insert values in the order from the smallest to the largest or from the largest to the smallest. For example, if you insert 31 values 1, 2, 3, 31 in the listed order, the tree will look like: The tree becomes a list (with the depth of 30) and it is not optimized for the search Your task is to write a member function to optimize the tree to make the depth of the tree to be (Login.-1 (n isthe number of nodes). For example, the above tree will be optimized to You can see the depth of the tree becomes 4. The tree of this structure is the best for the search. Please keep it in mind: your optimizing algorithm should not be hardcoded and should be able to apply to all binary trees regardless the content of the trees Instruction and Test: Use the 3 files (main.cpp, Tree hpp, and TreeNode.hpp iven to build this project. Develop an algorithm and implement it in a public member function called Optimize) with void return. To test your code, insert following code into your main function: Treecint optTree:; int depth; forlint in1: K32:i+//insert values 1,2,3 31 optTree inOrder Traversal)://you will output 1 23 31 std y Xc"The depth of the tree is:- return binarySearchHelper(ptr->rightPtr, Value);
}
}
};
#endif /* Tree_hpp */
*****************************************************************************************
// Treenode.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 */
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