forked from harikrishnan669/DS_Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearchtree.c
86 lines (86 loc) · 1.51 KB
/
binarysearchtree.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node1
{
int data;
struct node *left;
struct node *right;
}node;
node *create()
{
node *ptr;
int num;
printf("Enter the value(Input -1 if there is no data):");
scanf("%d",&num);
if(num==-1)
{
return NULL;
}
ptr=(node*)malloc(sizeof(node));
ptr->data=num;
if(num<ptr->data)
{
ptr->left=create();
}
else
{
ptr->right=create();
}
return ptr;
}
void inorder(node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->left);
printf("\t%d",ptr->data);
inorder(ptr->right);
}
}
void preorder(node *ptr)
{
if(ptr!=NULL)
{
printf("\t%d",ptr->data);
preorder(ptr->left);
preorder(ptr->right);
}
}
void postorder(node *ptr)
{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
printf("\t%d",ptr->data);
}
}
int main()
{
int choice;
node *root;
root=create();
printf("1-INORDER\n");
printf("2-PREORDER\n");
printf("3-POSTORDER\n");
printf("4-EXIT\n");
while(1)
{
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("The inorder is");
inorder(root);
break;
case 2:printf("The preorder is");
preorder(root);
break;
case 3:printf("The post order is");
postorder(root);
break;
case 4:exit(1);
break;
}
}
}