-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary tree.c
53 lines (53 loc) · 1.17 KB
/
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data ;
struct node *left ;
struct node *right ;
};
struct node* maketree(){
struct node *p ;
int info ;
printf("Enter the info or -1 for null\n");
scanf("%d", &info) ;
if(info == -1) return NULL ;
p = (struct node *)malloc(sizeof(struct node)) ;
p->data = info ;
printf("Enter the left child of %d\n",info );
p->left = maketree() ;
printf("Enter the right child of %d\n",info );
p->right = maketree() ;
return p ;
}
void preorder(struct node *t){
if(t!=NULL){
printf("%d->", t->data) ;
preorder(t->left) ;
preorder(t->right) ;
}
}
void postorder(struct node *t){
if(t!=NULL){
postorder(t->left) ;
postorder(t->right) ;
printf("%d->", t->data) ;
}
}
void inorder(struct node *t){
if(t!=NULL){
inorder(t->left) ;
printf("%d->", t->data) ;
inorder(t->right) ;
}
}
int main(){
struct node *root ;
root = maketree();
printf("Preorder: ");
preorder(root) ;
printf("\n Postorder :");
postorder(root) ;
printf("\n Inorder");
inorder(root) ;
return 0 ;
}