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

Sample Data Structures Questions Chapter 10 Trees (provide correct specific shor

ID: 3721237 • Letter: S

Question

Sample Data Structures Questions
Chapter 10
Trees (provide correct specific short answer in c++)

Using the binary_tree_node from Section 10.3, write a function to meet the following specification. You do not need to check the precondition.

template

bool has_42(binary_tree_node* root_ptr)

//Precondition: root_ptr is the root pointer of a binary tree (but NOT NECESSARILY a search tree).

//Postcondition: The return value indicates whether 42 appears somewhere

//in the tree. NOTE: If the tree is empty, the function returns false.

Explanation / Answer

Let the binary tree structure is defined as:

struct binary_tree_node {

binary_tree_node *left, *right;

int data;

}

Then,

//Code Begin

bool has_42(binary_tree_node *root_ptr) {

if(root_ptr == NULL)

return false;

if(root_ptr -> data == 42)

return true;

else if(root_ptr->data > 42)

return has_42(root_ptr->left);

return has_42(root_ptr->right);

}

//Code End

Please upvote if you've found this answer helpful.

Thank you.

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