algoalgo-world
algoalgo-world/tree/build-heap
tree/build-heap

힙 만들기

종류
최대 힙. 완전 이진 트리
지키는 것
부모가 자식보다 크다는 것뿐. 좌우에는 뜻이 없다
시간
O(n)
공간
O(1)
끝난 높이
log2 n
노드마다 더 드는 것
없다

이미 서 있는 트리에서 시작한다. 같은 힙을 하나씩 넣어 만들면 n log n 이고 이쪽은 n 이다

언어를 고르면 코드가 열린다

def heapify(a, end, root):
    biggest = root
    left = 2 * root + 1
    right = 2 * root + 2
    if left < end and a[left] > a[biggest]:
        biggest = left
    if right < end and a[right] > a[biggest]:
        biggest = right
    if biggest != root:
        a[root], a[biggest] = a[biggest], a[root]
        heapify(a, end, biggest)


def build_heap(a):
    for root in range(len(a) // 2 - 1, -1, -1):
        heapify(a, len(a), root)
function heapify(a, end, root) {
  let biggest = root;
  const left = 2 * root + 1;
  const right = 2 * root + 2;
  if (left < end && a[left] > a[biggest]) biggest = left;
  if (right < end && a[right] > a[biggest]) biggest = right;
  if (biggest === root) return;
  [a[root], a[biggest]] = [a[biggest], a[root]];
  heapify(a, end, biggest);
}

function buildHeap(a) {
  for (let root = (a.length >> 1) - 1; root >= 0; root--) {
    heapify(a, a.length, root);
  }
}
static void heapify(int a[], int end, int root) {
    int biggest = root;
    int left = 2 * root + 1;
    int right = 2 * root + 2;
    if (left < end && a[left] > a[biggest]) biggest = left;
    if (right < end && a[right] > a[biggest]) biggest = right;
    if (biggest == root) return;
    int t = a[root];
    a[root] = a[biggest];
    a[biggest] = t;
    heapify(a, end, biggest);
}

void build_heap(int a[], int n) {
    for (int root = n / 2 - 1; root >= 0; root--) heapify(a, n, root);
}
void heapify(std::vector<int>& a, int end, int root) {
    int biggest = root;
    int left = 2 * root + 1;
    int right = 2 * root + 2;
    if (left < end && a[left] > a[biggest]) biggest = left;
    if (right < end && a[right] > a[biggest]) biggest = right;
    if (biggest == root) return;
    std::swap(a[root], a[biggest]);
    heapify(a, end, biggest);
}

void build_heap(std::vector<int>& a) {
    int n = static_cast<int>(a.size());
    for (int root = n / 2 - 1; root >= 0; root--) heapify(a, n, root);
}
static void Heapify(int[] a, int end, int root) {
    int biggest = root;
    int left = 2 * root + 1;
    int right = 2 * root + 2;
    if (left < end && a[left] > a[biggest]) biggest = left;
    if (right < end && a[right] > a[biggest]) biggest = right;
    if (biggest == root) return;
    (a[root], a[biggest]) = (a[biggest], a[root]);
    Heapify(a, end, biggest);
}

static void BuildHeap(int[] a) {
    for (int root = a.Length / 2 - 1; root >= 0; root--) {
        Heapify(a, a.Length, root);
    }
}
static void heapify(int[] a, int end, int root) {
    int biggest = root;
    int left = 2 * root + 1;
    int right = 2 * root + 2;
    if (left < end && a[left] > a[biggest]) biggest = left;
    if (right < end && a[right] > a[biggest]) biggest = right;
    if (biggest == root) return;
    int t = a[root];
    a[root] = a[biggest];
    a[biggest] = t;
    heapify(a, end, biggest);
}

static void buildHeap(int[] a) {
    for (int root = a.length / 2 - 1; root >= 0; root--) {
        heapify(a, a.length, root);
    }
}
돌려 보고 코드도 본다