-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bst array.c
72 lines (66 loc) · 1.15 KB
/
Bst array.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>
#define SIZE 100
void create(int[],int ,int );
void inorder(int [],int);
void preorder(int[],int);
void postorder(int[],int);
main()
{
int no[SIZE];
int i,data,a;
for(i=1;i<SIZE;i++)
{
no[i]=-1;
}
do
{
printf("\nEnter the value: ");
scanf("%d",&data);
create(no,data,1);
printf("\nWish to continue? 1/2");
scanf("%d",&a);
}while(a==1);
printf("\nInorder :");inorder(no,1);
printf("\nPreorder :");preorder(no,1);
printf("\nPostorder :");postorder(no,1);
}
void create(int no[],int data, int index)
{
if(index==SIZE)
printf("\nSpace not available");
else
if(no[index]==-1)
no[index]=data;
else
if(data<no[index])
create(no,data,index*2);
else
create(no,data,(index*2)+1);
}
void inorder(int no[],int index)
{
if(no[index]!=-1)
{
inorder(no,index*2);
printf("%d",no[index]);
inorder(no,(index*2)+1);
}
}
void preorder(int no[],int index)
{
if(no[index]!=-1)
{
printf("%d",no[index]);
preorder(no,index*2);
preorder(no,(index*2)+1);
}
}
void postorder(int no[],int index)
{
if(no[index]!=-1)
{
postorder(no,index*2);
postorder(no,(index*2)+1);
printf("%d",no[index]);
}
}