In: Computer Science
Hi. Please could I get a complete answer to the question below using c++ and codeblocks
Add a recursive member function to bSearchTreeType that returns
the depth of a given node whose info member contains a specific
value, and returns -1 if the node is not in the tree. For example,
tree in figure 11-8 on page 622 of Malik,z the depth of 40 is 4,
the depth of node 50 is 1 etc.
Use the following header:
template<class elemType>
int bSearchTreeType<elemType>:: int nodeLevel( const elemType
& value
Thank you
/* We are not allowed to see book */
/* Below down is working function based on given information */
template<class elemType>
int nodelevel(const elemType & value)
{
queue<node<elemType>*> q;
int level = 1;
q.push(root);
// extra NULL is pushed to keep track
// of all the nodes to be pushed before
// level is incremented by 1
q.push(NULL);
while (!q.empty()) {
node<elemType>* temp = q.front();
q.pop();
if (temp == NULL) {
if (q.front() != NULL) {
q.push(NULL);
}
level += 1;
} else {
if (temp->value == value) {
return level;
}
if (temp->left) {
q.push(temp->left);
}
if (temp->right) {
q.push(temp->right);
}
}
}
return -1;
}