Skip to content

Commit 16fa68a

Browse files
committed
Implemented heap priority queue unit test.
1 parent cf09c15 commit 16fa68a

File tree

7 files changed

+556
-9
lines changed

7 files changed

+556
-9
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ add_executable(C_DataStructures
200200
"Unit Test/Tests/DataStructuresTests/TreesTest/SplayTreeTest/SplayTreeTest.h"
201201
"Unit Test/Tests/DataStructuresTests/PairsTest/PairTest/PairTest.c"
202202
"Unit Test/Tests/DataStructuresTests/PairsTest/PairTest/PairTest.h"
203-
DataStructure/Queues/HPriorityQueue.c DataStructure/Queues/HPriorityQueue.h)
203+
DataStructure/Queues/Sources/HPriorityQueue.c DataStructure/Queues/Headers/HPriorityQueue.h "Unit Test/Tests/DataStructuresTests/QueuesTest/HeapPriorityQueueTest/HeapPriorityQueueTest.c" "Unit Test/Tests/DataStructuresTests/QueuesTest/HeapPriorityQueueTest/HeapPriorityQueueTest.h")
204204

205205
if (NOT WIN32)
206206
target_link_libraries(C_DataStructures PRIVATE m)

DataStructure/Queues/HPriorityQueue.c renamed to DataStructure/Queues/Sources/HPriorityQueue.c

+95-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,97 @@
1-
#include "HPriorityQueue.h"
2-
#include "../../System/Utils.h"
3-
#include "../../Unit Test/CuTest/CuTest.h"
4-
#include "../Trees/Headers/BinaryHeap.h"
5-
#include "../Trees/Sources/BinaryHeap.c"
1+
#include "../Headers/HPriorityQueue.h"
2+
#include "../../../System/Utils.h"
3+
#include "../../../Unit Test/CuTest/CuTest.h"
4+
#include "../../Trees/Headers/BinaryHeap.h"
65

76

87

98

9+
/** This function will return the first child index,
10+
* of the passed parent index.
11+
*
12+
* Note: this function should only be called from the inside.
13+
*
14+
* @param parentIndex the parent index
15+
* @return it will return the first child index of the passed parent index
16+
*/
17+
18+
int hpQueueGetFChildIndex(int parentIndex) {
19+
return parentIndex * 2 + 1;
20+
}
21+
22+
23+
/** This function will return the second child index,
24+
* of the passed parent index.
25+
*
26+
* Note: this function should only be called from the inside.
27+
*
28+
* @param parentIndex the parent index
29+
* @return it will return the second child index of the passed parent index
30+
*/
31+
32+
int hpQueueGetSChildIndex(int parentIndex) {
33+
return parentIndex * 2 + 2;
34+
}
35+
36+
37+
/** This function will swap the passed two indices.
38+
*
39+
* @param arr the array pointer
40+
* @param fIndex the first index
41+
* @param sIndex the second index
42+
*/
43+
44+
void hpQueueSwap(void **arr, int fIndex, int sIndex) {
45+
46+
void *tempItem = arr[sIndex];
47+
arr[sIndex] = arr[fIndex];
48+
arr[fIndex] = tempItem;
49+
50+
}
51+
52+
53+
54+
/** This function will heap down the value in the passed index until it be in the right place.
55+
*
56+
* @param arr the array pointer
57+
* @param currentIndex the current index
58+
* @param length the length of the array
59+
* @param cmp the comparator function pointer
60+
*/
61+
62+
void hpQueueHeapDown(void **arr, int currentIndex, int length, int (*cmp)(const void *, const void *)) {
63+
64+
if (currentIndex >= length)
65+
return;
66+
67+
int fChildIndex = hpQueueGetFChildIndex(currentIndex);
68+
int sChildIndex = hpQueueGetSChildIndex(currentIndex);
69+
70+
fChildIndex = fChildIndex >= length ? currentIndex : fChildIndex;
71+
sChildIndex = sChildIndex >= length ? currentIndex : sChildIndex;
72+
73+
if (cmp(arr[currentIndex], arr[fChildIndex]) < 0 && cmp(arr[currentIndex], arr[sChildIndex]) < 0) {
74+
75+
int biggestChildIndex = cmp(arr[fChildIndex], arr[sChildIndex]) < 0 ? sChildIndex : fChildIndex;
76+
hpQueueSwap(arr, currentIndex, biggestChildIndex);
77+
hpQueueHeapDown(arr, biggestChildIndex, length, cmp);
78+
79+
} else if (cmp(arr[currentIndex], arr[fChildIndex]) < 0) {
80+
81+
hpQueueSwap(arr, currentIndex, fChildIndex);
82+
hpQueueHeapDown(arr, fChildIndex, length, cmp);
83+
84+
} else if (cmp(arr[currentIndex], arr[sChildIndex]) < 0) {
85+
86+
hpQueueSwap(arr, currentIndex, sChildIndex);
87+
hpQueueHeapDown(arr, sChildIndex, length, cmp);
88+
89+
}
90+
91+
}
92+
93+
94+
1095
/** This function will allocate a new heap priority queue then it will return its pointer.
1196
*
1297
* @param freeFn the free function pointer, that will be called to free the queue items
@@ -269,10 +354,13 @@ void **hpQueueToArray(HPriorityQueue *queue) {
269354
int length = hpQueueGetLength(queue);
270355

271356
while (length-- > 0) {
272-
binaryHeapSwap(arr, 0, length);
273-
binaryHeapDown(arr, 0, length, queue->heap->cmp);
357+
hpQueueSwap(arr, 0, length);
358+
hpQueueHeapDown(arr, 0, length, queue->heap->cmp);
274359
}
275360

361+
for (int i = 0; i < hpQueueGetLength(queue) / 2; i++)
362+
hpQueueSwap(arr, i, hpQueueGetLength(queue) - 1 - i);
363+
276364
return arr;
277365

278366
}

0 commit comments

Comments
 (0)