forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarytrees_traversal.c
67 lines (59 loc) · 1.18 KB
/
binarytrees_traversal.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
/*pre-order, in-order, post-order traversals on binary trees*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *left;
struct node *right;
};
struct node* newnode(int value) {
struct node *new = (struct node*)malloc(sizeof(struct node));
new->data=value;
new->left=NULL;
new->right=NULL;
return(new);
}
void preorder(struct node *root)
{
if(root==NULL)
return;
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct node *root)
{
if(root==NULL)
return;
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
void postorder(struct node *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
void main()
{
struct node*root;
root=newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->right->left = newnode(6);
root->right->right = newnode(7);
printf("Pre order traversal: \n");
preorder(root);
printf("\n");
printf("\nIn order traversal: \n");
inorder(root);
printf("\n");
printf("\nPost order traversal: \n");
postorder(root);
printf("\n");
}