Problem:
Complete Binary Tree A complete binary tree is a binary tree in which every internal node has two children and all leaves have the same depth. A binary tree in which if last level is not completely filled but all nodes (leaves) are pushed across to the left, is also (nearly) a complete binary tree. A binary heap data structure is an array that can be viewed as a nearly complete binary tree as shown in the following figure.   Each node of a nearly complete binary tree corresponds to an element of the array that stores the value in the node. An array $A$ that represents a binary heap has the heap size $H$, the number of elements in the heap, and each element of the binary heap is stored into $A[1...H]$ respectively. The root of the tree is $A[1]$, and given the index $i$ of a node, the indices of its parent $parent(i)$, left child $left(i)$, right child $right(i)$ can be computed simply by $\lfloor i / 2 \rfloor$, $2 \times i$ and $2 \times i + 1$ respectively. Write a program which reads a binary heap represented by a nearly complete binary tree, and prints properties of nodes of the binary heap in the following format: node $id$: key = $k$, parent key = $pk$, left key = $lk$, right key = $rk$, $id$, $k$, $pk$, $lk$ and $rk$ represent id (index) of the node, value of the node, value of its parent, value of its left child and value of its right child respectively. Print these properties in this order. If there are no appropriate nodes, print nothing. Input In the first line, an integer $H$, the size of the binary heap, is given. In the second line, $H$ integers which correspond to values assigned to nodes of the binary heap are given in order of node id (from $1$ to $H$). Output Print the properties of the binary heap in the above format from node $1$ to $H$ in order. Note that, the last character of each line is a single space character.  Constraints  $H \leq 250$  $-2,000,000,000 \leq$ value of a node $\leq 2,000,000,000$  Sample Input 1 5
7 8 1 2 3 Sample Output 1 node 1: key = 7, left key = 8, right key = 1, 
node 2: key = 8, parent key = 7, left key = 2, right key = 3, 
node 3: key = 1, parent key = 7, 
node 4: key = 2, parent key = 8, 
node 5: key = 3, parent key = 8, Reference Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution (Python):
def main():
    H = int(input())
    A = list(map(int,input().split()))
    for i in range(1, H+1, 1):
        key = A[i-1]
        if i != 1:
            parent_key = A[i//2 - 1]
        else:
            parent_key = None

        if (2*i) <= H:
            left_key = A[2*i-1]
        else:
            left_key = None

        if (2*i + 1) <= H:
            right_key = A[2*i]
        else:
            right_key = None

        if (parent_key is not None) and (left_key is not None) and (right_key is not None):
            print(f'node {i}: key = {A[i-1]}, parent key = {parent_key}, left key = {left_key}, right key = {right_key}, ')
        elif(parent_key is None) and (left_key is not None) and (right_key is not None):
            print(f'node {i}: key = {A[i-1]}, left key = {left_key}, right key = {right_key}, ')
        elif (parent_key is not None) and (left_key is not None) and (right_key is None):
            print(f'node {i}: key = {A[i-1]}, parent key = {parent_key}, left key = {left_key}, ')
        elif (parent_key is not None) and (left_key is None) and (right_key is None):
            print(f'node {i}: key = {A[i-1]}, parent key = {parent_key}, ')
        elif (parent_key is None) and (left_key is not None) and (right_key is None):
            print(f'node {i}: key = {A[i-1]}, left key = {left_key}, ')

main()

Solution (Python):
import sys
input = sys.stdin.readline

def parent(i):
    return i//2

def left(i):
    return 2*i

def right(i):
    return 2*i+1

H = int(input())


A = [None] + list(map(int, input().split()))


for i in range(1, H+1):
    print('node ' + str(i) + ': ', end='')
    print('key = ' + str(A[i]) + ', ', end='')
    if 1 <= parent(i):
        print('parent key = ' + str(A[parent(i)]) + ', ', end='')
    if left(i) <= H:
        print('left key = ' + str(A[left(i)]) + ', ', end='')
    if right(i) <= H:
        print('right key = ' + str(A[right(i)]) + ', ', end='')
    print('')

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')

parent = lambda i: math.floor(i / 2)
left = lambda i: 2 * i
right = lambda i: 2 * i + 1


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    H = ii()
    heap = il()

    for i in range(H):
        string = 'node {0}: key = {1}, '.format(i + 1, heap[i])
        i += 1
        if parent(i) >= 1: string += 'parent key = {0}, '.format(heap[parent(i) - 1])
        if left(i) <= H: string += 'left key = {0}, '.format(heap[left(i) - 1])
        if right(i) <= H: string += 'right key = {0}, '.format(heap[right(i) - 1])
        print(string)


if __name__ == '__main__':
    main()

Solution (Python):
class CompleteBinaryTree:
    def __init__(self, nodes):
        self.size = len(nodes)
        self.nodes = [None] + nodes
        
    
    def print_nodes(self):
        for i in range(1,self.size + 1):
            if i == 1:
                print('node {0}: key = {1}, left key = {2}, right key = {3}, '.format(i, self.nodes[i], self.nodes[2*i], self.nodes[2*i+1]))
            elif 2*i < self.size:
                print('node {0}: key = {1}, parent key = {2}, left key = {3}, right key = {4}, '.format(i, self.nodes[i], self.nodes[i//2], self.nodes[2*i], self.nodes[2*i+1]))
            elif 2*i == self.size:             
                print('node {0}: key = {1}, parent key = {2}, left key = {3}, '.format(i, self.nodes[i], self.nodes[i//2], self.nodes[2*i]))
            else:
                print('node {0}: key = {1}, parent key = {2}, '.format(i, self.nodes[i], self.nodes[i//2]))


if __name__ == '__main__':
    n = int(input())
    nodes = list(map(int, input().rstrip().split()))
    cbt = CompleteBinaryTree(nodes)
    cbt.print_nodes()

Solution (Python):
import sys
input = sys.stdin.readline


def main():
    H = int(input().strip())
    A = [int(i) for i in input().strip().split()]

    for i in range(H):
        print("node {}: key = {}, ".format(i + 1, A[i]), end="")
        if i > 0:
            print("parent key = {}, ".format(A[(i - 1) // 2]), end="")
        if (i * 2) + 1 < H:
            print("left key = {}, ".format(A[(i * 2) + 1]), end="")
        if (i * 2) + 2 < H:
            print("right key = {}, ".format(A[(i * 2) + 2]), end="")
        print()

    return


if __name__ == "__main__":
    main()

Solution (Python):
h = int(input())
H = list(map(int,input().split()))
for i in range(h):
    node = i+1
    print(f"node {node}: ",end = "")
    key = H[i]
    print(f"key = {key}, ",end = "")
    if 1 <= node//2 <= h:
        parent_key = H[node//2-1]
        print(f"parent key = {parent_key}, ",end = "")
    if 1 <= node*2 <= h:
        left_key = H[node*2-1]
        print(f"left key = {left_key}, ",end = "")
    if 1 <= node*2+1 <= h:
        right_key = H[node*2]
        print(f"right key = {right_key}, ",end = "")
    print()

Solution (Python):
N = int(input())
heap = list(map(int, input().split()))

for i, a in enumerate(heap, start=1):  
    print(f'node {i}: key = {a}, ', end='')
    
    if i//2 != 0:
        print(f'parent key = {heap[i//2 -1 ]}, ', end='')
        
    if 2 * i <= N:
        print(f'left key = {heap[2*i - 1]}, ', end='')

    if 2 * i + 1 <= N:
        print(f'right key = {heap[2*i]}, ', end='')
    print("")

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
for i in range(n):
    id = i + 1
    k = h[i]
    p = id // 2
    pk = h[p-1] if p > 0 else 0
    left = id * 2
    lk = h[left-1] if left <= n else 0
    right = id * 2 + 1
    rk = h[right-1] if right <= n else 0
    print('node {0}: key = {1}, '.format(id, k), end='')
    if p > 0:
        print('parent key = {0}, '.format(pk), end='')
    if left <= n:
        print('left key = {0}, '.format(lk), end='')
    if right <= n:
        print('right key = {0}, '.format(rk), end='')
    print()

Solution (Python):
n=int(input())
nums=list(map(int,input().split()))
tree={}

def show(tree,i,number):
    if tree[number][2]==None:
        if tree[number][1]==None:
            if tree[number][0]==None:
                print("node "+str(i+1)+": key = "+str(number)+", ")
            else:
                print("node "+str(i+1)+": key = "+str(number)+", left key = "+str(tree[number][0])+", ")
        else:
            print("node "+str(i+1)+": key = "+str(number)+", left key = "+str(tree[number][0])+", right key = "+str(tree[number][1])+", ")
    else:
        if tree[number][1]==None:
            if tree[number][0]==None:
                print("node "+str(i+1)+": key = "+str(number)+", parent key = "+str(tree[number][2])+", ")
            else:
                print("node "+str(i+1)+": key = "+str(number)+", parent key = "+str(tree[number][2])+", left key = "+str(tree[number][0])+", ")
        else:
            print("node "+str(i+1)+": key = "+str(number)+", parent key = "+str(tree[number][2])+", left key = "+str(tree[number][0])+", right key = "+str(tree[number][1])+", ")

for i in range(n):
    number=nums[i]
    tree[number]=[None,None,None]
    if i==0:
        if i+2<=n:
            tree[number]=[nums[i+1],nums[i+2],None]
        elif i+1<=n:
            tree[number]=[nums[i+1],None,None]
        show(tree,i,number)
    else:
        tree[number][2]=nums[int((i+1)/2)-1]
        if 2*(i+1)<n+1:
            if 2*(i+1)+1<n+1:
                tree[number][0]=nums[2*(i+1)-1]
                tree[number][1]=nums[2*(i+1)]
            else:
                tree[number][0]=nums[2*(i+1)-1]
        show(tree,i,number)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

for i,key in enumerate(A):
    i = i+1
    parent = A[int(i/2)-1] if int(i/2)>0 else None
    left = A[2*i-1] if 2*i<=n  else None
    right = A[2*i+1-1] if 2*i+1<=n else None 
    ans = 'node {}: key = {}, '.format(i,key)
    if parent:
        ans += 'parent key = {}, '.format(parent)
    if left:
        ans += 'left key = {}, '.format(left)
    if right:
        ans += 'right key = {}, '.format(right)
    print(ans)

Solution (Python):
N = int(input())
heap = list(map(int, input().split()))

for i, a in enumerate(heap, start=1):  
    print(f'node {i}: key = {a}, ', end='')
    
    if i//2 != 0:
        
        print(f'parent key = {heap[i//2 -1 ]}, ', end='')
    
    if 2 * i <= N:
        print(f'left key = {heap[2*i - 1]}, ', end='')
    
    if 2 * i + 1 <= N:
        print(f'right key = {heap[2*i]}, ', end='')
    print()

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    H = I()
    A = [0]
    tmp = LI()
    for i in tmp:
        A.append(i)

    for i in range(1, H+1):
        ans = ''
        ans += 'node ' + str(i) + ': '
        ans += 'key = ' + str(A[i]) + ', '
        if 1 <= i//2 < H+1:
            ans += 'parent key = ' + str(A[i//2]) + ', '
        if 1 <= 2*i < H+1:
            ans += 'left key = ' + str(A[2*i]) + ', '
        if 1 <= 2*i+1 < H+1:
            ans += 'right key = ' + str(A[2*i+1]) + ', '
        print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())

def parent(i):
    i += 1
    return int(i/2) -1

def left(i):
    i += 1
    return 2*i -1

def right(i):
    i += 1
    return 2*i + 1 -1

A = [int(i) for i in input().split()]

print('node {}: key = {}, left key = {}, right key = {}, '.format(1, A[0], A[left(0)], A[right(0)]))

for i in range(1, len(A)):
    output = 'node {}: key = {}, parent key = {}, '.format(i+1, A[i], A[parent(i)])
    left_i = left(i)
    if(left_i < len(A)):
        output += 'left key = {}, '.format(A[left_i])
    right_i = right(i)
    if(right(i) < len(A)):
        output += 'right key = {}, '.format(A[right_i])
        
    print(output)

Solution (Python):
n = int(input())
Arr = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
    print(f'node {i}: key = {Arr[i]}, ', end = '')
    if i > 1:
        print(f'parent key = {Arr[i//2]}, ', end = '')
    if i*2 <= n:
        print(F'left key = {Arr[i*2]}, ', end = '')
    if i*2 + 1 <= n:
        print(f'right key = {Arr[i*2 + 1]}, ', end = '')
    print()

Solution (Python):
def main():
    h = int(input())
    a = list(map(int, input().split()))

    for i, j in enumerate(a, start=1):
        print('node {}: key = {}, '.format(i, j), end='')
        
        if i // 2 >= 1:
            print('parent key = {}, '.format(a[i//2 -1]), end='')
        
        if i * 2 <= h:
            print('left key = {}, '.format(a[2*i - 1]), end='')
        if i * 2 + 1 <= h:
            print('right key = {}, '.format(a[2*i]), end='')
        print()
        
if __name__ == '__main__':
    main()

Solution (Python):
import math

def readinput():
    n=int(input())
    h=list(map(int,input().split()))
    return n,h

def printParent(i,h):
    parent=math.floor(i/2)
    if(parent>0):
        print('parent key = {}, '.format(h[parent]),end='')

def printLeft(i,h):
    left=2*i
    if(left<=len(h[1:])):
        print('left key = {}, '.format(h[left]),end='')

def printRight(i,h):
    right=2*i+1
    if(right<=len(h[1:])):
        print('right key = {}, '.format(h[right]),end='')

def main(n,h):
    h.insert(0,-1)
    for i in range(1,n+1):
        print('node {}: key = {}, '.format(i, h[i]),end='')
        printParent(i,h)
        printLeft(i,h)
        printRight(i,h)
        print('')

    return

if __name__=='__main__':
    n,h=readinput()
    main(n,h)

Solution (Python):
MAX = 10**5


def parent(i):
    return i // 2

def left(i):
    return 2 * i

def right(i):
    return 2 * i + 1


H = int(input())
A = list(map(int, input().split()))
A.append(10*12)


for i in range(1,H+1):
    print("node %d: key = %d, " %(i, A[i-1]), end = "")
    if parent(i) >= 1:
        print("parent key = %d, " %(A[parent(i)-1]), end = "")

    if left(i) <= H:
        print("left key = %d, " %(A[left(i)-1]), end = "")

    if right(i) <= H:
        print("right key = %d, " %(A[right(i)-1]), end = "")
    print()

Solution (Python):
n = int(input())

A = [""]

A += [int(i) for i in input().split()]



for i in range(1, len(A)):
    print("node {}: key = {},".format(i, A[i]), end = " ")

    if i != 1:
        print("parent key = {},".format(A[i//2]), end = " ")
    
    if 2 * i + 1 < len(A):
        print("left key = {}, right key = {},".format(A[2 * i], A[2 * i + 1]), end = " ")

    elif 2 * i + 1 == len(A):
        print("left key = {},".format(A[2 * i]), end = " ")

    print("")
   
"""
5 
7 8 1 2 3
"""

Solution (Python):
n = int(input())
cbt = input().split()
cbt.insert(0,0)

for i in range(1,len(cbt)):
    parent_index = int(i/2)
    left_index = 2*i
    right_index = 2*i +1
    if i == 1:
        print('node {}: key = {}, left key = {}, right key = {}, '.format(
            i,cbt[i],cbt[i+1],cbt[i+2]))
    else:
        if len(cbt) <= left_index:
            print('node {}: key = {}, parent key = {}, '.format(i,cbt[i],cbt[parent_index]))
        elif len(cbt) <= right_index:
            print('node {}: key = {}, parent key = {}, left key = {}, '.format(
                i,cbt[i],cbt[parent_index],cbt[left_index]))
        else:
            print('node {}: key = {}, parent key = {}, left key = {}, right key = {}, '.format(
                i,cbt[i],cbt[parent_index],cbt[left_index],cbt[right_index]))

Solution (Python):
H = int(input())
A = [None]
A.extend(list(map(int,input().split())))
for i in range(1, H+1):
    print(f"node {i}: key = {A[i]}, " 
        + (f"parent key = {A[int(i/2)]}, " if i > 1 else "")
        + (f"left key = {A[i*2]}, " if i*2 <= H else "")
        + (f"right key = {A[i*2+1]}, " if i*2+1 <= H else "")
    )

Solution (Python):



H = int(input()) 

nodes = list(map(int, input().split())) 

for i in range(H):
    parent = (i+1) // 2 - 1
    left = 2 * (i+1) - 1
    right = 2 * (i+1)

    print('node {}: key = {}, '.format(i+1, nodes[i]), end='')
    if i != 0:
        print('parent key = {}, '.format(nodes[parent]), end='')
    if left < H:
        print('left key = {}, '.format(nodes[left]), end='')
    if right < H:
        print('right key = {}, '.format(nodes[right]), end='')

    print('')

Solution (Python):
from sys import stdin
N=int(input())
n=list(map(int,(stdin.readline().strip().split())))
n.insert(0,None)
for i,j in enumerate(n):
    if i==0:continue
    left=i*2
    right=i*2+1
    if i==1:
        if left<=N and right<=N:
            print("node %d: key = %d, left key = %d, right key = %d, " % (i,j,n[left],n[right]))
        elif left<=N:
            print("node %d: key = %d, left key = %d, " % (i,j,n[left]))
        elif right<=N:
            print("node %d: key = %d, right key = %d, " % (i,j,n[right]))
        else:
            print("node %d: key = %d, " % (i,j))
    else:
        parent=i//2
        if left<=N and right<=N:
            print("node %d: key = %d, parent key = %d, left key = %d, right key = %d, " % (i,j,n[parent],n[left],n[right]))
        elif left<=N:
            print("node %d: key = %d, parent key = %d, left key = %d, " % (i,j,n[parent],n[left]))
        elif right<=N:
            print("node %d: key = %d, parent key = %d, right key = %d, " % (i,j,n[parent],n[right]))
        else:
            print("node %d: key = %d, parent key = %d, " % (i,j,n[parent]))

Solution (Python):
h = int(input())
K = list(map(int, input().split()))

K.insert(0, 0) 

for i in range(1, h + 1):
    s = f'node {i}: key = {K[i]}, '

    parent = i // 2
    left = 2 * i
    right = 2 * i + 1

    if parent >= 1:
        s += f'parent key = {K[parent]}, '
    if left <= h:
        s += f'left key = {K[left]}, '
    if right <= h:
        s += f'right key = {K[right]}, '

    print(s)

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
A.insert(0,-1)

for i in range(1,n+1):
    print("node {}: ".format(i),end = "")

    print("key = {}, ".format(A[i]),end = "")

    if (i // 2) > 0:
        print("parent key = {}, ".format(A[i // 2]),end = "")
    
    if 2*i <= n:
        print("left key = {}, ".format(A[2*i]),end = "")
    
    if 2*i + 1 <= n:
        print("right key = {}, ".format(A[2*i + 1]),end = "")
    
    print("")

Solution (Python):
N = int(input())
H = [int(h) for h in input().split()]

for i in range(1, N + 1):
    print('node {}: '.format(i), end='')
    print('key = {}, '.format(H[i - 1]), end='')
    if i // 2 > 0:
        print('parent key = {}, '.format(H[i // 2 - 1]), end='')
    if i * 2 <= N:
        print('left key = {}, '.format(H[i * 2 - 1]), end='')
    if i * 2 + 1 <= N:
        print('right key = {}, '.format(H[i * 2 + 1 - 1]), end='')
    print()

Solution (Python):
def parent(i):
    if i / 2 <= 0:
        return 0
    else:
        return int(i/2)
    
def left_child(i):
    if 2*i >= len(L):
        return 0
    else :
        return int(2*i)

def right_child(i):
    if 2*i+1 >= len(L):
        return 0
    else :
        return int(2*i + 1)
    
def make_print(l_idx,p,l_c,r_c):
    print_str='node '+str(l_idx+1)+': key = '+str(L[l_idx+1])
    if p != 0:
        print_str+=', parent key = '+str(L[p])
    if l_c != 0:
        print_str+=', left key = '+str(L[l_c])
    if r_c != 0:
        print_str+=', right key = '+str(L[r_c])
        
    print_str+=', '
    print(print_str)
    
N=int(input())
L=list(map(int,input().split()))
L=[0]+L
    
for l_idx,l in enumerate(L[1:]):


    p = parent(l_idx+1)
    l_c = left_child(l_idx+1)
    r_c = right_child(l_idx+1)

    make_print(l_idx,p,l_c,r_c)

Solution (Python):

def main():
    n = int(input())+1
    a = [-1]+list(map(int,input().split()))
    for i in range(1,n):
        print ('node %d: key = %d, '%(i,a[i]),end = '')
        if i!=1:print ('parent key = %d, '%a[i//2],end='')
        if i*2<n:print ('left key = %d, '%a[i*2],end='')
        if i*2+1<n:print ('right key = %d, '%a[i*2+1],end='')
        print ()


if __name__ == '__main__':
    main()

Solution (Python):
H = int(input())
A = [0]
for i in input().split(): A.append(i)
for i in range(1,H+1):
    print("node {}: key = {}, ".format(i,A[i]),end = "")
    if i != 1: print("parent key = {}, ".format(A[i//2]),end = "")
    if 2*i <= H: print("left key = {}, ".format(A[2*i]),end = "")
    if 2*i+1 <= H: print("right key = {}, ".format(A[2*i+1]),end="")
    print("")

Solution (Python):


H = int(input())
T = [-1] + [int(i) for i in input().split()]

for i in range(1, H+1):
    print("node ", i, ": ", sep="", end="")
    print("key = ", T[i], ", ", sep="", end="")
    if i != 1:
        print("parent key = ", T[i // 2], ", ", sep="", end="")
    if 2 * i <= H:
        print("left key = ", T[2 * i], ", ", sep="", end="")
    if 2 * i + 1 <= H:
        print("right key = ", T[2 * i + 1], ", ", sep="", end="")
    print()

Solution (Python):
H = int(input())
*A, = map(int, input().split())
A = [0] + A

for i, a in enumerate(A):
    if i == 0:
        continue
    print('node %d: ' % i, end='')
    print('key = %d, ' % A[i], end='')
    if i//2 != 0:
        print('parent key = %d, ' % A[i//2], end='')
    if i*2 <= H:
        print('left key = %d, ' % A[i*2], end='')
    if i*2+1 <= H:
        print('right key = %d, ' % A[i*2+1], end='')
    print()

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    heap_list = [int(item) for item in input().split()]

    for i, val in enumerate(heap_list):
        print('node {}: key = {}, '.format(i+1,val), end='')
        if (i - 1) // 2 >= 0:
            print('parent key = {}, '.format(heap_list[(i - 1) // 2]), end='')

        left_index = 2*i + 1
        if left_index < N:
            print('left key = {}, '.format(heap_list[left_index]), end='')

        right_index = 2*i + 2
        if right_index < N:
            print('right key = {}, '.format(heap_list[right_index]), end='')
        print('')


if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
sys.setrecursionlimit(15000)
n = int(input())
A = list(map(int,input().split()))

for i in range(n):
  if i != 0:
    parent = "parent key = {}, ".format(str(A[(i-1)//2]))
  else:
    parent = ""
  if i*2+1 < n:
    left = "left key = {}, ".format(str(A[i*2+1]))
  else:
    left = ""
  if i*2+2 < n:
    right = "right key = {}, ".format(str(A[i*2+2]))
  else:
    right = ""
  print("node {}: key = {}, {}{}{}".format(i+1,str(A[i]),parent,left,right))

Solution (Python):
def Parent(node_id):
    return int(node_id/2.0)

def LeftChild(node_id):
    return 2 * node_id

def RightChild(node_id):
    return 2 * node_id + 1

def Main():
    n = int(input())
    H = [None]*(n + 1)
    keys = [int(a) for a in input().split()]

    for i in range(1, n + 1):
        H[i] = keys[i - 1]

    for i in range(1, n + 1):
        information = "node {0}: key = {1}, ".format(i, H[i])
        
        if 1 <= Parent(i) <= n:
            information +=  "parent key = {0}, ".format(H[Parent(i)])

        if 1 <=  LeftChild(i) <= n:
            information += "left key = {0}, ".format(H[LeftChild(i)])

        if 1 <= RightChild(i) <= n:
            information += "right key = {0}, ".format(H[RightChild(i)])

        print(information)

Main()

Solution (Python):



n = int(input())

binary_heap = [int(i) for i in input().split()]
binary_heap = [0]+binary_heap

for i in range(1, n+1):
    s = 'node {}: key = {}, '.format(i, binary_heap[i])
    if i > 1:
        s += 'parent key = {}, '.format(binary_heap[i//2])
    if i * 2 <= n:
        s += 'left key = {}, '.format(binary_heap[i*2])
    if i * 2 + 1 <= n:
        s += 'right key = {}, '.format(binary_heap[i*2+1])

    print(s)

Solution (Python):

def parentNode(k):
    return k // 2


def leftNode(k):
    return 2 * k


def rightNode(k):
    return 2 * k + 1

banyak = int(input())
elems = [0] + [int(x) for x in input().split()]


for k in range(1, banyak+1):
    print("node {}: ".format(k), end="")
    print("key = {}, ".format(elems[k]), end="")
    if parentNode(k) >= 1:
        print("parent key = {}, ".format(
            elems[parentNode(k)]), end="")
    if leftNode(k) <= banyak:
        print("left key = {}, ".format(
            elems[leftNode(k)]), end="")
    if rightNode(k) <= banyak:
        print("right key = {}, ".format(
            elems[rightNode(k)]), end="")
    print()

Solution (Python):
import sys

def parent(i):
    return i // 2
def left(i):
    return 2 * i
def right(i):
    return 2 * i + 1



size = int(input())
heap = [i for i in map(int, input().split())]
heap = [0] + heap 

for i in range(1, size+1):
    print('node ' + str(i) + ': key = ' + str(heap[i]) + ', ', end='')
    if parent(i) >= 1:
        print('parent key = ' + str(heap[parent(i)]) + ', ', end='')
    if left(i) <= size:
        print('left key = ' + str(heap[left(i)]) + ', ', end='')
    if right(i) <= size:
        print('right key = ' + str(heap[right(i)]) + ', ', end='')
    print('')

Solution (Python):
import math

n = int(input())
leaves = list(map(int, input().split()))
leaves.insert(0, None)

for i in range(1, n+1):
  detail = ""
  detail += "node {0}: ".format(i)

  key = leaves[i]
  detail += "key = {0}, ".format(key)

  if i / 2 >= 1:
    detail += "parent key = {0}, ".format(leaves[i // 2])

  if 2 * i <= n:
    detail += "left key = {0}, ".format(leaves[2 * i])

  if (2*i+1) <= n:
    detail += "right key = {0}, ".format(leaves[2 * i + 1])

  print(detail)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

for i in range(n):
    id = i+1
    key = a[i]
    print('node %i: key = %i,'%(id, key), end='')
    
    if i>0:
        parent = a[id//2-1]
        print(' parent key = %i,'%parent, end='')
    
    if id*2-1<n:
        left = a[id*2-1]
        print(' left key = %i,'%(left), end='')
    if id*2<n:
        right = a[id*2]
        print(' right key = %i,'%(right), end='')
        
    print(' ')

Solution (Python):
H = int(input())
Heap = list(map(int,input().split()))
Heap = ["dummy"] + Heap

def Node_property(Heap):
    L = ["dummy"]
    for i in range(1,H+1):
        key,par,left,right = Heap[i],None,None,None
        if i//2 != 0:
            par = Heap[i//2]
        if 2*i < H+1:
            left = Heap[2*i]
        if 2*i+1 < H+1:
            right = Heap[2*i+1]
        L.append([key,par,left,right])
    return L

Ans = Node_property(Heap)
for i in range(1,H+1):
    print("node "+str(i)+": key = "+str(Ans[i][0])+", ",end="")
    for j in range(1,4):
        if Ans[i][j] != None:
            if j == 1:
                print("parent key = ",end="")
            if j == 2:
                print("left key = ",end="")
            if j == 3:
                print("right key = ",end="")
            print(str(Ans[i][j])+", ",end="")
    print()

Solution (Python):
def parentNode(k):
    return k // 2

def leftNode(k):
    return 2 * k 

def rightNode(k):
    return 2 * k + 1

H = int(input())
elems = [0] + [int(x) for x in input().split()]

for k in range(1, H+1):
    print(f'node {k}: ', end = '')
    print(f'key = {elems[k]}, ', end = '')
    if parentNode(k) >= 1:
        print(f'parent key = {elems[parentNode(k)]}, ', end = '')
    if leftNode(k) <= H:
        print(f'left key = {elems[leftNode(k)]}, ', end = '')
    if rightNode(k) <= H:
        print(f'right key = {elems[rightNode(k)]}, ', end = '')
    print()

Solution (Python):
if __name__ == '__main__':
    import sys
    input = sys.stdin.readline

    def parent(i):
        return i//2

    def left(i):
        return 2*i

    def right(i):
        return 2*i+1

    H = int(input())

    
    A = [None] + list(map(int, input().split()))

    
    for i in range(1, H+1):
        print('node ' + str(i) + ': ', end='')
        print('key = ' + str(A[i]) + ', ', end='')
        if 1 <= parent(i):
            print('parent key = ' + str(A[parent(i)]) + ', ', end='')
        if left(i) <= H:
            print('left key = ' + str(A[left(i)]) + ', ', end='')
        if right(i) <= H:
            print('right key = ' + str(A[right(i)]) + ', ', end='')
        print('')

Solution (Python):
def parent(i):
    return i//2
def left(i):
    return 2*i
def right(i):
    return 2*i+1

H = int(input())
A = [None] + list(map(int,input().split()))

for i in range(1,H+1):
    print('node {}: key = {}, '.format(i,A[i]), end='')
    if parent(i) >= 1:
        print('parent key = {}, '.format(A[parent(i)]), end='')
    if left(i) <= H:
        print('left key = {}, '.format(A[left(i)]), end='')
    if right(i) <= H:
        print('right key = {}, '.format(A[right(i)]), end='')
    print()

Solution (Python):
class Node:
  def __init__(self, id, lis):
    self.id = id + 1
    if id == 0:
      self.parent = None
    else:
      self.parent = lis[self.id // 2 - 1]
    if self.id * 2 + 1 <= len(lis):
      self.left = lis[self.id * 2 - 1]
      self.right = lis[self.id * 2]
    elif self.id * 2 <= len(lis) and self.id * 2 + 1> len(lis):
      self.left = lis[self.id * 2 - 1]
      self.right = None
    else:
      self.left = None
      self.right = None
    self.key = lis[id]
    self.lis = lis


  def print(self):
    if len(self.lis) == 1:
      print("node " + str(self.id) + ": key = " + str(self.key) + ", ")
    else:
      print("node " + str(self.id) + ": key = " + str(self.key) + ", ", end="")
      if self.parent == None and self.right == None:
        print("left key = " + str(self.left) + ", ")
      elif self.parent == None and self.left and self.right:
        print("left key = " + str(self.left) + ", " + "right key = " + str(self.right) + ", ")
      elif self.left and self.right:
        print("parent key = " + str(self.parent) + ", " + "left key = " + str(self.left) + ", " + "right key = " + str(self.right) + ", ")
      elif self.left and not self.right:
        print("parent key = " + str(self.parent) + ", " + "left key = " + str(self.left) + ", ")
      else:
        print("parent key = " + str(self.parent) + ", ")
    
H = int(input())

A = list(map(int, input().split()))

for i in range(H):
  node = Node(i, A)
  node.print()

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    keys = list(map(int, input().split()))
    for i, key in enumerate(keys, 1):
        print("node {}: key = {}, ".format(i, key), end="")
        if i//2:
            print("parent key = {}, ".format(keys[i//2-1]), end="")
        if 2*i-1 < n:
            print("left key = {}, ".format(keys[2*i-1]), end="")
        if 2*i < n:
            print("right key = {}, ".format(keys[2*i]), end="")
        print()