write search function for this coded class TreeNode { private: int data; TreeNode* left; TreeNode* right; public: TreeNode(); TreeNode(int); //getters and setters void setData(int); void setLeft(TreeNode*); void setRight(TreeNode*); int getData(); TreeNode* getLeft(); TreeNode* getRight(); }; #include"Node.h" class BST { private: TreeNode* root; public: BST(); void insert(int); bool isEmpty(); void search(int); TreeNode* findPlace(int); }; #include"BST.h" #include using namespace std; TreeNode::TreeNode() { data = -1; left = NULL; right = NULL; } TreeNode::TreeNode(int data) { this->data = data; } void TreeNode::setData(int data) { this->data = data; } void TreeNode::setLeft(TreeNode* left) { this->left = left; } void TreeNode::setRight(TreeNode* right) { this->right = right; } int TreeNode::getData() { return data; } TreeNode* TreeNode::getLeft() { return left; } TreeNode*TreeNode::getRight() { return right; } //implementation of BST BST::BST() { root = NULL; } void BST::insert(int data) { if (isEmpty()) { TreeNode* newNode = new TreeNode(data); root = newNode; } else { TreeNode* parent = findPlace(data); if (parent == NULL) { return; } else { TreeNode* newNode = new TreeNode(data); if (data > parent->getData()) { parent->setRight(newNode); } else { parent->setLeft(newNode); } } } } TreeNode* BST::findPlace(int data) { TreeNode* temp = root; TreeNode* parent = NULL; while (temp != NULL) { if (temp->getData() > data) { temp = temp->getRight(); parent = temp; } else if (temp->getData() < data) { temp = temp->getLeft(); parent = temp; } else { cout << "duplication is not allowed\n"; return NULL; } } return parent; } bool BST::isEmpty() { if (root == NULL) { return true; } else { return false; } } void search(int); #include"BST.h" #include using namespace std; int main() { BST bst; bst.insert(4); bst.insert(5); bst.insert(8); bst.insert(2); system("pause"); return 0; }
write search function for this coded
class TreeNode {
private:
int data;
TreeNode* left;
TreeNode* right;
public:
TreeNode();
TreeNode(int);
//getters and setters
void setData(int);
void setLeft(TreeNode*);
void setRight(TreeNode*);
int getData();
TreeNode* getLeft();
TreeNode* getRight();
};
#include"Node.h"
class BST {
private:
TreeNode* root;
public:
BST();
void insert(int);
bool isEmpty();
void search(int);
TreeNode* findPlace(int);
};
#include"BST.h"
#include<iostream>
using namespace std;
TreeNode::TreeNode() {
data = -1;
left = NULL;
right = NULL;
}
TreeNode::TreeNode(int data) {
this->data = data;
}
void TreeNode::setData(int data) {
this->data = data;
}
void TreeNode::setLeft(TreeNode* left) {
this->left = left;
}
void TreeNode::setRight(TreeNode* right) {
this->right = right;
}
int TreeNode::getData() {
return data;
}
TreeNode* TreeNode::getLeft() {
return left;
}
TreeNode*TreeNode::getRight() {
return right;
}
//implementation of BST
BST::BST() {
root = NULL;
}
void BST::insert(int data) {
if (isEmpty())
{
TreeNode* newNode = new TreeNode(data);
root = newNode;
}
else {
TreeNode* parent = findPlace(data);
if (parent == NULL) {
return;
}
else {
TreeNode* newNode = new TreeNode(data);
if (data > parent->getData())
{
parent->setRight(newNode);
}
else
{
parent->setLeft(newNode);
}
}
}
}
TreeNode* BST::findPlace(int data) {
TreeNode* temp = root;
TreeNode* parent = NULL;
while (temp != NULL)
{
if (temp->getData() > data) {
temp = temp->getRight();
parent = temp;
}
else if (temp->getData() < data)
{
temp = temp->getLeft();
parent = temp;
}
else
{
cout << "duplication is not allowed\n";
return NULL;
}
}
return parent;
}
bool BST::isEmpty() {
if (root == NULL)
{
return true;
}
else {
return false;
}
}
void search(int);
#include"BST.h"
#include<iostream>
using namespace std;
int main() {
BST bst;
bst.insert(4);
bst.insert(5);
bst.insert(8);
bst.insert(2);
system("pause");
return 0;
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)