-
Notifications
You must be signed in to change notification settings - Fork 1
/
Binarysearchtraversal.c
73 lines (61 loc) · 1.46 KB
/
Binarysearchtraversal.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
#include<stdio.h>
#include<process.h>
#include<malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *newnode(int data)
{
struct node *node =(struct node *)malloc(sizeof(struct node));
node->data=data;
node->left=NULL;
node->right=NULL;
return(node);
}
void preorder(struct node *node)
{
if(node==NULL)
return;
printf("\t %d",node->data);
preorder(node->left);
preorder(node->right);
}
void postorder(struct node *node)
{
if(node==NULL)
return;
postorder(node->left);
postorder(node->right);
printf("\t%d",node->data);
}
void inorder(struct node *node)
{
if(node == NULL)
return;
inorder(node->left);
printf("\t%d",node->data);
inorder(node->right);
}
int main()
{
struct node *root = newnode(1);
root->left = newnode(2);
root->right = newnode(3);
root->right->left = newnode(8);
root->right->right = newnode(9);
root->left->left = newnode(4);
root->left->right = newnode(5);
root->left->left->left = newnode(6);
root->left->left->right = newnode(7);
printf("\nPreorder traversal of binary tree is \n");
preorder(root);
printf("\nInorder traversal of binary tree is \n");
inorder(root);
printf("\nPostorder traversal of binary tree is \n");
postorder(root);
getchar();
return 0;
}