@@ -103,20 +103,17 @@ void binaryHeapDown(void **arr, int currentIndex, int length, int (*cmp)(const v
103
103
104
104
int fChildIndex = binaryHeapGetFChildIndex (currentIndex ),
105
105
sChildIndex = binaryHeapGetSChildIndex (currentIndex ),
106
- targetChildIndex = -1 ;
106
+ target = currentIndex ;
107
107
108
- if (fChildIndex < length || sChildIndex < length ) {
108
+ if (fChildIndex < length && cmp (arr [target ], arr [fChildIndex ]) < 0 )
109
+ target = fChildIndex ;
109
110
110
- if (fChildIndex < length && sChildIndex < length )
111
- targetChildIndex = cmp (arr [fChildIndex ], arr [sChildIndex ]) > 0 ? fChildIndex : sChildIndex ;
112
- else
113
- targetChildIndex = fChildIndex >= length ? sChildIndex : fChildIndex ;
111
+ if (sChildIndex < length && cmp (arr [target ], arr [sChildIndex ]) < 0 )
112
+ target = sChildIndex ;
114
113
115
- }
116
-
117
- if (targetChildIndex != -1 && cmp (arr [targetChildIndex ], arr [currentIndex ]) > 0 ) {
118
- binaryHeapSwap (arr , currentIndex , targetChildIndex );
119
- binaryHeapDown (arr , targetChildIndex , length , cmp );
114
+ if (target != currentIndex ) {
115
+ binaryHeapSwap (arr , currentIndex , target );
116
+ binaryHeapDown (arr , target , length , cmp );
120
117
}
121
118
122
119
}
@@ -260,7 +257,7 @@ void binaryHeapInsertAll(BinaryHeap *heap, void **items, int length) {
260
257
if (items [i ] == NULL ) {
261
258
#ifdef C_DATASTRUCTURES_ERRORSTESTSTRUCT_H
262
259
ERROR_TEST -> errorCode = INVALID_ARG ;
263
- return ;
260
+ return ;
264
261
#else
265
262
fprintf (stderr , INVALID_ARG_MESSAGE , "item pointer" , "binary heap data structure" );
266
263
exit (INVALID_ARG );
0 commit comments