aoj

ALDS1_9_A Complete Binary Tree

ALDS1_9_A Complete Binary Tree
Feb. 2, 2020, 1:52 p.m.

目次

解答

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#define MAX 100000

int parent(int i) { return i / 2; }
int left(int i) { return 2 * i; }
int right(int i) { return 2 * i + 1; }

int main(void){
    int H, i, A[MAX+1];

    cin >> H;
    for (int i=0; i<=H; i++) {
        cin >> A[i];
    }

    for (int i=0; i<=H; i++) {
        cout << "node " << i << ": key = " << A[i] << ", ";
        if (parent(i) >= 1) cout << "parent key = " << A[parent(i)] << ", ";
        if (left(i) <= H) cout << "left key = " << A[left(i)] << ", ";
        if (right(i) <= H) cout << "right key = " << A[right(i)] << ", ";
        cout << endl;
    }

    return 0;
}