-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBT_TO_BST.c
72 lines (60 loc) · 1.43 KB
/
BT_TO_BST.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
#include<stdio.h>
#include<stdlib.h>
struct treeNode{
int value;
struct treeNode *lptr;
struct treeNode *rptr;
};
void insert_val(struct treeNode ** root,int val){
struct treeNode * newNode,*temp;
temp = (*root);
newNode = (struct treeNode *)malloc(sizeof(struct treeNode));
if(newNode==NULL){
printf("error in malloc\n");
}
newNode->value = val;
newNode->lptr = NULL;
newNode->rptr = NULL;
if((*root) == NULL){
(*root) = newNode;
return;
}
while(1){
if(val < temp->value){
if(temp->lptr == NULL){
temp->lptr = newNode;
break;
}
temp = temp->lptr;
}
else{
if(temp->rptr == NULL){
temp->rptr = newNode;
break;
}
temp = temp->rptr;
}
}
}
void inorder(struct treeNode * r){
if(r != NULL){
inorder(r->lptr);
printf("%d ",r->value);
inorder(r->rptr);
}
}
int main(){
int n,val,bt[10]={0},i;
struct treeNode *root;
root = NULL;
printf("ENter how many node are there in your Binary tree\n");
scanf("%d",&n);
printf("Insert the value of binary tree in level order\n");
for(i=0;i<n;i++)
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
insert_val(&root,bt[i]);
printf("The inorder traversal of BST is\n");
inorder(root);
return 0;
}