-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTS program.c
122 lines (118 loc) · 2.42 KB
/
BTS program.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# https://www.facebook.com/permalink.php?story_fbid=180106856999528&id=100050006454252
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node* left;
struct node* right;
};
struct node* insertNode(struct node **r,int data)
{
struct node *temp,*ptr;
temp = (struct node*) malloc(sizeof(struct node));
temp->item=data;
temp->left=NULL;
temp->right=NULL;
if(*r==NULL)
*r=temp;
else
{
ptr=*r;
while(ptr)
{
if(ptr->item==data)
{
free(temp);
printf("\nDuplicate value not allowed");
}
else if(data<ptr->item)
{
if(ptr->left==NULL)
{
ptr->left=temp;
break;
}
else
ptr=ptr->left;
}
else
{
if(ptr->right==NULL)
{
ptr->right=temp;
break;
}
else
ptr=ptr->right;
}
}
}
}
void inorder(struct node *root)
{
if(root)
{
inorder(root->left);
printf("%d ",root->item);
inorder(root->right);
}
}
void preorder(struct node *root)
{
if(root)
{
printf("%d ",root->item);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct node *root)
{
if(root)
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->item);
}
}
int menu()
{
int choice;
printf("\n1. Add new element in BST");
printf("\n2. Delete an element from BST");
printf("\n3. Exit");
printf("\n\n Enter your choice:- ");
scanf("%d",&choice);
return choice;
}
int main()
{
struct node* root=NULL;
int data;
while(1)
{
system("cls");
printf("\nPreorder:- ");
preorder(root);
printf("\nInorder:- ");
inorder(root);
printf("\nPostorder:- ");
postorder(root);
switch(menu())
{
case 1:
printf("\nEnter a number to insert ");
scanf("%d",&data);
insertNode(&root,data);
break;
case 2:
break;
case 3:
exit(0);
default:
printf("\nInvalid choice");
}
getch();
}
}