-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
binary_tree_from_preorder_and_inorder.c
164 lines (150 loc) · 4.03 KB
/
binary_tree_from_preorder_and_inorder.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Construction of binary tree from inorder and preorder traversal
binary tree can be uniquely constructed if it's preorder and inorder traversals
are given.
The following steps need to be followed
1. From the preorder traversal , it is evident that the first element is the root node
2. In inorder traversal, all the nodes which are on the left side of root (from preorder)
belong to the left sub-tree and those which are on right side of the root (from preorder)
are on right side of root (from preorder) belong to right sub-tree
3. Now the problem reduces to form sub-trees and the same procedure can be applied
repeatedly.
*/
#include <stdio.h>
#include <stdlib.h>
//struct for binary tree
typedef struct Tree
{
char data;
struct Tree *left;
struct Tree *right;
} TreeNode;
// search function searches the inorder traversal and returns the index if found
int search(char in[], int start, int end, char key)
{
for (int i = start; i <= end; i++)
{
if (in[i] == key)
{
return i;
}
}
}
// Build Tree Builds the tree from preorder and inorder traversals
TreeNode *BuildTree(char in[], char pre[], int start, int end)
{
//preindex is static as it should get incremented after each recursive calls
static int preindex = 0;
if (start > end)
{
return NULL;
}
//get New node and make left and right as null
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->data = pre[preindex];
node->left = NULL;
node->right = NULL;
preindex++;
//if there is only one node return that node
if (start == end)
{
return node;
}
// find the location of the root from the preorder traversal in the inorder
//traversal and store it in loc
int loc = search(in, start, end, node->data);
//the elements to the left of the inorder traversal forms the left subtree
node->left = BuildTree(in, pre, start, loc - 1);
// the elements to the right of the inorder traversal forms the right subtree
node->right = BuildTree(in, pre, loc + 1, end);
return node;
}
void inorder(TreeNode *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%c ", root->data);
inorder(root->right);
}
}
void preorder(TreeNode *root)
{
if (root != NULL)
{
printf("%c ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(TreeNode *root)
{
if (root != NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c ", root->data);
}
}
void printTree(TreeNode *root)
{
printf("\nThe inorder traversal of the tree is\n");
inorder(root);
printf("\nThe preorder traversal of the tree is\n");
preorder(root);
printf("\nThe postorder traversal of the tree is\n");
postorder(root);
printf("\n");
}
int main()
{
printf("Enter the number of nodes in the binary tree \n");
int n;
scanf("%d", &n);
char *pre = (char *)malloc(sizeof(char) * (n + 1));
char *in = (char *)malloc(sizeof(char) * (n + 1));
printf("Enter the preorder traversal of the tree\n");
for (int i = 0; i < n; i++)
{
scanf(" %c", &pre[i]);
}
printf("Enter the inorder traversal of the tree \n");
for (int i = 0; i < n; i++)
{
scanf(" %c", &in[i]);
}
TreeNode *root = BuildTree(in, pre, 0, n - 1);
printTree(root);
return 0;
}
/*
Sample I/O :
1)
Enter the number of nodes in the binary tree
6
Enter the preorder traversal of the tree
A B D E C F
Enter the inorder traversal of the tree
D B E A F C
The inorder traversal of the tree is
D B E A F C
The preorder traversal of the tree is
A B D E C F
The postorder traversal of the tree is
D E B F C A
2)
Enter the number of nodes in the binary tree
4
Enter the preorder traversal of the tree
A B D C
Enter the inorder traversal of the tree
B D A C
The inorder traversal of the tree is
B D A C
The preorder traversal of the tree is
A B D C
The postorder traversal of the tree is
D B C A
Time complexity : O( n^2 )
Space complexity : O( n )
*/