Sample Data Structures Questions Chapter 10 Trees (provide correct specific shor
ID: 3721238 • 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
size_t count42(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 how many times 42 appears
// in the tree. NOTE: If the tree is empty, the function returns zero.
Explanation / Answer
size_t count42(binary_tree_node* root_ptr) { // if tree is null, then no count will be present if(root_ptr == NULL) { return 0; } // to store the count if this node has a value of 42 size_t count = 0; // I am assuming, binary_tree_node has three data elements //value, left and right if(root_ptr->value == 42) { count++; } // count if value is present in this node // and subsequently, go for the child trees return count + count42(root_ptr->left) + count42(root_ptr->right); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.