forked from sourav-122/hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmjaybinarytree.c
More file actions
109 lines (88 loc) · 1.83 KB
/
mjaybinarytree.c
File metadata and controls
109 lines (88 loc) · 1.83 KB
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct bst
{
int data;
struct bst *left;
struct bst *right;
};
struct bst * insert(struct bst *,int);
void inorder(struct bst *);
void preorder(struct bst *);
void postorder(struct bst *);
int main ()
{
struct bst *r=NULL;
r=insert(r,30);
r=insert(r,15);
r=insert(r,10);
r=insert(r,20);
r=insert(r,40);
r=insert(r,5);
r=insert(r,45);
r=insert(r,35);
printf("\n display element in inorder:-");
inorder(r);
printf("\n display element in preorder:-");
preorder(r);
printf("\n display element in postorder:-");
postorder(r);
return 1;
}
struct bst * insert(struct bst *q,int val)
{
struct bst *tmp;
tmp=(struct bst *)malloc(sizeof(struct bst));
if(q==NULL)
{
tmp->data=val;
tmp->left=tmp->right=NULL;
return tmp;
}
else
{
if(val<(tmp->data))
{
q->left=insert(q->left,val);
}
else
{
q->right=insert(q->right,val);
}
}
return q;
}
void inorder(struct bst *q)
{
if(q==NULL)
{
return;
}
inorder(q->left);
printf(" %d ",q->data);
inorder(q->right);
}
void preorder(struct bst *q)
{
if(q!=NULL)
{
printf(" %d ",q->data);
preorder(q->left);
preorder(q->right);
}
}
void postorder(struct bst *q)
{
if(q!=NULL)
{
postorder(q->left);
postorder(q->right);
printf(" %d ",q->data);
}
}
Output
display element in inorder:- 35 45 5 40 20 10 15 30
display element in preorder:- 30 15 10 20 40 5 45 35
display element in postorder:- 35 45 5 40 20 10 15 30
--------------------------------