-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDualHeap.h
917 lines (662 loc) · 19.8 KB
/
DualHeap.h
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
/** \file DualHeap.h
\brief Function prototypes and implementations for the DualHeap class
This contains the header file and implementation for methods of DualHeap class.
The following methods are implemented in this file: constructors, mutators, accessors, and helper functions.
*/
#ifndef BplusTournamentSort_DualHeap_h
#define BplusTournamentSort_DualHeap_h
#include <iostream>
#include <vector>
#include <limits>
#include <assert.h>
using namespace std;
// maxmum number of items that heap can hold
//#define HEAPSIZE 3
// ------------------------------
// Declaration of DualHeap Class
// ------------------------------
template <class Type>
class DualHeap
{
private:
//-- Attributes --//
// flag:
// True => active heap's head be left, pending's be right
// False => active heap's head be right, pending's be left
bool direction;
int HEAPSIZE;
//bool setmaxmin;
Type maxmin; // will be compared to new item
//string maxminstr;
vector<Type> data; // array structure used for heap
vector<Type> run; // output run
vector<vector<Type> > buffer; // buffer = list of lists
int current_size; // size of active heap
int pending_size; // size of pending heap
//-- Supporting methods --//
/** \fn int l_child(int)
* \brief gets the index of the left child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child is returned
*/
int l_child(int); // index of left child: general version
/** \fn int r_child(int)
* \brief gets the index of the right child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the right child is returned
*/
int r_child(int); // index of right child: general version
/** \fn int parent(int)
* \brief gets the index of the parent node
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node is returned
*/
int parent(int); // index of parent node: general version
/** \fn int current_l_child(int)
* \brief gets the index of the left child for the active heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child for the active heap is returned
*/
int current_l_child(int); // l_child for active heap
/** \fn int current_r_child(int)
* \brief gets the index of the right child for the active heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the right child for the active heap is returned
*/
int current_r_child(int); // r_child for active heap
/** \fn int current_parent(int)
* \brief gets the index of the parent node for the active heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node for the active heap is returned
*/
int current_parent(int);
/** \fn int pending_l_child(int)
* \brief gets the index of the left child for the pending heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child for the pending heap is returned
*/
int pending_l_child(int);
/** \fn int pending_r_child(int)
* \brief gets the index of the right child for the pending heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the right child for the pending heap is returned
*/
int pending_r_child(int);
/** \fn int pending_parent(int)
* \brief gets the index of the parent node for the pending heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node for the pending heap is returned
*/
int pending_parent(int);
/** \fn bool exist_current_l_child(int)
* \brief verifies whether the active heap has a left child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the left child for the active heap is returned
*/
bool exist_current_l_child(int);
/** \fn bool exist_current_r_child(int)
* \brief verifies whether the active heap has a right child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \pre the direction should be checked
* \post the index of the right child for the active heap is returned
*/
bool exist_current_r_child(int);
/** \fn bool exist_current_parent(int)
* \brief verifies whether the active heap has a parent node
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the index of the parent node for the active heap is returned
*/
bool exist_current_parent(int);
/** \fn bool exist_pending_l_child(int)
* \brief verifies whether the pending heap has a left child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \pre the direction should be checked
* \post the index of the left child for the pending heap is returned
*/
bool exist_pending_l_child(int);
/** \fn bool exist_pending_r_child(int)
* \brief verifies whether the pending heap has a right child
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \pre the direction should be checked
* \post the index of the right child for the pending heap is returned
*/
bool exist_pending_r_child(int);
/** \fn bool exist_pending_parent(int)
* \brief verifies whether the pending heap has a parent node
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \pre the direction should be checked
* \post the index of the parent node for the pending heap is returned
*/
bool exist_pending_parent(int);
/** \fn bool current_heap_push(Type)
* \brief pushes the new item into the active heap
* \param item is Type for the new item to be pushed
* \pre the parameter should be a Type
* \pre the active heap should not be full
* \post the new item is inserted into the active heap and the size of active heap incremented
*/
bool current_heap_push(Type); // will increments size, need manual heapify
/** \fn bool pending_heap_push(Type)
* \brief pushes the new item into the pending heap
* \param item is Type for the new item to be pushed
* \pre the parameter should be a Type
* \pre the pending heap should not be full
* \post the new item is inserted into the pending heap and the size of pending heap incremented
*/
bool pending_heap_push(Type); // will increments size, need manual heapify
/** \fn Type current_heap_pop()
* \brief pops an item from the active heap
* \pre the active heap should not be empty
* \post the new item is returned at the root of heap and the size of active heap decremented
*/
Type current_heap_pop(); // pop from active // need manually decrement size, heapify // => will decrements
/** \fn int current_next(int)
* \brief gets the next index for the active heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the next index for the active heap is returned
*/
int current_next(int); // next index for active heap
/** \fn int pending_next(int)
* \brief gets the next index for the pending heap
* \param n is an int for the index
* \pre the parameter should be entered as an int
* \post the next index for the pending heap is returned
*/
int pending_next(int); // next index for pending heap
/** \fn int current_head()
* \brief gets the head for the active heap
* \pre the direction should be verified
* \post the head for the active heap is returned
*/
int current_head(); // head of active heap
/** \fn int pending_head()
* \brief gets the head for the pending heap
* \pre the direction should be verified
* \post the head for the pending heap is returned
*/
int pending_head(); // head of pending heap
/** \fn int current_tail()
* \brief gets the tail for the active heap
* \pre the direction should be verified
* \post the tail for the active heap is returned
*/
int current_tail(); // tail of active heap
/** \fn int pending_tail()
* \brief gets the tail for the pending heap
* \pre the direction should be verified
* \post the tail for the pending heap is returned
*/
int pending_tail(); // tail of pending heap
/** \fn bool full()
* \brief verifies whether the heap has any available spot
* \pre the heap is filled with items from the active and pending heaps
* \post returns whether there is space in the heap
*/
bool full(); // no available spot?
/** \fn bool fullwith_pending()
* \brief verifies whether the heap is filled with pending items
* \pre the heap is filled with items from the pending heap
* \post returns whether there is space in the heap
*/
bool fullwith_pending(); // heap totally filled with pending items
/** \fn void finalize()
* \brief gets items from active heap to obtain the runs
* \pre the items are retrieved from the active heap
* \post there are no items left in the active heap
*/
void finalize(); // used by retrieve(), signaling there's no new item
/** \fn void current_heapify_up(int)
* \brief heapifies up a node in active heap
* \param n is an int for the index
* \pre checks whether the item in active heap's head is the root
* \post a node is heapified up in active heap
*/
void current_heapify_up(int); // heapify up a node in active heap
/** \fn void current_heapify_down(int)
* \brief heapifies down a node in active heap
* \param n is an int for the index
* \pre checks whether the root has children
* \post a node is heapified down in active heap
*/
void current_heapify_down(int); // heapify down a node in active heap
/** \fn void pending_heapify_up(int)
* \brief heapifies up a node in pending heap
* \param n is an int for the index
* \pre checks whether the item in active heap's head is the root
* \post a node is heapified up in pending heap
*/
void pending_heapify_up(int); // heapify up a node in pending heap
public:
/** Default constructor */
DualHeap();
/** Copy constructor */
DualHeap(int);
/** \fn void put(Type)
* \brief puts item into heap
* \param item is a Type for the item
* \pre checks whether heap is full with pending items
* \post the item is inserted in the heap
*/
void put(Type); // put items into heap
/** \fn vector<vector<Type> > retrieve()
* \brief returns list of lists
* \pre there should some lists to put in the buffer
* \post the buffer holding the lists is returned
*/
vector<vector<Type> > retrieve(); // returns list of lists
};
// ------------------------------
// Definition of DualHeap Class
// ------------------------------
//-- Public Methods --//
template <class Type>
DualHeap<Type>::DualHeap()
{
// heapsize is 3
HEAPSIZE = 3;
data.reserve(HEAPSIZE);
data.resize(HEAPSIZE);
direction = true;
current_size = 0;
pending_size = 0;
}
template <class Type>
DualHeap<Type>::DualHeap(int heapsize)
{
HEAPSIZE = heapsize;
data.reserve(HEAPSIZE);
data.resize(HEAPSIZE);
direction = true;
current_size = 0;
pending_size = 0;
}
template <class Type>
void DualHeap<Type>::put(Type item)
{
if (fullwith_pending())
{
buffer.push_back(run);
run = vector<Type>(); // empty output run
direction = not direction;
assert(current_size == 0);
current_size = pending_size;
pending_size = 0;
}
if (!full())
{
current_heap_push(item);
}
else
{
maxmin = current_heap_pop();
if (maxmin > item)
{
run.push_back(maxmin);
pending_heap_push(item);
}
else
{
run.push_back(maxmin);
current_heap_push(item);
}
}
}
template <class Type>
vector<vector<Type> > DualHeap<Type>::retrieve()
{
finalize();
return buffer;
}
template <class Type>
void DualHeap<Type>::finalize()
{
Type tempitem;
int heapsize = current_size;
for (int i = 0; i < heapsize; i++)
{
tempitem = current_heap_pop();
run.push_back(tempitem);
}
if (run.size() > 0)
{
buffer.push_back(run);
run = vector<Type>();
direction = not direction;
current_size = pending_size;
pending_size = 0;
}
heapsize = current_size;
for (int i = 0; i < heapsize; i++)
{
run.push_back(current_heap_pop());
}
if (run.size() > 0)
{
buffer.push_back(run);
run = vector<Type>();
direction = not direction;
current_size = 0;
}
}
//---- Support Methods ----//
//-- Private Methods --//
//-- Index Getter --//
template <class Type>
int DualHeap<Type>::l_child(int n)
{
return 2 * n + 1;
}
template <class Type>
int DualHeap<Type>::r_child(int n)
{
return 2 * (n + 1);
}
template <class Type>
int DualHeap<Type>::parent(int n)
{
return (n-1)/2;
}
template <class Type>
int DualHeap<Type>::current_l_child(int n)
{
if (direction)
{
return l_child(n);
}
else
{
return (HEAPSIZE - 1) - l_child((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::current_r_child(int n)
{
if (direction)
{
return r_child(n);
}
else
{
return (HEAPSIZE - 1) - r_child((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::current_parent(int n)
{
if (direction)
{
return parent(n);
}
else
{
return (HEAPSIZE - 1) - parent((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::pending_l_child(int n)
{
if (!direction)
{
return l_child(n);
}
else
{
return (HEAPSIZE - 1) - l_child((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::pending_r_child(int n)
{
if (!direction)
{
return r_child(n);
}
else
{
return (HEAPSIZE - 1) - r_child((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::pending_parent(int n)
{
if (!direction)
{
return parent(n);
}
else
{
return (HEAPSIZE - 1) - parent((HEAPSIZE - 1) - n);
}
}
template <class Type>
int DualHeap<Type>::current_next(int n)
{
return (direction)? n+1 : n-1;
}
template <class Type>
int DualHeap<Type>::pending_next(int n)
{
return (direction)? n-1 : n+1;
}
template <class Type>
int DualHeap<Type>::current_head()
{
return (direction)? 0 : (HEAPSIZE - 1);
}
template <class Type>
int DualHeap<Type>::pending_head()
{
return (!direction)? 0 : (HEAPSIZE - 1);
}
template <class Type>
int DualHeap<Type>::current_tail()
{
return (direction)? (current_size - 1) : (HEAPSIZE - current_size);
}
template <class Type>
int DualHeap<Type>::pending_tail()
{
return (!direction)? (pending_size - 1) : (HEAPSIZE - pending_size);
}
template <class Type>
bool DualHeap<Type>::exist_current_parent(int n)
{
if (direction)
{
return current_parent(n) >= 0;
}
else
{
return (HEAPSIZE - 1) >= current_parent(n);
}
}
template <class Type>
bool DualHeap<Type>::exist_current_l_child(int n)
{
if (direction)
{
return current_l_child(n) < current_size;
}
else
{
return (HEAPSIZE - current_size) <= current_l_child(n);
}
}
template <class Type>
bool DualHeap<Type>::exist_current_r_child(int n)
{
if (direction)
{
return current_r_child(n) < current_size;
}
else
{
return (HEAPSIZE - current_size) <= current_r_child(n);
}
}
template <class Type>
bool DualHeap<Type>::exist_pending_parent(int n)
{
if (!direction)
{
return pending_parent(n) >= 0;
}
else
{
return (HEAPSIZE - 1) >= pending_parent(n);
}
}
template <class Type>
bool DualHeap<Type>::exist_pending_l_child(int n)
{
if (!direction)
{
return pending_l_child(n) < current_size;
}
else
{
return (HEAPSIZE - current_size) <= pending_l_child(n);
}
}
template <class Type>
bool DualHeap<Type>::exist_pending_r_child(int n)
{
if (!direction)
{
return pending_r_child(n) < current_size;
}
else
{
return (HEAPSIZE - current_size) <= pending_r_child(n);
}
}
template <class Type>
bool DualHeap<Type>::current_heap_push(Type item)
{
if (full())
{
return false;
}
data[current_next(current_tail())] = item;
current_size++;
current_heapify_up(current_tail());
return true;
}
template <class Type>
bool DualHeap<Type>::pending_heap_push(Type item)
{
if (full())
{
return false;
}
data[pending_next(pending_tail())] = item;
pending_size++;
pending_heapify_up(pending_tail());
return true;
}
template <class Type>
Type DualHeap<Type>::current_heap_pop()
{
Type returndata = data[current_head()];
data[current_head()] = data[current_tail()];
current_size--;
current_heapify_down(current_head());
return returndata;
}
template <class Type>
bool DualHeap<Type>::full()
{
return (current_size + pending_size) == HEAPSIZE;
}
template <class Type>
bool DualHeap<Type>::fullwith_pending()
{
return (pending_size) == HEAPSIZE;
}
template <class Type>
void DualHeap<Type>::current_heapify_up(int n)
{
if (n == current_head()) // at root
return;
if (data[current_parent(n)] > data[n])
{
Type tempdata = data[current_parent(n)];
data[current_parent(n)] = data[n];
data[n] = tempdata;
current_heapify_up(current_parent(n)); // recursive call to parent node
}
}
template <class Type>
void DualHeap<Type>::current_heapify_down(int n)
{
// check if child exists
if (!exist_current_l_child(n))
{ // both children not exist
return;
}
else if (!exist_current_r_child(n))
{ // only left exist
if (data[current_l_child(n)] < data[n])
{
Type tempdata = data[current_l_child(n)];
data[current_l_child(n)] = data[n];
data[n] = tempdata;
current_heapify_down(current_l_child(n));
}
return;
}
else // both exist
// if left child is smaller than right
if (data[current_l_child(n)] < data[current_r_child(n)])
{
if (data[current_l_child(n)] < data[n])
{
Type tempdata = data[current_l_child(n)];
data[current_l_child(n)] = data[n];
data[n] = tempdata;
current_heapify_down(current_l_child(n));
}
}
// if right child is smaller
else
{
if (data[current_r_child(n)] < data[n])
{
Type tempdata = data[current_r_child(n)];
data[current_r_child(n)] = data[n];
data[n] = tempdata;
current_heapify_down(current_r_child(n));
}
}
return;
}
template <class Type>
void DualHeap<Type>::pending_heapify_up(int n)
{
if (n == pending_head())
return;
if (data[pending_parent(n)] > data[n])
{
Type tempdata = data[pending_parent(n)];
data[pending_parent(n)] = data[n];
data[n] = tempdata;
pending_heapify_up(pending_parent(n)); // recursive call to parent node
}
}
#endif