-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray_to_binary_tree.c
66 lines (66 loc) · 1.41 KB
/
array_to_binary_tree.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
//array to BT
#include<stdio.h>
#include<stdlib.h>
struct t_node{
int data;
struct t_node *left;
struct t_node *right;
};
typedef struct t_node TreeNode;
void in_order(TreeNode *root){
if(root!=NULL){
in_order(root->left);
printf("%d ",root->data);
in_order(root->right);
}
}
TreeNode *create_tree_node(int val){
TreeNode *nn=(TreeNode *)malloc(sizeof(TreeNode));
nn->data=val;
nn->left=NULL;
nn->right=NULL;
return nn;
}
TreeNode *constructBT(int *a,int n){
TreeNode *q[100];
int front,rear;
front=rear=0;
q[rear]=create_tree_node(a[0]);
rear++;
int i=0;
while(1){
if(2 * i+1>=n){
break;
}
if(2*i+1<n){
//creating a node with value at 2*i+1 index
TreeNode *t=create_tree_node(a[2*i+1]);
q[rear++]=t;//adding it to queue
q[front]->left=t;//connecting the left child
}
if(2*i+2<n){
//creating a node with value at 2*i+2 index
TreeNode *t=create_tree_node(a[2*i+2]);
q[rear++]=t;//adding it to queue
q[front]->right=t;//connecting the right child
}
front++;
i++;
}
return q[0];//root of constructed binary tree
}
int main(){
int n;
printf("enter the size of array:");
scanf("%d",&n);
int a[n];
printf("enter the array elements:\n");
int i;
for( i=0;i<n;i++){
scanf("%d",&a[i]);
}
//RETURN THE ROOT OF THE created BINARY TREE
TreeNode *root=constructBT(a,n);
//checking the tree
in_order(root);
}