--[[ @brief Reshuffles the heap if a given index is larger than one of its children.
--]]

function Heap:_HeapifyTopDown(i)
	local l = i * 2;
	local r = l + 1;
	local largest;
	if l <= #self and self.Comparator(self[l], self[i]) then
		largest = l;
	else
		largest = i;
	end
	if r <= #self and self.Comparator(self[r], self[largest]) then
		largest = r;
	end
	if largest ~= i then
		self[i], self[largest] = self[largest], self[i];
		self:_HeapifyTopDown(largest);
	end
end
