parent
Coddy의 힙 & 우선순위 큐 - 자료구조 시리즈 #7 코스 레슨 — 14개 중 4번째.
챌린지
쉬움MinHeap 클래스에 parent 메서드를 추가하세요.
이 메서드는 정수 i(힙의 인덱스)를 인자로 받아, 정수 나눗셈을 사용하여 부모의 인덱스인 (i - 1) / 2를 반환합니다.
직접 해보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "minheap.h"
int main() {
MinHeap h;
MinHeap_init(&h);
char line[1024];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "parent") == 0) { char* arg = strtok(NULL, " \t"); if (arg) printf("%d\n", MinHeap_parent(&h, atoi(arg))); }
}
return 0;
}