tinypile icon indicating copy to clipboard operation
tinypile copied to clipboard

jps.hh: redundant perculateUp call?

Open emily785 opened this issue 2 years ago • 2 comments

inline void _fixIdx(SizeT i) { _percolateDown(i); _percolateUp(i); }

_percolateDown already calls _percolateUp, so the standalone call seems redundant.

emily785 avatar Apr 27 '23 15:04 emily785

Yes, but the index i may change in _percolateDown() when traversing children. Depending on the new value a node goes either up or down the heap, and since that isn't tracked we need to try both directions.

EDIT: Better explained than i can right now: https://en.wikipedia.org/wiki/Binary_heap#Decrease_or_increase_key

fgenesis avatar Apr 27 '23 17:04 fgenesis

EDIT: Got it. Thank you for taking the time to answer.

emily785 avatar Apr 27 '23 17:04 emily785