ほぼChatGPTの出力です。
def make_heap(data, heap, hnum):
for i in range(hnum):
heap[i] = data[i]
k = i
while k > 0:
if heap[k] > heap[parent(k)]:
swap(heap, k, parent(k))
k = parent(k)
else:
break
def swap(heap, i, j):
tmp = heap[i]
heap[i] = heap[j]
heap[j] = tmp
def lchild(i):
return 2 * i + 1
def rchild(i):
return 2 * i + 2
def parent(i):
return (i - 1) // 2
data = [5, 13, 36, 8, 2, 2, 34, 63, 32, 1, 23, 9, 16, 24, 54]
heap = [0] * len(data)
hnum = len(data)
make_heap(data, heap, hnum)
print('整列後の配列 =>', heap)
# 木構造として整形して出力
tree = str(heap[0]) + '\n'
j = 1
try:
while heap[(2 ** j) - 1]:
tree += ', '.join(map(str, heap[(2 ** j) - 1: (2 ** (j + 1)) - 1])) + '\n'
j += 1
except:
pass
print('木構造 =>\n', tree)