-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTreeOperations.c
More file actions
335 lines (299 loc) · 9.67 KB
/
BinarySearchTreeOperations.c
File metadata and controls
335 lines (299 loc) · 9.67 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
/*The type of tree's element. It's an integer.*/
typedef int BinTreeElementType;
/*A pointer that points to tree's node*/
typedef struct BinTreeNode *BinTreePointer;
/*Every tree's node consists of BinTreeElementType data and pointers to left and Right child.*/
struct BinTreeNode {
BinTreeElementType Data;
BinTreePointer LChild, RChild;
};
typedef enum {
FALSE, TRUE
} boolean;
/*Every stack's element is a pointer.*/
typedef BinTreePointer StackElementType;
/*StackPointer point to a stack's node*/
typedef struct StackNode *StackPointer;
/*Every stack node consists of StackElementType data and a pointer that points to next node.*/
typedef struct StackNode
{
StackElementType Data;
StackPointer Next;
} StackNode;
void CreateStack(StackPointer *Stack);
boolean EmptyStack(StackPointer Stack);
void Push(StackPointer *Stack, StackElementType Item);
void Pop(StackPointer *Stack, StackElementType *Item);
void CreateBST(BinTreePointer *Root);
boolean EmptyBST(BinTreePointer Root);
void BSTInsert(BinTreePointer *Root, BinTreeElementType Item);
void BSTSearch(BinTreePointer Root, BinTreeElementType KeyValue, boolean *Found, BinTreePointer *LocPtr);
void BSTSearch2(BinTreePointer Root, BinTreeElementType Item, boolean *Found,BinTreePointer *LocPtr, BinTreePointer *Parent);
void BSTDelete(BinTreePointer *Root, BinTreeElementType KeyValue);
void InorderTraversal(BinTreePointer Root);
main()
{
int choice ;
char ch;
BinTreePointer ARoot, LocPtr;
BinTreeElementType AnItem;
boolean Found;
CreateBST(&ARoot);
do
{
menu(&choice);
switch(choice)
{
case 1: do
{
printf("Enter a number for insertion in the Tree: ");
scanf("%d", &AnItem);
BSTInsert(&ARoot, AnItem);
printf("\nContinue Y/N: ");
do
{
scanf("%c", &ch);
} while (toupper(ch)!= 'N' && toupper(ch)!= 'Y');
} while (toupper(ch)!='N');
break;
case 2: if (EmptyBST(ARoot))
printf("Empty tree\n");
else
InorderTraversal(ARoot);
break;
case 3: if (EmptyBST(ARoot))
printf("Empty tree\n");
else
{
printf("Enter a number for searching in the Tree: ");
scanf("%d", &AnItem);
BSTSearch(ARoot, AnItem, &Found, &LocPtr);
if (Found==FALSE)
printf("Item %d not in tree \n",AnItem);
else
printf("Item %d is in tree \n",AnItem);
}
break;
case 4: if (EmptyBST(ARoot))
printf("Empty tree\n");
else
{
printf("Enter a number for deleting in the Tree: ");
scanf("%d", &AnItem);
BSTDelete(&ARoot, AnItem);
}
break;
}
} while (choice!=5);
return 0;
}
void menu(int *choice)
{
printf(" MENOY \n");
printf("-------------------------------------------------\n");
printf("1. Insert an element to Binary Tree\n");
printf("2. In-oder traverse of Binary TreeA\n");
printf("3. Search for a element in Binary Tree\n");
printf("4. Delete an element of Binary Tree\n");
printf("5. Quit\n");
printf("\nChoice: ");
do
{
scanf("%d", choice);
} while (*choice<1 && *choice>4);
}
void CreateBST(BinTreePointer *Root)
/*Creates an empty binary search tree.
Returns a null pointer that it will be the tree's root.*/
{
*Root = NULL;
}
boolean EmptyBST(BinTreePointer Root)
/*Arguments: A BST pointer that points to tree's root.
Checks if BST is empty.
If it is empty returns true. Else returns false.*/
{ return (Root==NULL);
}
void BSTInsert(BinTreePointer *Root, BinTreeElementType Item)
/*Arguments: A BST pointer that point to tree's root and an element.
Inserts that element to tree.
Returns the modified BST.*/
{
BinTreePointer LocPtr, Parent;
boolean Found;
LocPtr = *Root;
Parent = NULL;
Found = FALSE;
while (!Found && LocPtr != NULL) {
Parent = LocPtr;
if (Item < LocPtr->Data)
LocPtr = LocPtr ->LChild;
else if (Item > LocPtr ->Data)
LocPtr = LocPtr ->RChild;
else
Found = TRUE;
}
if (Found)
printf("Element %c is already in BST\n", Item);
else {
LocPtr = (BinTreePointer)malloc(sizeof (struct BinTreeNode));
LocPtr ->Data = Item;
LocPtr ->LChild = NULL;
LocPtr ->RChild = NULL;
if (Parent == NULL)
*Root = LocPtr;
else if (Item < Parent ->Data)
Parent ->LChild = LocPtr;
else
Parent ->RChild = LocPtr;
}
}
void BSTSearch(BinTreePointer Root, BinTreeElementType KeyValue, boolean *Found,
BinTreePointer *LocPtr)
/*Arguments: A BST pointer that points to tree's root and a key value.
Searches a tree node with that key value.
If that node is found then LocPtr pointer points to that node and variable *found is true.
If not, variable *found is false. */
{
(*LocPtr) = Root;
(*Found) = FALSE;
while (!(*Found) && (*LocPtr) != NULL)
{
if (KeyValue < (*LocPtr)->Data)
(*LocPtr) = (*LocPtr)->LChild;
else
if (KeyValue > (*LocPtr)->Data)
(*LocPtr) = (*LocPtr)->RChild;
else (*Found) = TRUE;
}
}
void BSTSearch2(BinTreePointer Root, BinTreeElementType KeyValue, boolean *Found,
BinTreePointer *LocPtr, BinTreePointer *Parent)
/*Same as BSTSearch with the difference that *Parent variable points to the parent of the selected node.*/
{
*LocPtr = Root;
*Parent=NULL;
*Found = FALSE;
while (!(*Found) && *LocPtr != NULL)
{
if (KeyValue < (*LocPtr)->Data) {
*Parent=*LocPtr;
*LocPtr = (*LocPtr)->LChild;
}
else
if (KeyValue > (*LocPtr)->Data) {
*Parent=*LocPtr;
*LocPtr = (*LocPtr)->RChild;
}
else *Found = TRUE;
}
}
void BSTDelete(BinTreePointer *Root, BinTreeElementType KeyValue)
/*Arguments: A BinTreePointer that points to tree's root and a key-value for deletion.
Tries to find a node that contains that value.
If value is found, it's been deleted.
A modified tree is returned.*/
{
BinTreePointer
n, //points to the node that contains the key value. *)
Parent, // Father node of n's or nNext's
nNext, // In-order next node of n.
SubTree; // pointer of the subtree
boolean Found; // Is true if value is in BST *)
BSTSearch2(*Root, KeyValue, &Found , &n, &Parent);
if (!Found)
printf("Element %d exist in BST\n", KeyValue);
else {
if (n->LChild != NULL && n->RChild != NULL)
{ /*Node for deletion. Find in-order next and his father.*/
nNext = n->RChild;
Parent = n;
while (nNext->LChild !=NULL) //Left traversal *)
{
Parent = nNext;
nNext = nNext->LChild;
}
/*Copy context from nNext to n and change n so it points to the next node.*/
n->Data = nNext->Data;
n = nNext;
} //Case: that node has only one child
SubTree = n->LChild;
if (SubTree == NULL)
SubTree = n->RChild;
if (Parent == NULL) //Root it will be deleted
*Root = SubTree;
else if (Parent->LChild == n)
Parent->LChild = SubTree;
else
Parent->RChild = SubTree;
free(n);
}
}
void CreateStack(StackPointer *Stack)
/*Creates an empty linked stack.
Returns a pointer to that stack. Initially NULL*/
{
*Stack = NULL;
}
boolean EmptyStack(StackPointer Stack)
/*Arguments: A linked stack.
Checks if stack is empty.
If it is empty returns true, then false.*/
{
return (Stack==NULL);
}
void Push(StackPointer *Stack, StackElementType Item)
/*Arguments: a stack pointer that points to a stack and a stack's Item
Pushes the Item in the top.*/
{
StackPointer TempPtr;
TempPtr= (StackPointer)malloc(sizeof(struct StackNode));
TempPtr->Data = Item;
TempPtr->Next = *Stack;
*Stack = TempPtr;
}
void Pop(StackPointer *Stack, StackElementType *Item)
/*Pops the top Item. If stack is not empty.
If stack is empty a message is being displayed.*/
{
StackPointer TempPtr;
if (EmptyStack(*Stack)) {
printf("EMPTY Stack\n");
}
else
{
TempPtr = *Stack;
*Item=TempPtr->Data;
*Stack = TempPtr->Next;
free(TempPtr);
}
}
void InorderTraversal(BinTreePointer Root)
/*A BST pointer that points to tree's root.
Executes an in-order traversal and process each node just once.
Prints node's context.*/
{
StackPointer Stack;
BinTreePointer CurrPtr;
CurrPtr=Root;
CreateStack(&Stack);
do
{
while(CurrPtr!=NULL)
{
Push(&Stack,CurrPtr);
CurrPtr=CurrPtr->LChild;
}
if(!EmptyStack(Stack))
{
Pop(&Stack,&CurrPtr);
printf("%d ",CurrPtr->Data);
CurrPtr=CurrPtr->RChild;
}
}
while((!EmptyStack(Stack)) || CurrPtr!=NULL);
}