Skip to content

Commit d1e68f4

Browse files
authored
Merge pull request kothariji#583 from Bikash0007/patch-3
Foldable Binary Trees
2 parents 00950f1 + 6fa100c commit d1e68f4

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

Tree/foldable_btree.cpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/* You would want to remove below
5+
3 lines if your compiler supports
6+
bool, true and false */
7+
#define bool int
8+
#define true 1
9+
#define false 0
10+
11+
/* A binary tree node has data,
12+
pointer to left child and a
13+
pointer to right child */
14+
class node {
15+
public:
16+
int data;
17+
node* left;
18+
node* right;
19+
};
20+
21+
/* converts a tree to its mirror image */
22+
void mirror(node* node);
23+
24+
/* returns true if structure of
25+
two trees a and b is same only
26+
structure is considered for comparison, not data! */
27+
bool isStructSame(node* a, node* b);
28+
29+
/* Returns true if the given tree is foldable */
30+
bool isFoldable(node* root)
31+
{
32+
bool res;
33+
34+
/* base case */
35+
if (root == NULL)
36+
return true;
37+
38+
/* convert left subtree to its mirror */
39+
mirror(root->left);
40+
41+
/* Compare the structures of the
42+
right subtree and mirrored
43+
left subtree */
44+
res = isStructSame(root->left, root->right);
45+
46+
/* Get the original tree back */
47+
mirror(root->left);
48+
49+
return res;
50+
}
51+
52+
bool isStructSame(node* a, node* b)
53+
{
54+
if (a == NULL && b == NULL) {
55+
return true;
56+
}
57+
if (a != NULL && b != NULL && isStructSame(a->left, b->left) && isStructSame(a->right, b->right)) {
58+
return true;
59+
}
60+
61+
return false;
62+
}
63+
64+
/* UTILITY FUNCTIONS */
65+
/* Change a tree so that the roles of the left and
66+
right pointers are swapped at every node.
67+
See https:// www.geeksforgeeks.org/?p=662 for details */
68+
void mirror(node* Node)
69+
{
70+
if (Node == NULL)
71+
return;
72+
else {
73+
node* temp;
74+
75+
/* do the subtrees */
76+
mirror(Node->left);
77+
mirror(Node->right);
78+
79+
/* swap the pointers in this node */
80+
temp = Node->left;
81+
Node->left = Node->right;
82+
Node->right = temp;
83+
}
84+
}
85+
86+
/* Helper function that allocates a new node with the
87+
given data and NULL left and right pointers. */
88+
node* newNode(int data)
89+
{
90+
node* Node = new node();
91+
Node->data = data;
92+
Node->left = NULL;
93+
Node->right = NULL;
94+
95+
return (Node);
96+
}
97+
98+
/* Driver program to test mirror() */
99+
int main(void)
100+
{
101+
/* The constructed binary tree is
102+
1
103+
/ \
104+
2 3
105+
\ /
106+
4 5
107+
*/
108+
node* root = newNode(1);
109+
root->left = newNode(2);
110+
root->right = newNode(3);
111+
root->right->left = newNode(4);
112+
root->left->right = newNode(5);
113+
114+
if (isFoldable(root) == 1) {
115+
cout << "tree is foldable";
116+
}
117+
else {
118+
cout << "\ntree is not foldable";
119+
}
120+
return 0;
121+
}

0 commit comments

Comments
 (0)