This is all done in c++ (can only use <iostream>) do not use any other libraries
ID: 3757592 • Letter: T
Question
This is all done in c++ (can only use <iostream>) do not use any other libraries
// USE THE BELOW FUNCTION Do NOT ALTER
// Check if a tree contained the value "value" given the head node in the tree. // will have return false; for the base case.
bool ContainsRecursive(const BinaryTreeNode* tree_node, int value)
THE ".h" file is given below;
struct BinaryTreeNode {
int data;
BinaryTreeNode* left_child;
BinaryTreeNode* right_child;
};
class BinaryTree {
public:
BinaryTree();
int Size();
bool Contains(int value);
bool Insert(int value);
bool Remove(int value);
void Clear();
std::string ToString();
BinaryTreeNode* head;
};
Explanation / Answer
we can find the value in a BST using recursion.
C++ implementation of the above code:
bool contains(int key){
Node* p =root;
while((p!=NULL)&&(p->data!=key)){
if(key< p->data)p=p->left;
else p=p->right;
}
return p;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.