-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab12_1.c
147 lines (137 loc) · 3.43 KB
/
lab12_1.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Construct a BST, implement all the cases of deletion, then display the tree using preorder traversal.
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *lchild;
int info;
struct node *rchild;
};
struct node *insert(struct node *ptr, int ikey)
{
if (ptr == NULL)
{
ptr = (struct node *)malloc(sizeof(struct node));
ptr->info = ikey;
ptr->lchild = NULL;
ptr->rchild = NULL;
}
else if (ikey < ptr->info)
{
ptr->lchild = insert(ptr->lchild, ikey);
}
else if (ikey > ptr->info)
{
ptr->rchild = insert(ptr->rchild, ikey);
}
else
{
printf("Duplicate Key \n");
}
return ptr;
}
struct node *case_a(struct node *root, struct node *par, struct node *ptr)
{
if (par == NULL)
root = NULL;
else if (ptr == par->lchild)
par->lchild = NULL;
else
par->rchild = NULL;
free(ptr);
return (root);
}
struct node *case_b(struct node *root, struct node *par, struct node *ptr)
{
struct node *child;
if (ptr->lchild != NULL)
child = ptr->lchild;
else
child = ptr->rchild;
if (par == NULL)
root = child;
else if (ptr == par->lchild)
par->lchild = child;
else
par->rchild = child;
free(ptr);
return (root);
}
struct node *case_c(struct node *root, struct node *par, struct node *ptr)
{
struct node *succ, *parsucc;
parsucc = ptr;
succ = ptr->rchild;
while (succ->lchild != NULL)
{
parsucc = succ;
succ = succ->lchild;
}
ptr->info = succ->info;
if (succ->lchild == NULL && succ->rchild == NULL)
root = case_a(root, parsucc, succ);
else
root = case_b(root, parsucc, succ);
return (root);
}
struct node *del(struct node *root, int dkey)
{
struct node *par, *ptr;
ptr = root;
par = NULL;
while (ptr != NULL)
{
if (dkey == ptr->info)
break;
par = ptr;
if (ptr == NULL)
printf("dkey is not present in the tree: \n");
else if (ptr->lchild != NULL && ptr->rchild != NULL)
root = case_c(root, par, ptr);
else if (ptr->lchild != NULL)
root = case_b(root, par, ptr);
else if (ptr->rchild != NULL)
root = case_b(root, par, ptr);
else
root = case_a(root, par, ptr);
return (root);
}
}
void preorder(struct node *ptr)
{
if (ptr == NULL)
return;
printf("%d\n", ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
void main()
{
struct node *root = NULL, *ptr;
int choice, k;
while (choice != 4)
{
printf("\nEnter 1 to insert \nEnter 2 for preorder \nEnter 3 for deletion \nEnter 4 to terminate \nEnter choice \n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the key to be inserted \n");
scanf("%d", &k);
root = insert(root, k);
break;
case 2:
preorder(root);
break;
case 3:
printf("Enter the key to be deleted \n");
scanf("%d", &k);
root = del(root, k);
break;
case 4:
exit(1);
default:
printf("Wrong choice! \n");
}
}
}