728x90
반응형
def swap(tree, index_1, index_2):
"""완전 이진 트리의 노드 index_1과 노드 index_2의 위치를 바꿔준다"""
temp = tree[index_1]
tree[index_1] = tree[index_2]
tree[index_2] = temp
def heapify(tree, index, tree_size):
"""heapify 함수"""
# 왼쪽 자식 노드의 인덱스와 오른쪽 자식 노드의 인덱스를 계산
left_child_index = 2 * index
right_child_index = 2 * index + 1
largest = index # 일단 부모 노드의 값이 가장 크다고 설정
# 왼쪽 자식 노드의 값과 비교
if 0 < left_child_index < tree_size and tree[largest] < tree[left_child_index]:
largest = left_child_index
# 오른쪽 자식 노드의 값과 비교
if 0 < right_child_index < tree_size and tree[largest] < tree[right_child_index]:
largest = right_child_index
if largest != index: # 부모 노드의 값이 자식 노드의 값보다 작으면
swap(tree, index, largest) # 부모 노드와 최댓값을 가진 자식 노드의 위치를 바꿔준다
heapify(tree, largest, tree_size) # 자리가 바뀌어 자식 노드가 된 기존의 부모 노드를대상으로 또 heapify 함수를 호출한다
def heapsort(tree):
"""힙 정렬 함수"""
tree_size = len(tree)
# 마지막 인덱스부터 처음 인덱스까지 heapify를 호출한다
for index in range(tree_size-1, 0, -1):
heapify(tree, index, tree_size)
# 마지막인덱스부터 처음 인덱스까지
for i in range(tree_size-1, 0, -1):
swap(tree, 1, i) #root노드와 마지막 인덱스를 바꿔준 후
heapify(tree, 1, i) #root노드에 heapify를 호출한다
# 테스트 코드
data_to_sort = [None, 6, 1, 4, 7, 10, 3, 8, 5, 1, 5, 7, 4, 2, 1]
heapsort(data_to_sort)
print(data_to_sort)
728x90
반응형
'자료구조' 카테고리의 다른 글
[파이썬] 힙 우선순위 데이터 추출 구현 (0) | 2023.12.15 |
---|---|
[파이썬] 힙에 데이터 삽입하기 (0) | 2023.12.15 |
[파이썬] heapify 함수 구현 (1) | 2023.12.15 |
[파이썬] in-order 순회 구현하기 (1) | 2023.12.15 |
[파이썬] 완전 이진 트리 직접 구현하기 (1) | 2023.12.12 |