-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarytree_assignment1.c
83 lines (63 loc) · 1.54 KB
/
Binarytree_assignment1.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
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *Createnode(int data)
{
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = data;
n->left = NULL;
n->right = NULL;
return n;
}
struct node *insertleft(struct node * root , int data){
root->left = Createnode(data);
return root->left;
}
struct node *insertright(struct node * root , int data){
root->right = Createnode(data);
return root->right;
}
void inordertravarsal(struct node *root){
if(root != NULL){
inordertravarsal(root->left);
printf("%d " , root ->data);
inordertravarsal(root->right);
}
}
void pretravarsal(struct node *root){
if(root != NULL){
printf("%d " , root ->data);
pretravarsal(root->left);
pretravarsal(root->right);
}
}
void posttravarsal(struct node *root){
if(root != NULL){
posttravarsal(root->left);
posttravarsal(root->right);
printf("%d " , root ->data);
}
}
int main (){
struct node * root = Createnode(1);
insertleft(root , 12);
insertright(root , 9);
insertleft(root->left , 5);
insertright(root->left ,6);
insertleft(root->left->right , 2);
insertright(root->left->right ,3);
printf("Inorder travarsal : ");
inordertravarsal(root);
printf("\n");
printf("preorder travarsal : ");
pretravarsal(root);
printf("\n");
printf("postorder travarsal : ");
posttravarsal(root);
return 0;
}