Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Plz use the flies that I provided, and photo Thank you! // main.cpp #include <io

ID: 3720924 • Letter: P

Question

Plz use the flies that I provided, and photo Thank you!
// main.cpp
#include <iostream> #include <cstdlib> #include <ctime> #include "Tree.hpp"
int main() { srand(time_t(nullptr)); // randomize the random number generator    Tree<int> intTree; int intVal;    std::cout << "The values being placed in the tree are: ";    // generate a tree with values for (int i{1}; i <= 32; ++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<int>* 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 #ifndef Tree_hpp #define Tree_hpp
#include <iostream> #include "Treenode.hpp"
// Tree class-template definition template<typename NODETYPE> 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<NODETYPE>* binaryTreeSearch(int val) const { return binarySearchHelper(rootPtr, val); }    private: TreeNode<NODETYPE>* 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<NODETYPE>** ptr, const NODETYPE& value) { // subtree is empty; create new TreeNode containing value if(*ptr == nullptr) { *ptr = new TreeNode<NODETYPE>(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<NODETYPE>* 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<NODETYPE>* 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<NODETYPE>* 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(TreeNode<NODETYPE> *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<NODETYPE> *binarySearchHelper(TreeNode <NODETYPE> *ptr, int Value) const { if (ptr == 0) return 0; // compare one value to another value... std::cout << "Comparing " << Value << " to " << ptr->data;    if ( Value == ptr->data) { // this should match the same value std::cout << "; search complete" << std::endl; return ptr; } else if (Value < ptr->data) { // search value less than current data std::cout << "; smaller, walk left" << std::endl; return binarySearchHelper(ptr->leftPtr, Value); } else { // search value greater than current data std::cout << "; larger, walk right" << std::endl; 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<typename NODETYPE> class Tree;
// TreeNode class-template definition template<typename NODETYPE> class TreeNode { friend class Tree<NODETYPE>; 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<NODETYPE>* leftPtr{nullptr}; // pointer to left subtree NODETYPE data; TreeNode<NODETYPE>* rightPtr{nullptr}; // pointer to right subtree };
#endif /* Treenode_hpp */

Explanation / Answer

#include<iostream.h>

#include<vector.h>

#include<conio.h>

using namespace std;

class node{

public:

int data;

node* ltree;

node* rtree;

node(int x){

data = x;

ltree = NULL;

rtree = NULL;

}

node(int x, node* node1, node* node2){

data = x;

ltree = node1;

rtree = node2;

}

~node(){};

};

class BST{

public:

node* root;

BST(){

root = NULL;

}

node* newNode(int x){

node* Node1 = new node(x);

return Node1;

}

node* insertNode(int x, node* n1);

};

node* BST::insertNode(int x, node* n1){

if(n1 == NULL){

n1 = newNode(x);

}

else{

if(x <= n1->data){

n1->ltree = insertNode(x,n1->ltree);

}

else if(x > n1->data){

n1->rtree = insertNode(x,n1->rtree);

}

}

return n1;

}

int main(){

BST BT1;

BT1.root = BT1.insertNode(4,BT1.root);

BT1.root = BT1.insertNode(5,BT1.root);

getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote