Part4.10_자료구조(스택,큐,해쉬,힙)_최소힙__이진트리

Eugenius1st·2022년 1월 19일
0

Python_algorithm

목록 보기
27/83

최소힙

내가 생각한 코드

import sys
sys.stdin = open("input.txt", "rt")
list = []
n = 0
while n!=-1:
    n = int(input())
    list.append(n)
    list.sort()

    if(list[0] == -1):
        break
    elif(list[0] != -1):
        if(list[0] == 0):
            list.pop(0)
            if(len(list) == 1):
                list.pop(0)
            if(len(list) != 1 and list[0] == max(list)):
                break
            else:
                print(list[0])
                list.pop(0)            

이진트리 방식


Push 하고 up 하는 방식이다.

import sys
import heapq as hq #힙큐 받는 것!!
sys.stdin = open("input.txt", "rt")

a = []
while True:
    n=int(input())
    if n == -1:
        break
    if n==0:
        if len(a)==0:
            print(-1)
        else:
            print(hq.heappop(a)) # a에서 자료를 하나 끄집어 내고, 그것이 루트 노드 값이다.
    else:
        hq.heappush(a,n) # a라는 리스트에 n값을 push한다.

최소 힙 순서

profile
최강 프론트엔드 개발자가 되고싶은 안유진 입니다

0개의 댓글