-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbintree.c
1212 lines (1038 loc) · 31.9 KB
/
bintree.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*
* Copyright (c) 1997, 1998 Timpanogas Research Group, Inc. All Rights
* Reserved.
*
* AUTHOR : Jeff V. Merkey
* FILE : BINTREE.C
* DESCRIP : Binary Tree Library for MANOS v1.0
* DATE : June 26, 1998
*
***************************************************************************/
#include "version.h"
#include "stdarg.h"
#include "stdio.h"
#include "stdlib.h"
#include "ctype.h"
#include "string.h"
#include "keyboard.h"
#include "screen.h"
#include "types.h"
#include "emit.h"
#include "dos.h"
#include "tss.h"
#include "os.h"
#include "mps.h"
#include "hal.h"
#include "timer.h"
#include "kernel.h"
#include "peexe.h"
#include "malloc.h"
#include "free.h"
#define TEST
#include "bintree.h"
/* A safe malloc() */
static void *tmalloc(int size)
{
void *p;
if ((p = malloc(size)) == NULL)
{
printf("Out of memory\n");
return 0;
}
return p;
}
/*
* Create and initialize a node for the user. 'size' both can
* and should be greater than sizeof(Bnode) to allow for a
* data area for the user.
*/
Bnode *InitBintreeNode(int size)
{
Bnode *n;
n = tmalloc(size);
n -> link[LEFT] = n -> link[RIGHT] = NULL;
RBONLY(n -> red = 0;)
return n;
}
/* Create an empty tree */
Bintree *NewBintree (Bnode *dummy, CompFunc cf, int dup_ok, int node_size)
{
Bintree *t;
t = tmalloc(sizeof(Bintree));
t -> DummyHead = dummy;
t -> Compare = cf;
t -> DuplicatesOk = dup_ok;
t -> NodeSize = node_size;
return t;
}
#if defined(SPLAY)
/*
* During a top-down splay, we build up the future left and right
* sub-trees in trees whose roots are stored in the array LR[].
* LRwalk[] retains a current pointer into each of these trees.
* We are always interested in finding the bottom left node of
* the right tree or the bottom right node of the left tree.
* Thus, PUSH(LEFT) starts at LRwalk[LEFT], steps down and to
* the right until it hits bottom, and then stores the new
* location in LRwalk[LEFT].
*/
#define PUSH(x) { \
Bnode *w; \
for (w = LRwalk[x]; \
w -> link[!(x)]; \
w = w -> link[!(x)]); \
LRwalk[x] = w; \
}
int splay(Bintree *t, Bnode *n)
{
Bnode *s, *ch, *gch;
Bnode LR[2], *LRwalk[2];
int s_comp, ch_comp, dir, dir2;
s = t -> DummyHead -> link[RIGHT];
if (s == NULL) /* empty tree */
return 1; /* no match */
/*
* Create two empty trees: we place portions of the initial
* tree onto these two trees as we "splay down" the tree.
*/
LR[LEFT].link[RIGHT] = NULL;
LR[RIGHT].link[LEFT] = NULL;
LRwalk[LEFT] = &LR[LEFT];
LRwalk[RIGHT] = &LR[RIGHT];
/* not really needed */
LR[LEFT].link[LEFT] = NULL;
LR[RIGHT].link[RIGHT] = NULL;
for (;;) {
/* We are at s. Which way now? First, find s's child */
s_comp = n ? (t -> Compare)(n, s) : 1;
dir = s_comp < 0;
ch = s -> link[dir];
if (s_comp == 0 || ch == NULL)
break;
/* Now, find s's grandchild */
ch_comp = n ? (t -> Compare)(n, ch) : 1;
dir2 = ch_comp < 0;
gch = ch -> link[dir2];
/*
* If we've found a match for n (ch_comp==0) or if we've
* no further to go (gch==NULL), then we're done. ch will
* be the root of the new tree after reconstruction is
* complete. This case is the only exit from this loop.
*/
if (ch_comp == 0 || gch == NULL) {
s -> link[dir] = NULL; /* break link betw s and ch */
LRwalk[!dir] -> link[dir] = s; /* hang s on LR */
PUSH(!dir); /* and push LRwalk to bottom */
s = ch; /* advance s to ch */
s_comp = ch_comp;
break; /* proceed to tree reconstruction */
}
else { /* split up the tree as described in the text */
if (dir == dir2) { /* zig-zig */
s -> link[dir] = ch -> link[!dir];
ch -> link[!dir] = s;
ch -> link[dir] = NULL;
LRwalk[!dir] -> link[dir] = ch;
PUSH(!dir);
}
else { /* zig-zag */
s -> link[dir] = NULL;
LRwalk[!dir] -> link[dir] = s;
PUSH(!dir);
ch -> link[dir2] = NULL;
LRwalk[!dir2] -> link[dir2] = ch;
PUSH(!dir2);
}
s = gch;
}
}
/* put it all together */
LRwalk[LEFT] -> link[RIGHT] = s -> link[LEFT];
LRwalk[RIGHT] -> link[LEFT] = s -> link[RIGHT];
s -> link[LEFT] = LR[LEFT].link[RIGHT];
s -> link[RIGHT] = LR[RIGHT].link[LEFT];
t -> DummyHead -> link[RIGHT] = s;
return s_comp;
}
#endif
/* Find node n in tree t */
Bnode *FindBintree(Bintree *t, Bnode *n)
{
#if defined(SPLAY)
if (splay(t, n))
return NULL; /* exact match not found */
else
return t -> DummyHead -> link[RIGHT];
#else /* plain or red-black */
Bnode *s;
int dir;
s = t -> DummyHead -> link[RIGHT];
while (s != NULL) {
dir = (t -> Compare) (n, s);
/*
* If a match, we're done.
* For Red-Black, must also be a leaf.
*/
if (dir == 0 RBONLY(&& s -> link[RIGHT] == NULL))
return s;
dir = dir < 0;
s = s -> link[dir];
}
return NULL; /* no match */
#endif
}
#if defined(REDBLACK)
/*
* Rotate child and grandchild of r along the path
* specified by searching for n. For example, if n was
* equal to 3, gc2, or 4, the following rotation occurs:
*
* r r
* | |
* c gc2
* / \ ==> / \
* gc1 gc2 c 4
* / \ / \ / \
* 1 2 3 4 gc1 3
* / \
* 1 2
*
* As r may connect to c via either its left or right
* link, there are actually four symmetric variants.
*
* A pointer to the top of the new rotated nodes (in the
* case above, to gc2) is returned.
*
* This routine is complicated by the fact that the routine
* uses the value of the node n to decide which direction
* to rotate. This may or may not be the direction the caller
* has is mind. Rather than require the caller to specify
* the direction of the rotation, it seemed easier to allow
* the caller to specify whether to go in the direction of n
* or away from it. This is done by the last argument to the
* function, flip_mode. The caller can indicate that either
* or both of the directions to child and grandchild should
* be reversed during the rotation.
*/
#define NO_FLIP 0
#define FLIP_GCH 1
#define FLIP_CH 2
Bnode *rotate(Bintree *t, Bnode *n, Bnode *r, int flip_mode)
{
Bnode *ch, *gch;
int ch_dir, gch_dir;
/* Identify child and grandchild */
ch_dir = (t -> Compare) (n, r) < 0;
if (flip_mode & FLIP_CH)
ch_dir = !ch_dir;
if (r == t -> DummyHead) /* special condition */
ch_dir = RIGHT;
ch = r -> link[ch_dir];
gch_dir = (t -> Compare) (n, ch) < 0;
if (flip_mode) {
if (flip_mode == FLIP_GCH)
gch_dir = !gch_dir;
else
gch_dir = flip_mode & 1;
}
gch = ch -> link[gch_dir];
/* rotate: now move pointers */
ch -> link[gch_dir] = gch -> link[!gch_dir];
gch -> link[!gch_dir] = ch;
r -> link[ch_dir] = gch;
return gch;
}
/*
* Take care of colors and balance. It will color the current
* location red, the current location's children black, and
* then look to see if two consecutive red nodes have been
* created. If so, a single or double rotation will be done
* to fix the tree.
*/
void split(Bintree *t, /* tree */
Bnode *n, /* node being inserted */
Bnode **c, /* current location */
Bnode **p, /* its parent */
Bnode *g, /* its grandparent */
Bnode *gg) /* its great-grandparent */
{
if (t -> DummyHead -> red)
{
printf("dummyhead was red!!\n");
t -> DummyHead -> red = 0;
}
(*c) -> red = 1;
if ((*c) -> link[LEFT])
(*c) -> link[LEFT] -> red = 0;
if ((*c) -> link[RIGHT])
(*c) -> link[RIGHT] -> red = 0;
/*
* Check to make sure we haven't created two red
* links in a row. If we have, we must rotate.
*/
if ((*p) -> red) {
g -> red = 1;
/*
* If the red links don't point in the same direction,
* then will need a double rotation. The lower half
* is around the grandparent and then the upper half
* is around the great-grandparent.
*/
if (((t -> Compare) (n, g) < 0) !=
((t -> Compare) (n, *p) < 0))
*p = rotate(t, n, g, NO_FLIP);
/* Same for both single and double rotations. */
*c = rotate(t, n, gg, NO_FLIP);
(*c) -> red = 0;
}
t -> DummyHead -> link[RIGHT] -> red = 0;
}
#endif
/*
* Delete node n from tree t. Returns a pointer to the
* deleted node -- it should then be freed or otherwise
* destroyed. The versions for the binary tree and the
* red-black tree are very different, due to the balancing
* problems that the red-black version must handle.
*/
#if defined(REDBLACK)
Bnode *DelBintree (Bintree *t, Bnode *n)
{
/*
* The goal is to arrive at a leaf with a red parent.
* Thus, we force this by dragging a red node with us
* down the tree, re-arranging the tree to keep its
* balance as we go. All the rearrangements keep the tree
* balanced, so if we cancel the deletion or don't find
* the specified node to delete, we can just quit.
*/
Bnode *s, *p, *g;
int dir, next_dir;
g = NULL;
p = t -> DummyHead;
s = p -> link[RIGHT];
dir = RIGHT;
/*
* First, check on the root. It must exist, have children,
* and either it or one of its children must be red. We can
* just paint the root red, if necessary, as this will
* affect the black height of the entire tree equally.
*/
if (s == NULL)
return NULL;
/* Check to make sure the root isn't an only child. */
if (s -> link[LEFT] == NULL) {
if ((t -> Compare)(n, s) == 0) {
/* deleting the root */
p -> link[RIGHT] = NULL;
return s;
}
else
return NULL;
}
/* Now, either the root or one of its kids must be red */
if (!s -> link[LEFT] -> red &&
!s -> link[RIGHT] -> red)
s -> red = 1; /* Just color the root red */
/*
* Now, march down the tree, always working to make sure
* the current node is red. That way, when we do arrive
* at a leaf, its parent will be red, making the leaf
* very easy to delete (just drop the leaf, and replace
* its (red) parent with its (black) sib.)
*/
for (;;) {
/*
* If we're at a leaf, we're done.
*/
if (s -> link[LEFT] == NULL)
break;
/*
* Where are we going next?
*/
next_dir = (t -> Compare) (n, s) < 0;
/*
* If the current node or the next node
* is red, we can advance.
*/
if (s -> red || s -> link[next_dir] -> red)
;
/*
* (If the current node is black)
* (and the next node is black)
* but the next node's sib is red ...
*
* Then rotate from parent towards the red child. This
* will lower the current node, and give us a new
* grandparent (the old parent) and a new
* parent (the sib that was red). We the paint the
* current node red and the new parent is painted black.
*/
else if (s -> link[!next_dir] -> red) {
g = p;
p = rotate(t, s -> link[next_dir], p, FLIP_GCH);
s -> red = 1;
p -> red = 0;
}
/*
* (If the current node is black)
* (and its left child is black)
* (and its right child is black) ...
*
* then (a) the current node's parent must be red (we
* never advance unless we are leaving a red node),
* (b) its sib must be black (because the parent is red),
* and (c) we need to color the current node red. To
* make this possible, we color the current node red,
* the parent black and then check for tree imbalances.
* Two cases exist...
*/
else {
Bnode *sib;
if (!p -> red)
printf("Parent not red in case 2!\n");
sib = p -> link[!dir];
if (sib -> red)
printf("Sib not black in case 2!\n");
if (sib -> link[LEFT] == NULL) {
printf("Sib has no kids in case 2!\n");
return NULL;
}
s -> red = 1;
p -> red = 0;
/*
* First case: black sib has two black kids. Just
* color the sib red. In effect, we are reversing
* a simple color flip.
*/
if (!sib -> link[LEFT] -> red &&
!sib -> link[RIGHT] -> red)
sib -> red = 1;
/*
* Second case: black sib has at least one red kid.
* (It makes no difference if both kids are red.)
* We need to do either a single or double rotation
* in order to re-balance the tree.
*/
else {
int redkid_dir;
if (sib -> link[LEFT] -> red)
redkid_dir = LEFT;
else
redkid_dir = RIGHT;
if (!dir == redkid_dir)
{
sib -> red = 1;
sib -> link[redkid_dir] -> red = 0;
g = rotate(t, n, g, FLIP_GCH);
}
else
{
rotate(t, n, p, FLIP_CH + redkid_dir);
g = rotate(t, n, g, FLIP_GCH);
}
}
}
/* advance pointers */
dir = next_dir;
g = p;
p = s;
s = s -> link[dir];
}
/* Make the root black */
t -> DummyHead -> link[RIGHT] -> red = 0;
/* Delete it, if a match. Parent is red. */
if ((t -> Compare)(s, n) == 0)
{
if (!p -> red && p != t -> DummyHead)
printf("Parent not red at delete!\n");
g -> link[(t -> Compare)(s, g) < 0] =
p -> link[(t -> Compare)(s, p) >= 0];
free (p); /* release internal node that we created */
return s;
}
else return NULL;
}
#elif defined(SPLAY) /* Splay tree version */
Bnode *DelBintree (Bintree *t, Bnode *n)
{
Bnode *save, *t2;
if (splay(t, n))
save = NULL; /* match not found */
else
{
save = t -> DummyHead -> link[RIGHT];
t2 = save -> link[RIGHT];
if (t -> DummyHead -> link[RIGHT] == save -> link[LEFT])
{ /* '=' and not '==' is correct on previous line */
splay(t, NULL);
t -> DummyHead -> link[RIGHT] -> link[RIGHT] = t2;
}
else
t -> DummyHead -> link[RIGHT] = t2;
}
return save;
}
#else /* Binary tree version */
Bnode *DelBintree (Bintree *t, Bnode *n)
{
Bnode *p, *s, *save;
int dir, dir_old;
p = t -> DummyHead;
s = p -> link[RIGHT];
dir_old = dir = RIGHT;
/* Look for a match */
while (s != NULL && (dir = (t->Compare)(n, s)) != 0) {
p = s;
dir = dir < 0;
dir_old = dir;
s = p -> link[dir];
}
if (s == NULL)
return NULL; /* no match found */
save = s;
/*
* First case: if s has no right child, then replace s
* with s's left child.
*/
if (s -> link[RIGHT] == NULL)
s = s -> link[LEFT];
/*
* Second case: if s has a right child that lacks a left
* child, then replace s with s's right child and
* copy s's left child into the right child's left child.
*/
else if (s -> link[RIGHT] -> link[LEFT] == NULL) {
s = s -> link[RIGHT];
s -> link[LEFT] = save -> link[LEFT];
}
/*
* Final case: find leftmost (smallest) node in s's right
* subtree. By definition, this node has an empty left
* link. Free this node by copying its right link to
* its parent's left link and then give it both of s's
* links (thus replacing s).
*/
else {
Bnode *small;
small = s -> link[RIGHT];
while (small -> link[LEFT] -> link[LEFT])
small = small -> link[LEFT];
s = small -> link[LEFT];
small -> link[LEFT] = s -> link[RIGHT];
s -> link[LEFT] = save -> link[LEFT];
s -> link[RIGHT] = save -> link[RIGHT];
}
p -> link[dir_old] = s;
RBONLY(s -> red = 0;)
return save;
}
#endif
/* Insert node n into tree t */
int InsBintree (Bintree *t, Bnode *n)
{
#if defined(REDBLACK)
int p_dir;
Bnode *p, *s;
Bnode *g = NULL;
Bnode *gg = NULL;
/* Search until we find a leaf. */
p = t -> DummyHead;
p_dir = RIGHT; /* direction from p to s */
s = p -> link[RIGHT];
if (s) {
Bnode *temp;
int dir;
/* Look for a leaf, splitting nodes on the way down */
while (s -> link[RIGHT] != NULL) {
if (s -> link[LEFT] -> red &&
s -> link[RIGHT] -> red)
split(t, n, &s, &p, g, gg);
gg = g;
g = p;
p = s;
p_dir = (t -> Compare) (n, s) < 0;
s = s -> link[p_dir];
}
dir = (t -> Compare) (n, s);
if (t -> DuplicatesOk == 0 && dir == 0)
return TREE_FAIL; /* duplicate - not allowed */
/*
* Must replace s with a new internal node that has as
* its children s and n. The new node gets the larger of
* s and n as its key. The new node gets painted red, its
* children are black. Coloring is done by split().
*/
temp = tmalloc(t -> NodeSize);
dir = dir < 0;
CopyDataB((LONG *)temp, dir ? (LONG *)s : (LONG *)n, t -> NodeSize);
temp -> link[dir] = n;
temp -> link[!dir] = s;
n = temp;
}
/* Add the new node */
p -> link[p_dir] = n;
/* Color this node red and check red-black balance */
split(t, n, &n, &p, g, gg);
return TREE_OK;
#elif defined(SPLAY)
int dir;
Bnode *r;
dir = splay(t, n);
if (dir == 0 && t -> DuplicatesOk == 0)
return TREE_FAIL;
r = t -> DummyHead -> link[RIGHT];
if (r == NULL) /* first node? */
t -> DummyHead -> link[RIGHT] = n;
else {
dir = dir < 0;
n -> link[dir] = r -> link[dir];
r -> link[dir] = NULL;
n -> link[!dir] = r;
t -> DummyHead -> link[RIGHT] = n;
}
return TREE_OK;
#else /* plain binary tree */
int p_dir;
Bnode *p, *s;
/* Search until we find an empty arm. */
p = t -> DummyHead;
p_dir = RIGHT; /* direction from p to s */
s = p -> link[RIGHT];
while (s != NULL) {
p = s;
p_dir = (t -> Compare) (n, s);
if (p_dir == 0 && t -> DuplicatesOk == 0)
return TREE_FAIL; /* duplicate */
p_dir = p_dir < 0;
s = s -> link[p_dir];
}
/* Add the new node */
p -> link[p_dir] = n;
return TREE_OK;
#endif
}
/*
* Recursive tree walk routines. The entry point is
* WalkBintree. It will do an inorder traversal of the
* tree, call df() for each node and leaf.
*/
void rWalk(Bnode *n, int level, DoFunc df)
{
if (n != NULL) {
rWalk(n -> link[LEFT], level + 1, df);
df(n, level);
rWalk(n -> link[RIGHT], level + 1, df);
}
}
int WalkBintree(Bintree *t, DoFunc df)
{
if (t -> DummyHead -> link[RIGHT] == NULL)
{
printf("Empty tree\n");
return TREE_FAIL;
}
rWalk(t -> DummyHead -> link[RIGHT], 0, df);
return TREE_OK;
}
#if defined(TEST)
/*
* Test driver
*/
#define BUFLEN 100
/* Our binary tree is made up of these */
typedef struct sMynode {
/* A copy of the items in a Bnode */
BINTREE_STUFF(sMynode);
/*
* Now for the user's part of the structure. We could put
* anything here. For these routines, a simple text area.
*/
char text[80];
} Mynode;
int LoadString(Bintree *t, char *string)
{
Mynode *m;
m = (Mynode *) InitBintreeNode(sizeof(Mynode));
strncpy(m->text, string, sizeof(m->text));
m->text[sizeof(m->text) - 1] = 0;
return InsBintree(t, (Bnode *) m);
}
void FindString(Bintree *t, char *string)
{
Mynode m, *r;
strncpy(m.text, string, sizeof(m.text));
m.text[sizeof(m.text) - 1] = 0;
if ((r = (Mynode *) FindBintree(t, (Bnode *) &m)) == NULL)
printf(" Not found.\n");
else
printf(" Found '%s'.\n", r -> text);
}
void DeleteString(Bintree *t, char *string)
{
Mynode m, *n;
strncpy(m.text, string, sizeof(m.text));
m.text[sizeof(m.text) - 1] = 0;
n = (Mynode *) DelBintree(t, (Bnode *) &m);
if (n)
free (n);
else
printf(" Did not find '%s'.\n", string);
}
void LoadTreeFile(Bintree *t, char *fname)
{
FILE *infile;
char buffer[BUFLEN], *s;
int i = 0, j = 0;
if ((infile = fopen(fname, "r")) == NULL)
{
printf(" Couldn't open the file.\n");
return;
}
while (fgets(buffer, BUFLEN, infile))
{
s = buffer + strlen(buffer);
while(iscntrl(*s))
*s-- = 0;
if (buffer[0] == ';') /* a comment */
;
else if (buffer[0] == '-' && buffer[1] != 0) {
DeleteString(t, buffer+1);
j++;
}
else {
LoadString(t, buffer);
i++;
}
}
fclose(infile);
printf("Loaded %d items and deleted %d from %s.\n",
i, j, fname);
}
void DeleteTreeFile(Bintree *t, char *fname)
{
FILE *infile;
char buffer[BUFLEN], *s;
int j = 0;
if ((infile = fopen(fname, "r")) == NULL)
{
printf(" Couldn't open the file.\n");
return;
}
while (fgets(buffer, BUFLEN, infile))
{
s = buffer + strlen(buffer);
while(iscntrl(*s))
*s-- = 0;
if (buffer[0] == ';') /* a comment */
;
else if (buffer[0] == '-' && buffer[1] != 0) {
DeleteString(t, buffer+1);
j++;
}
else {
DeleteString(t, buffer);
j++;
}
}
fclose(infile);
printf("deleted %d from %s.\n",
j, fname);
}
/*
* A sample action function: it prints out the data
* at each node along with the node's level in the tree
*/
int ShowFunc(void *m, int level)
{
RBONLY(if (((Mynode *)m) -> link[LEFT] == NULL))
printf("%s (%d)\n", ((Mynode *)m) -> text, level);
return TREE_OK;
}
/*
* A pair of functions to print the tree as a diagram.
*/
#if !defined(ALTDRAW)
#define TOP 'Ú'
#define BOT 'À'
#define HOR 'Ä'
#define VRT '³'
#else
#define TOP '/'
#define BOT '\\'
#define HOR '-'
#define VRT '|'
#endif
#if defined(REDBLACK)
#if !defined(ALTDRAW)
#define RTOP 'É'
#define RBOT 'È'
#define RHOR 'Í'
#define RVRT 'º'
#else
#define RTOP '*'
#define RBOT '*'
#define RHOR '#'
#define RVRT '#'
#endif
#endif
#define DRAWBUF 100
char draw[DRAWBUF];
char work[DRAWBUF * 2];
int maxdepth;
RBONLY(int blackheight;)
RBONLY(int maxblack;)
FILE *outfile;
void xrWalk(Bnode *n, int level)
{
int i;
if (n != NULL) {
/* Monitor */
if (level > maxdepth)
maxdepth = level;
RBONLY(if (!n -> red) blackheight++;)
/*
* Go right
*/
draw[level * 2] = TOP;
#if defined(REDBLACK)
if (n -> link[RIGHT] && n -> link[RIGHT] -> red)
draw[level * 2] = RTOP;
#endif
draw[level * 2 + 1] = ' ';
xrWalk(n -> link[RIGHT], level + 1);
/*
* Show current node
*/
strncpy(work, draw, level * 2);
if (level > 0) {
int c;
c = work[0];
for (i = 2; i < level * 2; i += 2)
#if !defined(REDBLACK)
if (work[i] == c)
#else
if (((c == TOP || c == RTOP) &&
(work[i] == TOP || work[i] == RTOP)) ||
((c == BOT || c == RBOT) &&
(work[i] == BOT || work[i] == RBOT)))
#endif
work[i - 2] = ' ';
else
c = work[i];
work[level * 2 - 1] =
RBONLY(((Mynode *)n) -> red ? RHOR :)
HOR;
for (i = 0; i < level * 2 - 2; i += 2)
if (work[i] != ' ') {
#if !defined(REDBLACK)
work[i] = VRT;
#else
if (work[i] == TOP || work[i] == BOT)
work[i] = VRT;
else
work[i] = RVRT;
#endif
}
}
sprintf(work + level * 2, "%s (%d)",
((Mynode *)n)->text, level);
printf(work, outfile);
#if defined(REDBLACK)
if (n -> link[LEFT] == NULL) { /* leaf */
if (maxblack < 0)
maxblack = blackheight;
else if (maxblack != blackheight)
printf(" Leaf has black height %d!",
blackheight - 1);
}
#endif
printf("\n");
/*
* Go left
*/
draw[level * 2] = BOT;
#if defined(REDBLACK)
if (n -> link[LEFT] && n -> link[LEFT] -> red)
draw[level * 2] = RBOT;