-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree2.c
93 lines (87 loc) · 1.47 KB
/
Tree2.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
84
85
86
87
88
89
90
91
92
93
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data; //actual data
struct node *left; //left child
struct node *right; //right child
}tree;
void preorder(tree *root)
{
if(root!=NULL)
{
printf("\n %d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(tree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("\n %d ",root->data);
inorder(root->right);
}
}
void postorder(tree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("\n %d ",root->data);
}
}
tree * insert(tree *root, int num)
{
tree *temp;
if(root==NULL)
{
temp=(tree*)malloc(sizeof(tree));
temp->data=num;
temp->left=NULL;
temp->right=NULL;
root=temp;
return root;
}
else if(num<=root->data)
root->left=insert(root->left,num);
else if(num>root->data)
root->right=insert(root->right,num);
return root;
}
int main()
{
int ch,num;
tree *root;
root=NULL;
while(1)
{
printf("\n 1 for Insert ");
printf("\n 2 for Display ");
printf("\n 3 for Exit ");
printf("\n Enter the choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the number ");
scanf("%d",&num);
root=insert(root,num);
break;
case 2:
printf("\n Pre order traverse\n");
preorder(root);
printf("\n In order traverse\n");
inorder(root);
printf("\n Post order traverse\n");
postorder(root);
break;
case 3:
exit(1) ;
default:
printf("\n Wrong choice ");
}
}
}