Using the binarytreenode from page 465 write a function to m
Using the binary_tree_node from page 465, write a function to meet the following specification. You do not need to check the precondition.
..................................................
Using the binary_tree_node from page 465, write a function to meet the following specification. You do not need to check the precondition.
class, stack class, au We will start with only two funcUILJ FIGURE 10.6 The Binary Tree Node Definition Class Definition template class binary_tree_node public: / TYPEDEF typedef Item value type; // CONSTRUCTOR binary tree_node( const Item& init_data = Item( ), binary-tree-node* init_1 eft = NULL, binary tree_node* init.right NULL data-field- 1 eft-field right-field = init-data; i ni t-left; = init-right; // MODIFICATION MEMBER FUNCTIONS Item& data() return data_field; binary tree_node*& leftC) return left field; binary tree_node*& right( return right field; b void set_data(const Item& new_data) f data field new_data; void set left(binary_tree_node* new left) left field - newleft: void set-right(binary-tree-node* new-right) { right-field = new-right // CONSTANT MEMBER FUNCTIONS const Item& data() const f return data field; nst binary tree_node* left() co nst treturn left field; const binary-t const binary tree.node* right( const t return right field; bool isleaf() const return (left field NUL (right-field == NULL); } L) && private: Item data field; binary tree_node *left field: binary-tree-nod e right field; 3: www.cs.colorado.edu/-main/chapter10/bin tree.hSolution
bool has_42(binary_tree_node* node){
if(node==null){
return FALSE;
}
if(node->data_field==42){
return TRUE;
}
else if (node->data_field<42){
return bool has_42(node->right_field)
}
return bool has_42(node->left_field)
}
For counting the number of times 42 is there, however if the same number exists more than once or not depends on the definition of the binary tree. It can be dome by inorder traversal and checking if consecutive elements are 42 or not with a counter.
static int count = 0;
size_t count42(binary_tree_node* node)
{
if (node == NULL)
return count;
count42(node->left); //recur left
//check current
if(node->data_field >42){
return count;
}
else if(node->data_field ==42){
count ++;
}
//recur right
count42(node->right);
}

