최대 힙
def enq(v):
global last
last += 1
arr[last] = v
c = last
p = c // 2
while p >= 1 and arr[p] < arr[c]:
arr[p], arr[c] = arr[c], arr[p]
c = p
p = c // 2
return
def deq():
global last
tmp = arr[1]
arr[1] = arr[last]
last -= 1
p = 1
c = p * 2
while c <= last:
if c + 1 <= last and arr[c] < arr[c + 1]:
c += 1
if arr[p] < arr[c]:
arr[p], arr[c] = arr[c], arr[p]
p = c
c = p * 2
else:
break
return tmp
arr = [0] * 101
last = 0
enq(3)
enq(2)
enq(4)
enq(7)
enq(5)
enq(1)
enq(9)
print(arr)
최소 힙
arr = [0] * 10
def enq(v):
global last
last += 1
arr[last] = v
c = last
p = c // 2
while p >= 1 and arr[p] > arr[c]:
arr[p], arr[c] = arr[c], arr[p]
c = p
p = c // 2
def deq():
global last
tmp = arr[1]
arr[1] = arr[last]
last -= 1
p = 1
c = p * 2
while c <= last:
if c + 1 <= last and arr[c] > arr[c + 1]:
c += 1
if arr[p] > arr[c]:
arr[p], arr[c] = arr[c], arr[p]
p = c
c = p * 2
else:
break
return tmp
last = 0
enq(3)
enq(6)
enq(5)
enq(8)
enq(1)
enq(2)
enq(4)
deq()
print(arr)