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"#includeusing 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 BSTBST::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() <> { temp = temp->getLeft(); parent = temp; } else { cout < "duplication="" is="" not=""> return NULL; } } return parent;}bool BST::isEmpty() { if (root == NULL) { return true; } else { return false; }}void search(int);#include"BST.h"#includeusing namespace std;int main() { BST bst; bst.insert(4); bst.insert(5); bst.insert(8); bst.insert(2); system("pause"); return 0;}
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 BSTBST::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() <> { temp = temp->getLeft(); parent = temp; } else { cout < "duplication="" is="" not=""> return NULL; } } return parent;
}bool BST::isEmpty() { if (root == NULL) { return true; } else { return false; }}void search(int);
#include"BST.h"#includeusing namespace std;int main() { BST bst; bst.insert(4); bst.insert(5); bst.insert(8); bst.insert(2); system("pause"); return 0;}
int main() {
BST bst;
bst.insert(4); bst.insert(5); bst.insert(8); bst.insert(2);
system("pause"); return 0;}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here