Menu
Coddy logo textTech

parent

Lektion 4 von 14 im Kurs Heaps & Priority Queues - Datenstrukturen-Serie #7 von Coddy.

challenge icon

Aufgabe

Einfach

Fügen Sie der Klasse MinHeap eine Methode parent hinzu.

Sie erhält eine Ganzzahl i (einen Index im Heap) und gibt den Index seines Elternelements zurück: (i - 1) / 2 unter Verwendung von Ganzzahldivision.

Probier es selbst

#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;
}

Alle Lektionen in Heaps & Priority Queues - Datenstrukturen-Serie #7