container/heap
힙(heap)은 최댓값/최솟값을 가장 빠르게 찾아내려고 고안한 자료구조로, 최대 힙과 최소 힙이 있다. 부모 노드의 값이 자식 노드의 값보다 항상 큰 힙을 ‘최대 힙’이라 하고, 부모 노드의 값이 자식 노드의 값보다 항상 작은 힙을 ‘최소 힙’이라 한다. 최소 힙은 첫 번째 요소가 항상 최솟값으로 유지된다.
container/heap 패키지는 힙 자료구조에 필요한 기능을 제공한다. heap.Interface에 정의된 메서드를 가진 타입은 힙으로 사용할 수 있다.
▼ heap.Interface 타입
type Interface interface { sort.Interface Push(x interface{}) Pop() interface{} }
heap.Interface에는 sort.Interface가 있고, 요소를 추가/제거하기 위한 Push()와 Pop() 메서드가 정의되어 있다.
▼ sort.Interface 타입
type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) }