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):

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):
def walk_heap(A, idx, idx_format_map):
    if len(A) < idx:
        return
    idx_format = "node {}: ".format(idx)
    key_format = "key = {}, ".format(A[idx-1])
    parent_format = ""
    left_format = ""
    right_format = ""
    if idx > 1:
        parent_format = "parent key = {}, ".format(A[idx//2-1])
    if idx*2 <= len(A):
        left_format = "left key = {}, ".format(A[idx*2-1])
    if idx*2+1 <= len(A):
        right_format = "right key = {}, ".format(A[idx*2])
    answer_format = "{}{}{}{}{}".format(idx_format, key_format, parent_format, left_format, right_format)
    idx_format_map[idx] = answer_format
    walk_heap(A, idx*2, idx_format_map)
    walk_heap(A, idx*2+1, idx_format_map)
n = int(input())
A = list(map(int, input().split(' ')))
idx_format_map = {}
walk_heap(A, 1, idx_format_map)
for i in range(n):
    print(idx_format_map[i+1])

Solution (Python):
import math

n = input()

main = input().split()
main.insert(0,int(n))

del n

for i in range(1,main[0]+1):
	print("node " + str(i) + ": key = " + str(main[i]),end=", ")

	if i != 1:
		a = math.floor(i/2)
		print("parent key = " + main[a],end=", ")

	if i*2 <= main[0]:
		print("left key = " + str(main[i*2]),end=", ")
		a = i*2
		a += 1
		if a <= main[0]:
			print("right key = " + str(main[a]),end=", ")
	print()

Solution (Python):
"""
ALDS1_9_A
完全二分木を実装する
"""
import copy
import math

def get_parent(index):
    parent_index = math.floor((index-1)/2)
    return parent_index
    
def get_left(index):
    left_index = 2*index+1
    return left_index

def get_right(index):
    right_index = 2*index+2
    return right_index
    

n = int(input())
heap_list = list(map(int, input().split()))+[None]*(n+1)


for i in range(n):
    k = heap_list[i]
    k_sentence = 'key = '+str(k)+', '
    if heap_list[get_parent(i)]:
        p = heap_list[get_parent(i)]
        p_sentence = 'parent key = '+str(p)+', '
    else:
        p_sentence = ''
    if heap_list[get_left(i)]:
        l = heap_list[get_left(i)]
        l_sentence = 'left key = '+str(l)+', '
    else:
        l_sentence = ''
    if heap_list[get_right(i)]:
        r = heap_list[get_right(i)]
        r_sentence = 'right key = '+str(r)+', '
    else:
        r_sentence = ''

    print('node '+str(i+1)+': '+k_sentence+p_sentence+l_sentence+r_sentence)

Solution (Python):
import sys

heap_size = int(input()) 

key_list = list(map(int, sys.stdin.readline().split()))
for i, key in enumerate(key_list):
    node_no = i + 1
    parent_no = node_no // 2
    left_no = 2 * node_no
    right_no = left_no + 1

    print('node {}: key = {}, '.format(node_no, key), end='')
    if parent_no >= 1:
        print('parent key = {}, '.format(key_list[parent_no - 1]), end='')
    if left_no <= heap_size:
        print('left key = {}, '.format(key_list[left_no - 1]), end='')
    if right_no <= heap_size:
        print('right key = {}, '.format(key_list[right_no - 1]), end='')
    print()

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

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

Solution (Python):
n=int(input())+1
A=[0]+list(map(int,input().split()))
Heap=[]
for i in range(1,n):
    parent,right,left=i//2,i*2,i*2+1
    right=A[right] if right<n else 0
    left=A[left] if left<n else 0
    Heap.append([i,A[i],A[parent],right,left])

for i in range(n-1):
    node,key,parent,left,right=Heap[i]
    print("node {}: key = {}, ".format(node,key), end="")
    if parent!=0:print("parent key = {}, ".format(parent), end="")
    if left!=0:print("left key = {}, ".format(left), end="")
    if right!=0:print("right key = {}, ".format(right), end="")
    print()

Solution (Python):
def main():
    n = int(input())
    numbers = list(map(int, input().split()))
    for i in range(n):
        print("node {}: ".format(i + 1), end="")
        print("key = {}, ".format(numbers[i]), end="")
        if i + 1 > 1:
            print("parent key = {}, ".format(numbers[(i + 1) // 2 - 1]), end="")
        if (i + 1) * 2 <= n:
            print("left key = {}, ".format(numbers[(i + 1) * 2 - 1]), end="")
        if (i + 1) * 2 + 1 <= n:
            print("right key = {}, ".format(numbers[(i + 1) * 2 + 1 - 1]), end="")
        print("\n", end="")


if __name__ == "__main__":
    main()

Solution (Python):



def print_heap (heap, i):
    ret = "node {0}: key = {1}, ".format(i + 1, heap[i])

    p = get_parent_node(i)
    if p is not None:
        ret += "parent key = {0}, ".format(heap[p])

    len_heap = len(heap)
    l = get_left_node(i)
    r = get_right_node(i)

    if l < len_heap:
        ret += "left key = {0}, ".format(heap[l])
    if r < len_heap:
        ret += "right key = {0}, ".format(heap[r])

    print(ret)
    return

def get_parent_node (i):
    p = (i - 1) // 2
    if p < 0:
        return None
    else:
        return p

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

def get_right_node (i):
    r = (i + 1) * 2
    return r

def proc_inputs ():
    num_inputs = int(input())
    inputs = [i for i in input().split()]
    return num_inputs, inputs



def main ():
    num_inputs, inputs = proc_inputs()
    heap = [None] * num_inputs
    for i, node in enumerate(inputs):
        heap[i] = node

    for i, node in enumerate(inputs):
        print_heap(heap, i)
    
    exit(0)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
for i in range(n):
	ans1="node "+str(i+1)+": "
	ans2="key = "+str(a[i])+", "
	if (i+1)//2>=1:
		ans3="parent key = "+str(a[(i+1)//2-1])+", "
	else:
		ans3=""
	if (i+1)*2<=n:
		ans4="left key = "+str(a[(i+1)*2-1])+", "
	else:
		ans4=""
	if (i+1)*2+1<=n:
		ans5="right key = "+str(a[(i+1)*2])+", "
	else:
		ans5=""
	print(ans1+ans2+ans3+ans4+ans5)

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

Solution (Python):


def parent(i):
    if heap[i // 2] != None:
        print(", parent key = " + str(heap[i // 2]), end = "")
    else:
        print("", end = "")

def left(i):
    if 2 * i <= n:
        print(", left key = " + str(heap[2 * i]), end = "")
    else:
        print("", end = "")

def right(i):
    if 2 * i + 1 <= n:
        print(", right key = " + str(heap[2 * i + 1]), end = "")
    else:
        print("", end = "")


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

for i in range(1, n + 1):
    print("node", str(i) + ": key = " + str(heap[i]), end = "")
    parent(i)
    left(i)
    right(i)
    print(", ")

Solution (Python):
def parent_id(id, H):
    if 1 <= id // 2 and id // 2 <= H:
        return id// 2
    return None

def left_id(id, H):
    if 1 <= id  *2 and id * 2 <= H:
        return id* 2
    return None

def right_id(id, H):
    if 1 <= (id * 2) + 1 and (id * 2) + 1 <= H:
        return (id * 2) + 1
    return None

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

T = [{} for _ in range(H)]

for i in range(H):
    T[i]["id"] = i + 1
    T[i]["key"] = A[i]
    T[i]["parent_id"] = parent_id(T[i]["id"], H)
    T[i]["left_id"] = left_id(T[i]["id"], H)
    T[i]["right_id"] = right_id(T[i]["id"], H)

for i in range(H):
    parent_str, left_str, right_str = "", "", ""
    node_str = "node {}: ".format(T[i]["id"])
    key_str = "key = {}, ".format(T[i]["key"])
    if T[i]["parent_id"] != None:
        parent_str = "parent key = {}, ".format(T[int(T[i]["parent_id"])-1]["key"])
    if T[i]["left_id"] != None:
        left_str = "left key = {}, ".format(T[int(T[i]["left_id"])-1]["key"])
    if T[i]["right_id"] != None:
        right_str = "right key = {}, ".format(T[int(T[i]["right_id"])-1]["key"])
    print(node_str + key_str + parent_str + left_str + right_str)

Solution (Python):
def slove(nodelist):
    for i in range(len(nodelist)):
        out = 'node %d: key = %d, ' % (i + 1, nodelist[i])
        if i > 0:
            out += 'parent key = %d, ' % (nodelist[(i - 1)//2])
        if 2 * i + 1 < len(nodelist):
            out += 'left key = %d, ' % (nodelist[2 * i + 1])
        if 2 * i + 2 < len(nodelist):
            out += 'right key = %d, ' % (nodelist[2 * i + 2])
        print(out)

if __name__ == '__main__':
    n = input()
    nodelist = [int(num) for num in input().split(' ')]
    slove(nodelist)

Solution (Python):
h=int(input())
key=list(map(int,input().split()))
p=[None]*h
l=[None]*h
r=[None]*h
for i in range(h):
    if i>=1:
        p[i]=key[(i+1)//2-1]
    if (i+1)*2<=h:
        l[i]=key[i*2+1]
    if (i+1)*2+1<=h:
        r[i]=key[i*2+2]
for i in range(h):
    print("node {}:".format(i+1),end="")
    print(" key = {}".format(key[i]),end=",")
    if p[i]:
        print(" parent key = {}".format(p[i]),end=",")
    if l[i]:
        print(" left key = {}".format(l[i]),end=",")
    if r[i]:
        print(" right key = {}".format(r[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] * (H + 1)

_ = list(map(int, input().split()))
A[1:] = _

for i in range(1, H+1):
    output = 'node {}: key = {}, '.format(i, A[i])

    if parent(i) != 0:
        output += 'parent key = {}, '.format(A[parent(i)])

    if left(i) <= H:
        output += 'left key = {}, '.format(A[left(i)])

    if right(i) <= H:
        output += 'right key = {}, '.format(A[right(i)])

    print(output)

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

N = int(input())
btree = list(map(int, input().split()))

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

    print(result)

Solution (Python):
class Node():
    def __init__(self):
        self.left=None
        self.right=None
        self.key=None
        self.parent=None

def setkeys(T):
    length=len(T)
    ls=[Node() for i in range(length)]
    for i in range(length):
        ls[i].key=T[i]
        if i+1!=1:
            ls[i].parent=T[(i+1)//2-1]
        if length>=2*(i+1):
            ls[i].left=T[2*i+1]
        if length>=2*(i+1)+1:
            ls[i].right=T[2*i+2]
    return ls


h=int(input())
nodes=[int(i) for i in input().split()]
ls=setkeys(nodes)
j=0
for i in ls:
    j+=1
    if i.parent and i.left and i.right:
        print('node '+str(j)+': key = '+str(i.key)+', parent key = '+str(i.parent)+', left key = '+str(i.left)+', right key = '+str(i.right)+', ')
    elif i.left and i.right:
        print('node '+str(j)+': key = '+str(i.key)+', left key = '+str(i.left)+', right key = '+str(i.right)+', ')
    elif i.parent and i.left:
        print('node '+str(j)+': key = '+str(i.key)+', parent key = '+str(i.parent)+', left key = '+str(i.left)+', ')
    elif i.left:
        print('node '+str(j)+': key = '+str(i.key)+', left key = '+str(i.left)+', ')
    else:
        print('node '+str(j)+': key = '+str(i.key)+', parent key = '+str(i.parent)+', ')

Solution (Python):
n = int(input())
s = list(map(int, input().split()))
answerL = []
for i in range(len(s)):
    ans = 'node '+str(i+1)+': '
    ans+= 'key = '+str(s[i])+', '
    if i!= 0:
        ans+='parent key = '+str(s[(i-1)//2])+', '
    if 2*i+1 < len(s):
        ans+='left key = '+str(s[2*i+1])+', '
    if 2*i+2 < len(s):
        ans+='right key = '+str(s[2*i+2])+', '
    answerL.append(ans)
for ans in answerL:
    print(ans)

Solution (Python):
def solve(T, n):
    def judge(x):
        return True if 0 <= x < n else False

    for i in range(n):
        print("node " + str(i + 1) + ": ", end = "")
        print("key = " + str(T[i]) + ", ", end = "")
        if judge(int((i + 1) / 2) - 1):
            print("parent key = " + str(T[int((i+1)/2)-1]) + ", ", end = "")
        if judge(2 * i + 1):
            print("left key = " + str(T[2*i+1]) + ", ", end = "")
        if judge(2 * i + 2):
            print("right key = " + str(T[2*i+2]) + ", ", end = "")
        print("")
    return

n = int(input())
T = list(map(int, input().split(" ")))
solve(T, n)

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):






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):
def parent(i: int) -> int:
	return i // 2

def left(i: int) -> int:
	return 2 * i

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

if __name__ == "__main__":
	H = int(input())
	A = [None]
	A = A + list(map(int, input().split()))
	for i in range(1, H + 1):
		print(f"node {i}: key = {A[i]}, ", end="")
		if parent(i) >= 1:
			print(f"parent key = {A[parent(i)]}, ", end="")
		if left(i) <= H:
			print(f"left key = {A[left(i)]}, ", end="")
		if right(i) <= H:
			print(f"right key = {A[right(i)]}, ", end="")
		print()

Solution (Python):
MAX = 100000


def parent(i):
    return i//2


def left(i):
    return 2*i


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


def main():
    global H
    H = int(input())
    A = [0] * (MAX+1)

    v_s = input().split(' ')

    for i in range(H):
        A[i+1] = int(v_s[i])

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


if __name__ == '__main__':
    main()

Solution (Python):

import sys

H = int(input())

A = sys.stdin.readline().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 - 1]), end='')

    if (i + 1) * 2 - 1 <= H - 1:
        print('left key = {}, '.format(A[(i + 1) * 2 - 1]), end='')
        if (i + 1) * 2 <= H - 1:
            print('right key = {}, '.format(A[(i + 1) * 2]), end='')

    print('')

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

def main():
    H = int(input())
    A = [0] + list(map(int, input().split()))
    for k in range(1, H+1):
        pk = None
        lk = None
        rk = None
        if k // 2 > 0:
            pk = k // 2
        if 2 * k < H+1:
            lk = 2 * k
        if 2 * k + 1 < H+1:
            rk = 2 * k + 1
        print('node {}: key = {}, '.format(k, A[k]), end='')
        if pk:
            print('parent key = {}, '.format(A[pk]), end='')
        if lk:
            print('left key = {}, '.format(A[lk]), end='')
        if rk:
            print('right key = {}, '.format(A[rk]), end='')
        print()

if __name__=='__main__': main()

Solution (Python):
from math import ceil

N = int(input())
H = list(map(int, input().split()))

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

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

r = [-1]*n
l = [-1]*n
p = [-1]*n

for i in range(n):
    if 2*i+2<=n-1:
        r[i] = k[2*i+2]
    if 2*i + 1 <= n-1:
        l[i] = k[2*i+1]
    p[i] = k[(i-1)//2]
for i in range(n):
    print(f"node {i+1}:","key =",k[i],end =", ")
    if i!=0:
        print("parent key =",p[i],end=", ")
    if 2*i + 1 <= n-1:
        print("left key =",l[i], end=", ")
    if 2*i+2<=n-1:
        print("right key =",r[i], end=", ")
    print()

Solution (Python):
def hasParent(id, n, h):
    pk = (id+1)//2
    if pk >= 1 and pk <= n:
        return "parent key = {}, ".format(h[pk-1])
    else:
        return ""


def hasChildren(id, n, h):
    lk = (id+1)*2
    rk = (id+1)*2+1
    ret = ""
    if lk >= 1 and lk <= n:
        ret = "left key = {}, ".format(h[lk-1])

    if rk >= 1 and rk <= n:
        ret += "right key = {}, ".format(h[rk-1])

    return ret


def main():
    N = int(input())
    H = list(map(int, input().split()))

    for node, key in enumerate(H):
        addtext = hasParent(node, N, H)
        addtext += hasChildren(node, N, H)

        print("node {}: key = {}, {}".format(node+1, key, addtext))


if __name__ == '__main__':
    main()

Solution (Python):
from math import floor
n = int(input())
nodes = list(map(int, input().split()))
for i, node in enumerate(nodes, start=1):
    parent = floor(i / 2)
    left = i * 2
    right = i * 2 + 1
    print('node {}: key = {}, '.format(i, node), end='')
    if parent > 0:
        print('parent key = {}, '.format(nodes[parent - 1]), end='')
    if left <= n:
        print('left key = {}, '.format(nodes[left - 1]), end='')
    if right <= n:
        print('right key = {}, '.format(nodes[right - 1]), end='')
    print()

Solution (Python):
class Node:
    def __init__(self, idx, key):
        self.idx = idx
        self.key = key
        self.parent = None
        self.left = None
        self.right = None

    def print_self(self):
        s = 'node {}: key = {}, '.format(self.idx + 1, self.key)
        if self.parent is not None:
            s += 'parent key = {}, '.format(self.parent)
        if self.left is not None:
            s += 'left key = {}, '.format(self.left)
        if self.right is not None:
            s += 'right key = {}, '.format(self.right)
        print(s)

n = int(input())
array = list(map(int, input().split()))
heap = []

for i in range(n):
    node = Node(i, array[i])
    heap.append(node)

for i in range(1, n):
    node = heap[i]

    parent = heap[(i+1) // 2 - 1]
    node.parent = parent.key
    
    
    if i % 2 == 1:
        parent.left = node.key
    else:
        parent.right = node.key

for i in range(n):
    node = heap[i]
    node.print_self()

Solution (Python):
if __name__ == "__main__":
    node_num = int(input())
    heap = list(map(lambda x: int(x), input().split()))

    for idx in range(len(heap)):
        idx += 1  
        print(f"node {idx}: key = {heap[idx - 1]}, ", end="")

        parent_idx = idx // 2
        left_idx = 2 * idx
        right_idx = (2 * idx) + 1

        if 0 != parent_idx:
            print(f"parent key = {heap[parent_idx - 1]}, ", end="")
        if len(heap) >= left_idx:
            print(f"left key = {heap[left_idx - 1]}, ", end="")
        if len(heap) >= right_idx:
            print(f"right key = {heap[right_idx - 1]}, ", end="")

        print()

Solution (Python):
def parent(i):
    return(i // 2)

def left(i):
    return(i * 2)

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

H = int(input())
A_list = list(map(int, input().split()))
A_list.insert(0, 0)
for i in range(1, H+1):
    print("node ", i, ": key = ", A_list[i], ", ", sep="", end="")
    if parent(i) >= 1:
        print("parent key = ", A_list[parent(i)], ", ", sep="", end="")
    if left(i) <= H:
        print("left key = ", A_list[left(i)], ", ", sep="", end="")
    if right(i) <= H:
        print("right key = ", A_list[right(i)], ", ", sep="", end="")
    print("")

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

A = [None] * (H + 1)

for cnt, key in enumerate(map(int, input().split())):
    A[cnt + 1] = key

index_min = 1
index_max = len(A) - 1

for id in range(1, len(A)):
    k = A[id]
    print(f"node {id}: key = {k}, ", end="")

    index_pk = id // 2
    if index_min <= index_pk <= index_max:
        pk = A[index_pk]
        print(f"parent key = {pk}, ", end="")

    index_lk = 2 * id
    if index_min <= index_lk <= index_max:
        lk = A[index_lk]
        print(f"left key = {lk}, ", end="")

    index_rk = 2 * id + 1
    if index_min <= index_rk <= index_max:
        rk = A[index_rk]
        print(f"right key = {rk}, ", end="")

    print()

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):
h=int(input())
a=list(map(lambda x:int(x),input().split()))
for i in range(1,h+1):
   key=a[i-1]
   print("node {0}: key = {1}, ".format(i,key),end="")
   if i!=1:
       parentindex=i//2
       parentkey=a[parentindex-1]
       print("parent key = {0}, ".format(parentkey),end="")
   leftindex=2*i
   if leftindex<=h:
       leftkey=a[leftindex-1]
       print("left key = {0}, ".format(leftkey),end="")
   rightindex=2*i+1
   if rightindex<=h:
       rightkey=a[rightindex-1]
       print("right key = {0}, ".format(rightkey),end="")
   print()

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

class Node():
  def __init__(self, data):
    self.data = data
    self.parent = None
    self.left, self.right = None, None

def all_print(node, key, parent, left, right):
  if node:
    print("node {}: ".format(node), end="")
  if key:
    print("key = {}, ".format(key), end="")
  if parent:
    print("parent key = {}, ".format(parent), end="")
  if left:
    print("left key = {}, ".format(left), end="")
  if right:
    print("right key = {}, ".format(right), end="")
  print("")
  


result = []
for i in range(n):
  x = Node(li[i])
  result.append(x)

for i in range(n):
  x = result[i]
  if 2 * (i+1) <= n:
    x.left = result[2*(i+1)-1]
  if 2 * (i+1) + 1 <= n:
    x.right = result[2*(i+1)]
  if i >= 1:
    x.parent = result[int((i+1)/2)-1]
  result.append(x)

for i in range(n):
  node = i+1
  key = result[i].data
  if node != 1:
    parent = result[i].parent.data
  else:
    parent = None
  if result[i].left:
    left = result[i].left.data
  else:
    left = None
  if result[i].right:
    right = result[i].right.data
  else:
    right = None
  all_print(node, key, parent, left, right)

Solution (Python):
n = int(input())
H = [int(i) for i in input().split()]
for ni in range(1,n+1):
    print("node "+str(ni)+": ", end = "")
    print("key = "+str(H[ni-1])+", ", end = "")
    if ni != 1:
        print("parent key = "+str(H[ni//2-1])+", ", end = "")
    if 2*ni <= n:
        print("left key = "+str(H[2*ni-1])+", ", end = "")
    if 2*ni+1 <= n:
        print("right key = "+str(H[2*ni])+", ", end = "")
    print()

Solution (Python):
def parent(num):
    return int(num/2)

def left(num):
    return int(num*2)

def right(num):
    return int(num*2)+1

if __name__ == "__main__":    
    n = int(input())
    h_list = [-1] + list(map(int, input().split()))
    for i in range(1, n+1):
        sentence = 'node {0}: key = {1}, '.format(i, h_list[i])
        if parent(i) > 0:
            sentence += 'parent key = {0}, '.format(h_list[parent(i)])
        if left(i) <= n:
            sentence += 'left key = {0}, '.format(h_list[left(i)])
        if right(i) <= n:
            sentence += 'right key = {0}, '.format(h_list[right(i)])
        print(sentence)

Solution (Python):
import sys
input = sys.stdin.readline
H = int(input())
h = [int(ele) for ele in input().split()]
h = [0] + h


def main():
    for i in range(1, H+1):
        print('node ' + str(i) + ': key = ' +
              str(h[i]) + ', ' + parent(i) + left(i) + right(i))


def parent(i):
    if i == 1:
        return ''

    p_idx = int(i/2)
    return 'parent key = ' + str(h[p_idx]) + ', '


def left(i):
    l_idx = 2*i
    if H < l_idx:
        return ''

    return 'left key = ' + str(h[l_idx]) + ', '


def right(i):
    r_idx = 2*i + 1
    if H < r_idx:
        return ''

    return 'right key = ' + str(h[r_idx]) + ', '


if __name__ == "__main__":
    main()

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

Solution (Python):
class Heap():
    def __init__(self, H):
        self.n = len(H)
        self.nodes = {}
        for i, key in enumerate(H):
            self.add_node(i+1, key)
        self.connect()

    def add_node(self, index, key):
        if index not in self.nodes:
            self.nodes[index] = Node(index, key)

    def connect(self):
        for i in range(1, self.n+1):
            if i//2 != 0:
                self.nodes[i].parent = self.nodes[i//2]
            if i*2 <= self.n:
                self.nodes[i].left = self.nodes[i*2]
            if i*2+1 <= self.n:
                self.nodes[i].right = self.nodes[i*2 + 1]


class Node():
    def __init__(self, index, key):
        self.index = index
        self.key = key
        self.parent = None
        self.left = None
        self.right = None

    def __str__(self):
        out = 'node {}: key = {}, '.format(self.index, self.key)
        if self.parent:
            out += 'parent key = {}, '.format(self.parent.key)
        if self.left:
            out += 'left key = {}, '.format(self.left.key)
        if self.right:
            out += 'right key = {}, '.format(self.right.key)
        return out


n = int(input())
H = list(map(int, input().split()))
heap = Heap(H)
for i in range(1, n+1):
    print(heap.nodes[i])

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

class Node():
    def __init__(self,value):
        self.value = value
        self.parent = -1
        self.kids = []

    def addParent(self,parent):
        self.parent = parent

    def addKid(self,kids):
        self.kids = kids

nodes = {}
for i in range(len(elements)):
    nodes[i] = Node(int(elements[i]))

parentIndex = 0
def checkKids(index):
    leftKid = index*2+1
    rightKid = index*2+2
    kids = []
    if leftKid in nodes:
        kids.append(leftKid)
        nodes[leftKid].addParent(index)
        checkKids(leftKid)
    if rightKid in nodes:
        kids.append(rightKid)
        nodes[rightKid].addParent(index)
        checkKids(rightKid)
    nodes[index].addKid(kids)

checkKids(parentIndex)

for id in nodes:
    ans = ""
    ans += "node " + str(id+1)
    ans += ": key = " + str(nodes[id].value)
    if not nodes[id].parent == -1:
        ans += ", parent key = " + str(nodes[nodes[id].parent].value)
    if len(nodes[id].kids) == 1:
        ans += ", left key = " + str(nodes[nodes[id].kids[0]].value)
    elif len(nodes[id].kids) == 2:
        ans += ", left key = " + str(nodes[nodes[id].kids[0]].value)
        ans += ", right key = " + str(nodes[nodes[id].kids[1]].value)
    print(ans + ", ")

Solution (Python):
h = int(input())
t = input().split()
for i in range(h):
    ans = f"node {i+1}: key = {t[i]}, "
    p = (i+1)//2-1
    if p >= 0:
        ans += f"parent key = {t[min([p,h])]}, "
    l, r = (i+1)*2-1, (i+1)*2
    if l < h:
        ans += f"left key = {t[l]}, "
        if r < h:
            ans += f"right key = {t[r]}, "
    print(ans)

Solution (Python):
n = int(input())
heap = list(map(int, input().split()))
for i in range(len(heap)):
    out = 'node {}: key = {}, '.format(i + 1, heap[i])
    parent = (i - 1) // 2
    out += 'parent key = {}, '.format(heap[parent]) if parent >= 0 else ''
    left = 2 * i + 1
    out += 'left key = {}, '.format(heap[left]) if left < len(heap) else ''
    right = 2 * i +2
    out += 'right key = {}, '.format(heap[right]) if right < len(heap) else ''
    print(out)