# Binary Tree to Mirror Image in Data Structure

An essential data structure in computer science & programming is the binary tree. They are made out by a group of nodes, every one of which has two or fewer children. A variety of operations can be carried out on binary trees, which are used to hold hierarchical data like file systems or family trees.

Making a mirror image of a binary tree is a typical operation done on binary trees. By switching the left and right child nodes of each node in the binary tree, a mirror image of the tree is produced. With the root node maintaining in the same location, this procedure creates a new tree which is a reflection of the original tree.

A straightforward recursive approach can be used to create a binary tree mirror image. The method operates by moving around the tree while switching the left and right child nodes of every node.

Recursively calling itself from the left and right child nodes till it reach a leaf node, the algorithm begins at the root node. When reaching a leaf node, the algorithm goes back to its parent node and switches the left and right child connections finally going back to the original node.

### Flipping a Binary Tree:

Steps:

A binary tree is flipped by going to each tree node and switching the left and right subtrees. Recursively carrying out this process from the tree's root node onward is possible.

The steps for inverting a binary tree are as follows:

• Verify whether the tree's root node is null. In that case, return null.
• Change the root node's left and right subtrees.
• Flip the root node's left subtree repeatedly.
• Flip the root node's right subtree repeatedly.
• Return the flipped tree's root node.

Python Implementation Example:These actions can be carried out in Python by using a straightforward function. The binary tree's root node is passed into the function, which then outputs the binary tree's flipped root node.

``````class Node:
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right

def flip_tree(root):
if root is None:
return None

# Swap the left and right subtrees of the root node
root.left, root.right = root.right, root.left

# Recursively flip the left subtree of the root node
flip_tree(root.left)

# Recursively flip the right subtree of the root node
flip_tree(root.right)

return root
``````

A Node class which depicts a node inside the binary tree is defined in the implementation discussed above. The flip tree function swaps the left and right subtrees of the tree by starting with the root node.

In computer science, binary trees are indeed a fundamental type of data structure. Each of the nodes that make them up can only have two child nodes. The left child as well as the right child nodes are the two child nodes. The node in the tree that occurs before the right child is called the left child. The root node is the tree's highest point.

A graphic representation of a binary tree shows the root node just at top and the child nodes spreading off from it. Here is an illustration of a binary tree: Both left and right child connections of each node in a binary tree must be switched in order to create the binary tree's mirror image. The opposite of the binary tree shown above is shown here: Each node's left and right child nodes have been switched, as you can see.

Let's go step by step through the process of mirroring a binary tree. We'll use the binary tree shown above as an illustration.

Begin the with root node (node 5).

Change the root node's left and right child nodes. For every child node, repeat step 2 recursively.

For the root node's (node 3) left child node: For the right child node of the root node (node 8): For the left child node of node 3 (node 4): For the right child node of node 8 (node 9): For the left child node of node 4 (node 2): In conclusion, we swap both left and right child nodes of each node in a binary tree to obtain the binary tree's mirror image.

Recursively applying this swap to every child node after starting at the root node allows us to process every node in the tree.