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

Using the binary_tree_node, write a recursive function to meet the following spe

ID: 3816370 • Letter: U

Question

Using the binary_tree_node, write a recursive function to meet the following specification. Check as much of the precondition as possible.

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

#include<bits/stdc++.h>

using namespace std;

void flip(binary_tree_node<Item>* root_ptr)
{
   if(root_ptr == NULL)
       return
   swap(root_ptr->left, root_ptr->right); //most essential step- swap the two children
   flip(root_ptr->left); // do this swapping recursively.
   flip(root_ptr->right);
}

sample i/p:

1
/
2 3

Sample op
1
/
3 2

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