Skip to content

p450 힙 구현에서 _percolate_up 함수 분기 #156

Description

def _percolate_up(self):
i = len(self)
parent = i // 2

while parent > 0:
	if self.items[i] < self.items[parent]:
		self.items[parent], self.items[i] = self.items[i], self.items[parent]
        i = parent
        parent = i // 2					

현재 책에서는 min heap 구현 시 위와 같이 삽입된 노드가 그 부모보다 작지 않을 때 swap은 하지 않고 계속 위로 percolate 하도록 되어있습니다.
이때 아래처럼 break을 걸어 percolate을 멈춰야 하지 않나요?

def _percolate_up(self):
i = len(self)
parent = i // 2

while parent > 0:
	if self.items[i] < self.items[parent]:
		self.items[parent], self.items[i] = self.items[i], self.items[parent]
		i = parent
		parent = i // 2					
	else:
		break

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions