template <class Item> void flip(binary_tree_node<Item>* root_ptr) // Preconditio
ID: 3570123 • Letter: T
Question
template <class Item>
void flip(binary_tree_node<Item>* root_ptr)
// Precondition: root_ptr is the root pointer of a non-empty binary tree.
// Postcondition: The tree is now the mirror image of its original value.
// Example original tree: Example new tree:
// 1 1
// / /
// 2 3 3 2
// / /
// 4 5 5 4
Explanation / Answer
Code for the flip function which is very much similar to the mirror function given as follows:
template <class Item>
void flip(binary_tree_node<Item>* root_ptr))
{
if (root_ptr ==NULL)
return;
else
{
binary_tree_node<Item>* temp;
// construct the subtrees
flip(root_ptr.init_left);
flip(root_ptr.init_right);
// swap the pointers in this node
temp = root_ptr.init_left;
root_ptr.init_left = root_ptr.init_right;
root_ptr.init_right = temp;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.