-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_tree_preorder.c
80 lines (74 loc) · 1.61 KB
/
build_tree_preorder.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
#include<stdlib.h>
#include<stdio.h>
struct node
{
struct node *left;
struct node *right;
int data;
};
struct node *newNode(int info);
struct node *construct(int preorder[],char array[],int n);
struct node *construct_preorder(int preorder[],char array[],int *index,int n)
{
int index_array=*index;
if(index==n)
return ;
struct node *root=newNode(preorder[index_array]);
(*index)++;
if(array[index_array]=='N')
{
root->left=construct_preorder(preorder,array,index,n);
root->right=construct_preorder(preorder,array,index,n);
}
return root;
}
struct node *construct(int preorder[],char array[],int n)
{
int index=0;
return construct_preorder(preorder,array,&index,n);
}
struct node *newNode(int info)
{
struct node *node=(struct node*)malloc(sizeof(struct node ));
node->data=info;
node->left=NULL;
node->right=NULL;
return (node);
}
void preorder(struct node *root)
{
if(root==NULL)
return ;
printf("%d\n",root->data);
preorder(root->left);
preorder(root->right);
printf("\n");
}
void inorder(struct node *root)
{
if(root==NULL)
return;
inorder(root->left);
printf("%d\n",root->data);
inorder(root->right);
}
int main()
{
struct node *root = NULL;
/* Constructing tree given in the above figure
10
/ \
30 15
/ \
20 5 */
int pre[] = {10, 30, 20, 5, 15};
char preLN[] = {'N', 'N', 'L', 'L', 'L'};
int n = sizeof(pre)/sizeof(pre[0]);
// construct the above tree
root = construct (pre, preLN, n);
// Test the constructed tree
printf("Following is Inorder Traversal of the Constructed Binary Tree: \n");
preorder(root);
inorder(root);
return 0;
}