project2/p2q2-ucs.rb
# Sift down the element at index i
Sift down the element at index i
Sift down the element at index i
def sift_down(i)
    child = (i * 2) + 1
    return if child >= @heap.length
    child += 1 if child + 1 < @heap.length and @heap[child][1] > @heap[child+1][1]
    if @heap[i][1] > @heap[child][1]
      @heap[child], @heap[i] = @heap[i], @heap[child]
      sift_down(child)
    end
  end
def sift_down ( i ) child = ( i * 2 ) + 1 return if child >= @heap . length child += 1 if child + 1 < @heap . length and @heap [ child ] [ 1 ] > @heap [ child + 1 ] [ 1 ] if @heap [ i ] [ 1 ] > @heap [ child ] [ 1 ] @heap [ child ] , @heap [ i ] = @heap [ i ] , @heap [ child ] sift_down ( child ) end end
Sift down the element at index i
Sift down the element at index i
i