inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

Python 응용 - 파이썬으로 배우는 자료구조와 알고리즘

heap 생성, 힙 아이템 삭제 및 수정, 힙의 최대, 최소 값 구하기

showHeap 파일 소스코드좀 부탁해요

842

oncore

작성한 질문수 1

0

강의가 중간에 끊켰는지 ? 갑자기 아무설명도 없다가 showHeap이 나와서 강의 진행하는데 소스 코드좀 부탁드립니다

python

답변 15

0

MyungHak

'''

피이썬 3.x 버젼과 2.x버젼의 차이가 있으니 유의해주시기 바랍니다.

이 코드는 이전 강의에서 댓글로 써주신 분 것을 가져온거입니다

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

output = StringIO() # for 3.x

#output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

'''

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

0

MyungHak

파이썬 2.x버젼과 3.x버젼이 차이가 있으니 주의해 주세요

그리고 이 코드는 이 전 강의에서 댓글로 소스코드를 올려주신 분 것 입니다.

"""

import math

from io import StringIO, BytesIO

3.x 는 String IO,

2.x는 BytesIO 사용바람

def show_tree(tree, total_width=36, fill= ' '):

Pretty-print a tree

#output = StringIO() # for 3.x

output = BytesIO() # for 2.x

last_row = -1

for i, n in enumerate(tree):

if i:

row = int(math.floor(math.log(i+1, 2))) # 지수, 밑

# i =0부터 시작돼서 i+1, 이진트리기떄문에 밑은 2

else:

row =0

if row!= last_row:

output.write('\n')

columns = 2**row

col_width = int(math.floor((total_width * 1.0) / columns ))

output.write(str(n).center(col_width, fill))

last_row = row

print(output.getvalue())

print('-'*total_width)

print()

return

"""

코딩팡 Pro 등록방법 문의

0

17

1

'자동으로 저장할 수 없습니다. 이 파일은 원격으로 또는 다른 탭에서 업데이트되었습니다.'

0

16

2

수업자료

1

18

1

random_state에 따른 최종스코어 점수차이

0

23

2

화면이 보이지 않습니다.

0

19

1

강사님 강의 자료와 코드 부탁드립니다.

0

19

1

주피터 노트북 실행 토큰

0

12

0

코랩 백엔드

0

33

3

전체 과정에 대한 질문입니다.

1

31

1

xgboost 사용 시 오류 이유

0

30

2

강사님 강의 더 주세요

1

33

1

작업형 2 제출하는 법

0

50

2

Section 5. 문자열 슬라이싱, 파생변수, 평균값 질문 드립니다.

0

36

1

예시문제 작업형2(ver. 2025)

0

39

2

수강 연장 문의드립니다.

0

40

2

강사님 강의 잘듣고있습니다! 오류관련 질문 드립니다!

1

42

2

예시문제 작업형2(ver. 2025)

0

46

2

작업형2 모의문제 3 질문

0

52

1

오랜만에 인사드립니다 강사님

1

43

1

강의신청 후

0

47

1

안녕하세요 강사님 문의사항이 있습니다

0

44

1

수강 연장 문의

0

89

4

bisect와 힙의 속도차이

0

389

0

감사합니다.

0

294

0