forked from Kraken-ops/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytree.c
64 lines (49 loc) · 981 Bytes
/
binarytree.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
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *lnode;
struct node *rnode;
};
struct node * createnode(int data){
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
ptr->data=data;
ptr->lnode=NULL;
ptr->rnode=NULL;
return(ptr);
}
void inorder(struct node *p){
if(p){
inorder(p->lnode);
printf("%d",p->data);
inorder(p->rnode);
}
}
void postorder(struct node *p){
if(p){
postorder(p->lnode);
postorder(p->rnode);
printf("%d",p->data);
}
}
void preorder(struct node *p){
if(p){
printf("%d",p->data);
preorder(p->lnode);
preorder(p->rnode);
}
}
int main(){
struct node *p=createnode(4);
struct node *p1=createnode(1);
struct node *p2=createnode(2);
p->lnode=p1;
p->rnode=p2;
inorder(p);
printf("\n");
postorder(p);
printf("\n");
preorder(p);
return 0;
}