Javatpoint Logo
Javatpoint Logo

Foldable binary tree in C++

In this article, we will describe the foldable binary tree in C++. In C++, a foldable binary tree is a tree data structure with mirror-image left and right subtrees for each node. The left and right subtrees should have the same structure if you can fold the tree along its center. This mirror symmetry is checked iteratively when implementing a foldable binary tree. The tree is foldable if each left and right children of the node overlap. Recursive algorithms in C++ can be used to achieve this characteristic, which is essential for some tree operations.

We can use different kinds of Approaches:

  1. Foldable Binary Trees by Switching to the Mirror of the Left Subtree
  2. Foldable Binary Trees can be found by determining whether the left and right subtrees are mirror images:
  3. By using the Breadth-First-Search Approach

1. Foldable Binary Trees by Switching to the Mirror of the Left Subtree

The objective is to transform the left subtree into its mirror and then compare it to its right subtree. You can follow these steps to solve the problem:

  • If the tree is empty, return true.
  • Next, transform the left subtree to its mirror image.
  • After that, check and save the result if the structure of both subtrees on the left and right is the same.
  • istheStructSame(root->left, root->right) = res. istheStructSame() recursively compares structures in two subtrees and returns true if the structures are the same.
  • Undo the changes done in step (2) to get back the original tree.
  • Finally, return the result res that was stored in step 3.

Filename: Is_foldable.cpp

Output:

The given tree is foldable.

2. Foldable Binary Trees can be found by determining whether the left and right subtrees are mirror images:

The objective is to determine whether the left and right subtrees are mirrors. You can follow these steps to solve the problem:

  • If the tree is empty, return true.
  • Otherwise, check if the left and right subtrees are structurally similar. Use the utility function IsFoldable(root->left, root->right).
  • Check whether m1 and m2 are mirror images of one other.
  • If neither tree is empty, return true.
  • If one is empty while the other one is not, return false.
  • If the following requirements are met, return true.
  • n1->left is a mirror image of n2->right.
  • n1->right is a mirror image of n2->left.

Filename: Is_foldable_mirror.cpp

Output:

The given tree is foldable.

3. By using the Breadth-First-Search Approach

The idea is to traverse the tree using Queue and the BFS technique. You can follow these steps to solve the problem:

  • The right child of the right subtree is the left child of the left subtree. Each of them should be valid.
  • The left child of the right subtree equals the right child of the left subtree. They should both be null or not null.

Filename: IsFoldableTree.cpp

Output:

The given tree is foldable.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA