-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASSIGN6.BAK
More file actions
executable file
·471 lines (388 loc) · 12.8 KB
/
ASSIGN6.BAK
File metadata and controls
executable file
·471 lines (388 loc) · 12.8 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
{ Melissa Mangos }
{ May 31, 2013 }
{ This program simulates bank data in a binary tree }
program BankData;
uses wincrt;
{ Assign record }
type
TClientPtr = ^TClient;
TClient = record
FirstName: string[15];
LastName : string[30];
Phone : string[12];
Savings, Chequings, GICs, Bonds, Mutual : real;
pLeft, pRight, pParent : TClientPtr;
end; { TStudent }
{ Variable Declarations }
var
pRoot : TClientPtr;
{ METHODS ------------------------------------------------- }
function LeftOrRight (pChild, pParent : TClientPtr) : string;
{ Determines if child is to the left or right of given parent }
var
direction : string;
begin
if (pChild^.LastName < pParent^.LastName) or
((pChild^.LastName = pParent^.LastName) and (pChild^.FirstName < pParent^.FirstName)) then
{ Child is to the left }
begin
direction := 'Left';
end
else if (pChild^.LastName > pParent^.LastName) or
((pChild^.LastName = pParent^.LastName) and (pChild^.FirstName > pParent^.FirstName)) then
{ Child is to the right }
begin
direction := 'Right';
end; { if }
{ Return direction }
LeftOrRight := direction;
end; { LeftOrRight }
function SearchClient (pCurrent, pSearch : TClientPtr) : TClientPtr;
{ Searches for a client }
var
direction : string;
begin
{ Checks which side the searched person should go }
if (pSearch^.LastName <> pCurrent^.LastName) or (pSearch^.FirstName <> pCurrent^.FirstName) then
begin
{ Find out if child is on parents left or right }
direction := LeftOrRight (pSearch, pCurrent);
if (direction = 'Left') then
begin
{ Checks left for nil and calls function on left if not nil }
if (pCurrent^.pLeft <> nil) then
begin
pCurrent := SearchClient (pCurrent^.pLeft, pSearch);
end; { if }
end
else if (direction = 'Right') then
begin
{ Checks right for nil and calls function on right if not nil }
if (pCurrent^.pRight <> nil) then
begin
pCurrent := SearchClient (pCurrent^.pRight, pSearch);
end; { if }
end; { if }
end; { if }
{ Returns client if found, parent if not found }
SearchClient := pCurrent;
end; { SearchClient }
procedure InsertClient (var pNew : TClientPtr);
{ Inserts a new client into the binary tree }
var
pCurrent : TClientPtr;
direction : string;
begin
{ Makes the root equal to the first client }
if (pRoot = nil) then
begin
pRoot := pNew;
pRoot^.pParent := nil;
pRoot^.pRight := nil;
pRoot^.pLeft := nil;
end
else
begin
{ Searches for a spot to put the new client }
pCurrent := SearchClient (pRoot, pNew);
{ If the client does not exist }
if (pCurrent^.FirstName <> pNew^.FirstName) or (pCurrent^.LastName <> pNew^.LastName) then
begin
{ Decides if child is on parents left or right }
direction := LeftOrRight (pNew, pCurrent);
{ Places the client on the left }
if (direction = 'Left') then
begin
pCurrent^.pLeft := pNew;
end
{ Places the client on the right }
else if (direction = 'Right') then
begin
pCurrent^.pRight := pNew;
end;
{ Initializes the new client }
pNew^.pParent := pCurrent;
pNew^.pLeft := nil;
pNew^.pRight := nil;
end; { if }
end; { if }
end; { InsertClient }
procedure ReadFile;
{ Reads clients from text file into nodes }
var
pNew : TClientPtr;
fo : text;
begin
{ Open text file }
assign (fo, 'clients.txt');
reset (fo);
{ Initialize an empty list }
pRoot := nil;
while (not (eof (fo))) do
begin
{ Create a new node by reading text file }
new (pNew);
readln (fo, pNew^.FirstName);
readln (fo, pNew^.LastName);
readln (fo, pNew^.Phone);
readln (fo, pNew^.Savings);
readln (fo, pNew^.Chequings);
readln (fo, pNew^.GICs);
readln (fo, pNew^.Bonds);
readln (fo, pNew^.Mutual);
{ Inserts the client from text file }
InsertClient (pNew);
end; { while }
end; { ReadFile }
procedure DisplayClient (pClient : TClientPtr);
{ Displays a given client's information }
begin
writeln ('');
writeln (pClient^.FirstName);
writeln (pClient^.LastName);
writeln (pClient^.Phone);
writeln ('Savings Account: ', pClient^.Savings : 8 : 2);
writeln ('Chequing Account: ', pClient^.Chequings : 8 : 2);
writeln ('G.I.C.s: ', pClient^.GICs : 8 : 2);
writeln ('Bonds: ', pClient^.Bonds : 8 : 2);
writeln ('Mutual Funds: ', pClient^.Mutual : 8 : 2);
end; { DisplayClient }
procedure DeleteClient (var pClient : TClientPtr);
{ Deletes a given client }
var
pReplace : TClientPtr;
direction : string;
begin
{ Checks if client is on left or right of parent }
direction := LeftOrRight (pClient, pClient^.pParent);
if (pClient^.pLeft = nil) and (pClient^.pRight = nil) then
{ Leaf node }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
pClient^.pParent^.pLeft := nil;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
pClient^.pParent^.pRight := nil;
end; { if }
end
else if (pClient^.pLeft <> nil) and (pClient^.pRight = nil) then
{ One child on left }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
{ Sets parents left to child }
pClient^.pParent^.pLeft := pClient^.pLeft;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
{ Sets parents right to child }
pClient^.pParent^.pRight := pClient^.pLeft;
end; { if }
{ Sets child's parent to client' s parent }
pClient^.pLeft^.pParent := pClient^.pParent;
end
else if (pClient^.pRight <> nil) and (pClient^.pLeft = nil) then
{ One child on right }
begin
if (direction = 'Left') then
{ Left side of parent }
begin
{ Sets parents left to child }
pClient^.pParent^.pLeft := pClient^.pRight;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
{ Sets parents right to child }
pClient^.pParent^.pRight := pClient^.pRight;
end; { if }
{ Sets child's parent to client's parent }
pClient^.pRight^.pParent := pClient^.pParent;
end
else if (pClient^.pLeft <> nil) and (pClient^.pRight <> nil) then
{ Two children }
begin
{ Replaces node with next alphabetically }
pReplace := pClient^.pRight;
while (pReplace^.pLeft <> nil) do
begin
pReplace := pReplace^.pLeft;
end; { while }
{ Remove the replacement from its original spot }
if (pReplace^.pLeft = nil) and (pReplace^.pRight = nil) then
{ Leaf node }
begin
pReplace^.pParent^.pLeft := nil;
end
else if (pReplace^.pLeft = nil) then
{ Child on the right }
begin
pReplace^.pParent^.pLeft := pReplace^.pRight;
pReplace^.pRight^.pParent := pReplace^.pParent;
end
else if (pReplace^.pRight = nil) then
{ Child on the left }
begin
pReplace^.pParent^.pLeft := pReplace^.pLeft;
pReplace^.pLeft^.pParent := pReplace^.pParent;
end; { if }
{ Set the replacement to equal the deleted node }
if (direction = 'Left') then
{ Left side of parent }
begin
pClient^.pParent^.pLeft := pReplace;
end
else if (direction = 'Right') then
{ Right side of parent }
begin
pClient^.pParent^.pRight := pReplace;
end; { if }
{ Puts replacement into spot of deleted }
pReplace^.pParent := pClient^.pParent;
pReplace^.pLeft := pClient^.pLeft;
pReplace^.pRight := pClient^.pRight;
end; { if }
{ Dispose node }
dispose (pClient);
end; { DeleteClient }
procedure AlphaClient (pClient : TClientPtr);
{ Displays the clients in alphabetical order }
begin
if (pClient^.pLeft <> nil) then
{ Displays on the left using recursion }
begin
AlphaClient (pClient^.pLeft);
end; { if }
{ Display Client }
write (pClient^.FirstName, ' ', pClient^.LastName, ' ');
if (pClient^.pRight <> nil) then
{ Displays on right using recursion }
begin
AlphaClient (pClient^.pRight);
end; { if }
end;
{ MAIN PROGRAM -------------------------------------------- }
{ Variable Decalrations }
var
pSearch, pClient, pNew : TClientPtr;
answer, alpha : string;
begin
{ Reads Text file and calls input and search functions }
ReadFile;
{ Creates a new node to be searched }
new (pSearch);
while (alpha <> 'Yes') do
begin
{ Inputs a person to search }
write ('Enter the client''s first name: ');
readln (pSearch^.FirstName);
write('Enter the client''s last name: ');
readln (pSearch^.LastName);
{ Searches for the person }
pClient := SearchClient (pRoot, pSearch);
if (pClient^.FirstName = pSearch^.FirstName) and (pClient^.LastName = pSearch^.LastName) then
begin
{ Displays the person's information }
DisplayClient (pClient);
{ Asks to delete client }
writeln ('');
write ('Would you like to delete the client (Yes or No)? ');
readln (answer);
if (answer = 'Yes') then
begin
{ Deletes the client }
DeleteClient (pClient);
end;
end
else
begin
{ If client is not found, asks to insert a new one }
writeln ('');
write ('Would you like to insert a client (Yes or No)? ');
readln (answer);
if (answer = 'Yes') then
begin
{ Gets new clients information }
new (pNew);
pNew^.FirstName := pSearch^.FirstName;
pNew^.LastName := pSearch^.LastName;
write ('Phone number? ');
readln (pNew^.Phone);
write ('Savings account? ');
readln (pNew^.Savings);
write ('Chequing account? ');
readln (pNew^.Chequings);
write ('G.I.C.s? ');
readln (pNew^.GICs);
write ('Bonds? ');
readln (pNew^.Bonds);
write ('Mutual Funds? ');
readln (pNew^.Mutual);
{ Inserts client into binary tree }
InsertClient (pNew);
{ Displays new client's information }
DisplayClient (pNew);
end;
end; { if }
{ Asks for alphabetical order (exits the loop) }
writeln ('');
write ('Would you like to see the clients alphabetically (Yes to exit)? ');
readln (alpha);
end;
{ Displays clients in alphbetical order }
AlphaClient (pRoot);
end. { program }
(*
{ Inserts a new client into the binary tree }
{ procedure Insert (Root, new)
IF pRoot is a nil THEN
Intialize the tree with the new node
pLeft and pRight to nil
pParent to nil
pRoot = pNew
ELSE
Find where to insert the new node (say at pCurrent (a nil), keep track of pCurrent's parent)
IF new node's name < parent's name THEN
insert at pParent's Left
ELSE (new node's name > parent's name)
pParent's Right
END IF
close off children
END IF }
begin
initialize an empty tree (pRoot)
WHILE now eof DO
Create a new node
Read the next record into the new node
Insert new node into the tree
END WHILE
end.
DELETING A NODE
There are three situations:
First find node
1. Deleting a leaf node
- set the parent's left or right to a nil
- dispose of leaf node
2. Node has one child
- connect node's parent to node's child
( Four scenarios : left left, left right, right left, right right )
- dispose of node
3. Deleting a node with two children
- Replace the node with another client (with the next person alphabetically)
- go left once then right as far as you can
OR
- go right once then left as far as you can
- use replacement node to take place of the node to be deleted
BUT ... is the replacement node a leaf or does it have one child?
IF replacement node is a leaf THEN
Set the parents left or right to a nil
ELSE
Connect the parent to the child
END IF
*)