-
Notifications
You must be signed in to change notification settings - Fork 6
/
interval_tree.c
793 lines (706 loc) · 23.3 KB
/
interval_tree.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
#include "interval_tree.h"
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
#include<sys/unistd.h>
#include<unistd.h>
// If the symbol CHECK_INTERVAL_TREE_ASSUMPTIONS is defined then the
// code does a lot of extra checking to make sure certain assumptions
// are satisfied. This only needs to be done if you suspect bugs are
// present or if you make significant changes and want to make sure
// your changes didn't mess anything up.
// #define CHECK_INTERVAL_TREE_ASSUMPTIONS 1
const int MIN_INT=-MAX_INT;
// define a function to find the maximum of two objects.
#define ITMax(a, b) ( (a > b) ? a : b )
inline void Assert(int assertion, char* error) {
if(!assertion) {
printf("Assertion Failed: %s\n",error);
exit(1);
}
}
IntervalTreeNode::IntervalTreeNode(){}
IntervalTreeNode::IntervalTreeNode(Interval * newInterval)
: storedInterval (newInterval) ,
key(newInterval->GetLowPoint()),
high(newInterval->GetHighPoint()) ,
maxHigh(high) {}
IntervalTreeNode::~IntervalTreeNode(){}
Interval::Interval(){}
Interval::~Interval(){}
void Interval::Print() const {}
IntervalTree::IntervalTree()
{
nil = new IntervalTreeNode;
nil->left = nil->right = nil->parent = nil;
nil->red = 0;
nil->key = nil->high = nil->maxHigh = MIN_INT;
nil->storedInterval = NULL;
root = new IntervalTreeNode;
root->parent = root->left = root->right = nil;
root->key = root->high = root->maxHigh = MAX_INT;
root->red=0;
root->storedInterval = NULL;
/* the following are used for the Enumerate function */
recursionNodeStackSize = 128;
recursionNodeStack = (it_recursion_node *)
malloc(recursionNodeStackSize*sizeof(it_recursion_node));
recursionNodeStackTop = 1;
recursionNodeStack[0].start_node = NULL;
}
/***********************************************************************/
/* FUNCTION: LeftRotate */
/**/
/* INPUTS: the node to rotate on */
/**/
/* OUTPUT: None */
/**/
/* Modifies Input: this, x */
/**/
/* EFFECTS: Rotates as described in _Introduction_To_Algorithms by */
/* Cormen, Leiserson, Rivest (Chapter 14). Basically this */
/* makes the parent of x be to the left of x, x the parent of */
/* its parent before the rotation and fixes other pointers */
/* accordingly. Also updates the maxHigh fields of x and y */
/* after rotation. */
/***********************************************************************/
void IntervalTree::LeftRotate(IntervalTreeNode* x) {
IntervalTreeNode* y;
/* I originally wrote this function to use the sentinel for */
/* nil to avoid checking for nil. However this introduces a */
/* very subtle bug because sometimes this function modifies */
/* the parent pointer of nil. This can be a problem if a */
/* function which calls LeftRotate also uses the nil sentinel */
/* and expects the nil sentinel's parent pointer to be unchanged */
/* after calling this function. For example, when DeleteFixUP */
/* calls LeftRotate it expects the parent pointer of nil to be */
/* unchanged. */
y=x->right;
x->right=y->left;
if (y->left != nil) y->left->parent=x; /* used to use sentinel here */
/* and do an unconditional assignment instead of testing for nil */
y->parent=x->parent;
/* instead of checking if x->parent is the root as in the book, we */
/* count on the root sentinel to implicitly take care of this case */
if( x == x->parent->left) {
x->parent->left=y;
} else {
x->parent->right=y;
}
y->left=x;
x->parent=y;
x->maxHigh=ITMax(x->left->maxHigh,ITMax(x->right->maxHigh,x->high));
y->maxHigh=ITMax(x->maxHigh,ITMax(y->right->maxHigh,y->high));
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not red in ITLeftRotate");
Assert((nil->maxHigh=MIN_INT),
"nil->maxHigh != MIN_INT in ITLeftRotate");
#endif
}
/***********************************************************************/
/* FUNCTION: RighttRotate */
/**/
/* INPUTS: node to rotate on */
/**/
/* OUTPUT: None */
/**/
/* Modifies Input?: this, y */
/**/
/* EFFECTS: Rotates as described in _Introduction_To_Algorithms by */
/* Cormen, Leiserson, Rivest (Chapter 14). Basically this */
/* makes the parent of x be to the left of x, x the parent of */
/* its parent before the rotation and fixes other pointers */
/* accordingly. Also updates the maxHigh fields of x and y */
/* after rotation. */
/***********************************************************************/
void IntervalTree::RightRotate(IntervalTreeNode* y) {
IntervalTreeNode* x;
/* I originally wrote this function to use the sentinel for */
/* nil to avoid checking for nil. However this introduces a */
/* very subtle bug because sometimes this function modifies */
/* the parent pointer of nil. This can be a problem if a */
/* function which calls LeftRotate also uses the nil sentinel */
/* and expects the nil sentinel's parent pointer to be unchanged */
/* after calling this function. For example, when DeleteFixUP */
/* calls LeftRotate it expects the parent pointer of nil to be */
/* unchanged. */
x=y->left;
y->left=x->right;
if (nil != x->right) x->right->parent=y; /*used to use sentinel here */
/* and do an unconditional assignment instead of testing for nil */
/* instead of checking if x->parent is the root as in the book, we */
/* count on the root sentinel to implicitly take care of this case */
x->parent=y->parent;
if( y == y->parent->left) {
y->parent->left=x;
} else {
y->parent->right=x;
}
x->right=y;
y->parent=x;
y->maxHigh=ITMax(y->left->maxHigh,ITMax(y->right->maxHigh,y->high));
x->maxHigh=ITMax(x->left->maxHigh,ITMax(y->maxHigh,x->high));
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not red in ITRightRotate");
Assert((nil->maxHigh=MIN_INT),
"nil->maxHigh != MIN_INT in ITRightRotate");
#endif
}
/***********************************************************************/
/* FUNCTION: TreeInsertHelp */
/**/
/* INPUTS: z is the node to insert */
/**/
/* OUTPUT: none */
/**/
/* Modifies Input: this, z */
/**/
/* EFFECTS: Inserts z into the tree as if it were a regular binary tree */
/* using the algorithm described in _Introduction_To_Algorithms_ */
/* by Cormen et al. This funciton is only intended to be called */
/* by the InsertTree function and not by the user */
/***********************************************************************/
void IntervalTree::TreeInsertHelp(IntervalTreeNode* z) {
/* This function should only be called by InsertITTree (see above) */
IntervalTreeNode* x;
IntervalTreeNode* y;
z->left=z->right=nil;
y=root;
x=root->left;
while( x != nil) {
y=x;
if ( x->key > z->key) {
x=x->left;
} else { /* x->key <= z->key */
x=x->right;
}
}
z->parent=y;
if ( (y == root) ||
(y->key > z->key) ) {
y->left=z;
} else {
y->right=z;
}
#if defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not red in ITTreeInsertHelp");
Assert((nil->maxHigh=MIN_INT),
"nil->maxHigh != MIN_INT in ITTreeInsertHelp");
#endif
}
/***********************************************************************/
/* FUNCTION: FixUpMaxHigh */
/**/
/* INPUTS: x is the node to start from*/
/**/
/* OUTPUT: none */
/**/
/* Modifies Input: this */
/**/
/* EFFECTS: Travels up to the root fixing the maxHigh fields after */
/* an insertion or deletion */
/***********************************************************************/
void IntervalTree::FixUpMaxHigh(IntervalTreeNode * x) {
while(x != root) {
x->maxHigh=ITMax(x->high,ITMax(x->left->maxHigh,x->right->maxHigh));
x=x->parent;
}
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#endif
}
/* Before calling InsertNode the node x should have its key set */
/***********************************************************************/
/* FUNCTION: InsertNode */
/**/
/* INPUTS: newInterval is the interval to insert*/
/**/
/* OUTPUT: This function returns a pointer to the newly inserted node */
/* which is guarunteed to be valid until this node is deleted. */
/* What this means is if another data structure stores this */
/* pointer then the tree does not need to be searched when this */
/* is to be deleted. */
/**/
/* Modifies Input: tree */
/**/
/* EFFECTS: Creates a node node which contains the appropriate key and */
/* info pointers and inserts it into the tree. */
/***********************************************************************/
IntervalTreeNode * IntervalTree::Insert(Interval * newInterval)
{
IntervalTreeNode * y;
IntervalTreeNode * x;
IntervalTreeNode * newNode;
x = new IntervalTreeNode(newInterval);
TreeInsertHelp(x);
FixUpMaxHigh(x->parent);
newNode = x;
x->red=1;
while(x->parent->red) { /* use sentinel instead of checking for root */
if (x->parent == x->parent->parent->left) {
y=x->parent->parent->right;
if (y->red) {
x->parent->red=0;
y->red=0;
x->parent->parent->red=1;
x=x->parent->parent;
} else {
if (x == x->parent->right) {
x=x->parent;
LeftRotate(x);
}
x->parent->red=0;
x->parent->parent->red=1;
RightRotate(x->parent->parent);
}
} else { /* case for x->parent == x->parent->parent->right */
/* this part is just like the section above with */
/* left and right interchanged */
y=x->parent->parent->left;
if (y->red) {
x->parent->red=0;
y->red=0;
x->parent->parent->red=1;
x=x->parent->parent;
} else {
if (x == x->parent->left) {
x=x->parent;
RightRotate(x);
}
x->parent->red=0;
x->parent->parent->red=1;
LeftRotate(x->parent->parent);
}
}
}
root->left->red=0;
return(newNode);
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not red in ITTreeInsert");
Assert(!root->red,"root not red in ITTreeInsert");
Assert((nil->maxHigh=MIN_INT),
"nil->maxHigh != MIN_INT in ITTreeInsert");
#endif
}
/***********************************************************************/
/* FUNCTION: GetSuccessorOf */
/**/
/* INPUTS: x is the node we want the succesor of */
/**/
/* OUTPUT: This function returns the successor of x or NULL if no */
/* successor exists. */
/**/
/* Modifies Input: none */
/**/
/* Note: uses the algorithm in _Introduction_To_Algorithms_ */
/***********************************************************************/
IntervalTreeNode * IntervalTree::GetSuccessorOf(IntervalTreeNode * x) const
{
IntervalTreeNode* y;
if (nil != (y = x->right)) { /* assignment to y is intentional */
while(y->left != nil) { /* returns the minium of the right subtree of x */
y=y->left;
}
return(y);
} else {
y=x->parent;
while(x == y->right) { /* sentinel used instead of checking for nil */
x=y;
y=y->parent;
}
if (y == root) return(nil);
return(y);
}
}
/***********************************************************************/
/* FUNCTION: GetPredecessorOf */
/**/
/* INPUTS: x is the node to get predecessor of */
/**/
/* OUTPUT: This function returns the predecessor of x or NULL if no */
/* predecessor exists. */
/**/
/* Modifies Input: none */
/**/
/* Note: uses the algorithm in _Introduction_To_Algorithms_ */
/***********************************************************************/
IntervalTreeNode * IntervalTree::GetPredecessorOf(IntervalTreeNode * x) const {
IntervalTreeNode* y;
if (nil != (y = x->left)) { /* assignment to y is intentional */
while(y->right != nil) { /* returns the maximum of the left subtree of x */
y=y->right;
}
return(y);
} else {
y=x->parent;
while(x == y->left) {
if (y == root) return(nil);
x=y;
y=y->parent;
}
return(y);
}
}
/***********************************************************************/
/* FUNCTION: Print */
/**/
/* INPUTS: none */
/**/
/* OUTPUT: none */
/**/
/* EFFECTS: This function recursively prints the nodes of the tree */
/* inorder. */
/**/
/* Modifies Input: none */
/**/
/* Note: This function should only be called from ITTreePrint */
/***********************************************************************/
void IntervalTreeNode::Print(IntervalTreeNode * nil,
IntervalTreeNode * root) const {
storedInterval->Print();
printf(", k=%i, h=%i, mH=%i",key,high,maxHigh);
printf(" l->key=");
if( left == nil) printf("NULL"); else printf("%i",left->key);
printf(" r->key=");
if( right == nil) printf("NULL"); else printf("%i",right->key);
printf(" p->key=");
if( parent == root) printf("NULL"); else printf("%i",parent->key);
printf(" red=%i\n",red);
}
void IntervalTree::TreePrintHelper( IntervalTreeNode* x) const {
if (x != nil) {
TreePrintHelper(x->left);
x->Print(nil,root);
TreePrintHelper(x->right);
}
}
IntervalTree::~IntervalTree() {
IntervalTreeNode * x = root->left;
TemplateStack<IntervalTreeNode *> stuffToFree;
if (x != nil) {
if (x->left != nil) {
stuffToFree.Push(x->left);
}
if (x->right != nil) {
stuffToFree.Push(x->right);
}
// delete x->storedInterval;
delete x;
while( stuffToFree.NotEmpty() ) {
x = stuffToFree.Pop();
if (x->left != nil) {
stuffToFree.Push(x->left);
}
if (x->right != nil) {
stuffToFree.Push(x->right);
}
// delete x->storedInterval;
delete x;
}
}
delete nil;
delete root;
free(recursionNodeStack);
}
/***********************************************************************/
/* FUNCTION: Print */
/**/
/* INPUTS: none */
/**/
/* OUTPUT: none */
/**/
/* EFFECT: This function recursively prints the nodes of the tree */
/* inorder. */
/**/
/* Modifies Input: none */
/**/
/***********************************************************************/
void IntervalTree::Print() const {
TreePrintHelper(root->left);
}
/***********************************************************************/
/* FUNCTION: DeleteFixUp */
/**/
/* INPUTS: x is the child of the spliced */
/* out node in DeleteNode. */
/**/
/* OUTPUT: none */
/**/
/* EFFECT: Performs rotations and changes colors to restore red-black */
/* properties after a node is deleted */
/**/
/* Modifies Input: this, x */
/**/
/* The algorithm from this function is from _Introduction_To_Algorithms_ */
/***********************************************************************/
void IntervalTree::DeleteFixUp(IntervalTreeNode* x) {
IntervalTreeNode * w;
IntervalTreeNode * rootLeft = root->left;
while( (!x->red) && (rootLeft != x)) {
if (x == x->parent->left) {
w=x->parent->right;
if (w->red) {
w->red=0;
x->parent->red=1;
LeftRotate(x->parent);
w=x->parent->right;
}
if ( (!w->right->red) && (!w->left->red) ) {
w->red=1;
x=x->parent;
} else {
if (!w->right->red) {
w->left->red=0;
w->red=1;
RightRotate(w);
w=x->parent->right;
}
w->red=x->parent->red;
x->parent->red=0;
w->right->red=0;
LeftRotate(x->parent);
x=rootLeft; /* this is to exit while loop */
}
} else { /* the code below is has left and right switched from above */
w=x->parent->left;
if (w->red) {
w->red=0;
x->parent->red=1;
RightRotate(x->parent);
w=x->parent->left;
}
if ( (!w->right->red) && (!w->left->red) ) {
w->red=1;
x=x->parent;
} else {
if (!w->left->red) {
w->right->red=0;
w->red=1;
LeftRotate(w);
w=x->parent->left;
}
w->red=x->parent->red;
x->parent->red=0;
w->left->red=0;
RightRotate(x->parent);
x=rootLeft; /* this is to exit while loop */
}
}
}
x->red=0;
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not black in ITDeleteFixUp");
Assert((nil->maxHigh=MIN_INT),
"nil->maxHigh != MIN_INT in ITDeleteFixUp");
#endif
}
/***********************************************************************/
/* FUNCTION: DeleteNode */
/**/
/* INPUTS: tree is the tree to delete node z from */
/**/
/* OUTPUT: returns the Interval stored at deleted node */
/**/
/* EFFECT: Deletes z from tree and but don't call destructor */
/* Then calls FixUpMaxHigh to fix maxHigh fields then calls */
/* ITDeleteFixUp to restore red-black properties */
/**/
/* Modifies Input: z */
/**/
/* The algorithm from this function is from _Introduction_To_Algorithms_ */
/***********************************************************************/
Interval * IntervalTree::DeleteNode(IntervalTreeNode * z){
IntervalTreeNode* y;
IntervalTreeNode* x;
Interval * returnValue = z->storedInterval;
y= ((z->left == nil) || (z->right == nil)) ? z : GetSuccessorOf(z);
x= (y->left == nil) ? y->right : y->left;
if (root == (x->parent = y->parent)) { /* assignment of y->p to x->p is intentional */
root->left=x;
} else {
if (y == y->parent->left) {
y->parent->left=x;
} else {
y->parent->right=x;
}
}
if (y != z) { /* y should not be nil in this case */
#ifdef DEBUG_ASSERT
Assert( (y!=nil),"y is nil in DeleteNode \n");
#endif
/* y is the node to splice out and x is its child */
y->maxHigh = MIN_INT;
y->left=z->left;
y->right=z->right;
y->parent=z->parent;
z->left->parent=z->right->parent=y;
if (z == z->parent->left) {
z->parent->left=y;
} else {
z->parent->right=y;
}
FixUpMaxHigh(x->parent);
if (!(y->red)) {
y->red = z->red;
DeleteFixUp(x);
} else
y->red = z->red;
delete z;
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not black in ITDelete");
Assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete");
#endif
} else {
FixUpMaxHigh(x->parent);
if (!(y->red)) DeleteFixUp(x);
delete y;
#ifdef CHECK_INTERVAL_TREE_ASSUMPTIONS
CheckAssumptions();
#elif defined(DEBUG_ASSERT)
Assert(!nil->red,"nil not black in ITDelete");
Assert((nil->maxHigh=MIN_INT),"nil->maxHigh != MIN_INT in ITDelete");
#endif
}
return returnValue;
}
/***********************************************************************/
/* FUNCTION: Overlap */
/**/
/* INPUTS: [a1,a2] and [b1,b2] are the low and high endpoints of two */
/* closed intervals. */
/**/
/* OUTPUT: stack containing pointers to the nodes between [low,high] */
/**/
/* Modifies Input: none */
/**/
/* EFFECT: returns 1 if the intervals overlap, and 0 otherwise */
/***********************************************************************/
int Overlap(int a1, int a2, int b1, int b2) {
if (a1 <= b1) {
return( (b1 <= a2) );
} else {
return( (a1 <= b2) );
}
}
/***********************************************************************/
/* FUNCTION: Enumerate */
/**/
/* INPUTS: tree is the tree to look for intervals overlapping the */
/* closed interval [low,high] */
/**/
/* OUTPUT: stack containing pointers to the nodes overlapping */
/* [low,high] */
/**/
/* Modifies Input: none */
/**/
/* EFFECT: Returns a stack containing pointers to nodes containing */
/* intervals which overlap [low,high] in O(max(N,k*log(N))) */
/* where N is the number of intervals in the tree and k is */
/* the number of overlapping intervals */
/**/
/* Note: This basic idea for this function comes from the */
/* _Introduction_To_Algorithms_ book by Cormen et al, but */
/* modifications were made to return all overlapping intervals */
/* instead of just the first overlapping interval as in the */
/* book. The natural way to do this would require recursive */
/* calls of a basic search function. I translated the */
/* recursive version into an interative version with a stack */
/* as described below. */
/***********************************************************************/
/* The basic idea for the function below is to take the IntervalSearch */
/* function from the book and modify to find all overlapping intervals */
/* instead of just one. This means that any time we take the left */
/* branch down the tree we must also check the right branch if and only if */
/* we find an overlapping interval in that left branch. Note this is a */
/* recursive condition because if we go left at the root then go left */
/* again at the first left child and find an overlap in the left subtree */
/* of the left child of root we must recursively check the right subtree */
/* of the left child of root as well as the right child of root. */
TemplateStack<void *> * IntervalTree::Enumerate(int low,
int high) {
TemplateStack<void *> * enumResultStack;
IntervalTreeNode* x=root->left;
int stuffToDo = (x != nil);
// Possible speed up: add min field to prune right searches //
#ifdef DEBUG_ASSERT
Assert((recursionNodeStackTop == 1),
"recursionStack not empty when entering IntervalTree::Enumerate");
#endif
currentParent = 0;
enumResultStack = new TemplateStack<void *>(4);
while(stuffToDo) {
if (Overlap(low,high,x->key,x->high) ) {
enumResultStack->Push(x->storedInterval);
recursionNodeStack[currentParent].tryRightBranch=1;
}
if(x->left->maxHigh >= low) { // implies x != nil
if ( recursionNodeStackTop == recursionNodeStackSize ) {
recursionNodeStackSize *= 2;
recursionNodeStack = (it_recursion_node *)
realloc(recursionNodeStack,
recursionNodeStackSize * sizeof(it_recursion_node));
if (recursionNodeStack == NULL)
exit(1);
}
recursionNodeStack[recursionNodeStackTop].start_node = x;
recursionNodeStack[recursionNodeStackTop].tryRightBranch = 0;
recursionNodeStack[recursionNodeStackTop].parentIndex = currentParent;
currentParent = recursionNodeStackTop++;
x = x->left;
} else {
x = x->right;
}
stuffToDo = (x != nil);
while( (!stuffToDo) && (recursionNodeStackTop > 1) ) {
if(recursionNodeStack[--recursionNodeStackTop].tryRightBranch) {
x=recursionNodeStack[recursionNodeStackTop].start_node->right;
currentParent=recursionNodeStack[recursionNodeStackTop].parentIndex;
recursionNodeStack[currentParent].tryRightBranch=1;
stuffToDo = ( x != nil);
}
}
}
#ifdef DEBUG_ASSERT
Assert((recursionNodeStackTop == 1),
"recursionStack not empty when exiting IntervalTree::Enumerate");
#endif
return(enumResultStack);
}
int IntervalTree::CheckMaxHighFieldsHelper(IntervalTreeNode * y,
const int currentHigh,
int match) const
{
if (y != nil) {
match = CheckMaxHighFieldsHelper(y->left,currentHigh,match) ?
1 : match;
if (y->high == currentHigh)
match = 1;
match = CheckMaxHighFieldsHelper(y->right,currentHigh,match) ?
1 : match;
}
return match;
}
/* Make sure the maxHigh fields for everything makes sense. *
* If something is wrong, print a warning and exit */
void IntervalTree::CheckMaxHighFields(IntervalTreeNode * x) const {
if (x != nil) {
CheckMaxHighFields(x->left);
if(!(CheckMaxHighFieldsHelper(x,x->maxHigh,0) > 0)) {
exit(1);
}
CheckMaxHighFields(x->right);
}
}
void IntervalTree::CheckAssumptions() const {
CheckMaxHighFields(root->left);
}