널리 잘 알려진 자료구조 중 최대 힙이 있다. 최대 힙을 이용하여 다음과 같은 연산을 지원하는 프로그램을 작성하시오.
1) 배열에 자연수 x를 넣는다.
2) 배열에서 가장 큰 값을 출력하고, 그 값을 배열에서 제거한다.
3) 프로그램은 처음에 비어있는 배열에서 시작하게 된다.
첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0이라면 배열에서 가장 큰 값을 출력하고 그 값을 배열에서 제거하는 경우이다. 입력되는 자연수는 231보다 작다.
입력에서 0이 주어진 횟수만큼 답을 출력한다. 만약 배열이 비어 있는 경우인데 가장 큰 값을 출력하라고 한 경우에는 0을 출력하면 된다.
13
0
1
2
0
0
3
2
1
0
0
0
0
0
0
2
1
3
2
1
0
0
1: [10] / \ 1: 2: 3: 2:[5] 3:[3] => heap = [0, 10, 5, 3 ]
parent_node_idx = (current_node_idx //2)
이다.heap =[0]
def exchange_with_parent(heap, current_idx):
if current_idx<=1:
return
parent_idx = current_idx//2
current_val = heap[current_idx]
parent_val = heap[parent_idx]
if current_val > parent_val:
heap[current_idx], heap[parent_idx] = heap[parent_idx], heap[current_idx]
exchange_with_parent(heap, parent_idx)
def insert(n):
heap.append(n)
current_idx = len(heap)-1
exchange_with_parent(heap, current_idx)
for i in [1,3,5,8,7,32,2,1,3]:
insert(i)
print(heap)
heap = [0, 32, 7, 8, 3, 5, 3, 2, 1, 1]
def exchange_with_child(heap, current_idx):
children_idx = [current_idx*2, current_idx *2+1]
for child_idx in children_idx:
if len(heap) < child_idx+1:
return
if heap[current_idx]< heap[child_idx]:
heap[current_idx], heap[child_idx] = heap[child_idx], heap[current_idx]
print(heap)
exchange_with_child(heap, child_idx)
def pop_out():
heap[1] = heap.pop()
## 여기서 run_exchange
current_idx = 1
exchange_with_child(heap , current_idx)
pop_out()```