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())
tree = list(map(int, input().split()))

for i in range(n) :
    s = 'node ' + str(i + 1) + ': key = ' + str(tree[i])
    if (i > 0) & (i % 2 == 0) :
        s += ', parent key = ' + str(tree[i // 2 - 1])
    elif (i > 0) & (i % 2 == 1):
        s += ', parent key = ' + str(tree[i // 2])

    if i * 2 + 2 < n :
        s += ', left key = ' + str(tree[i * 2 + 1]) + ', right key = ' + str(tree[i * 2 + 2])
    elif i * 2 + 1 < n :
        s += ', left key = ' + str(tree[i * 2 + 1])

    print(s, ', ', sep = '')

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

for i in range(n) :
    s = 'node ' + str(i + 1) + ': key = ' + str(tree[i])
    if (i > 0) & (i % 2 == 0) :
        s += ', parent key = ' + str(tree[i // 2 - 1])
    elif (i > 0) & (i % 2 == 1):
        s += ', parent key = ' + str(tree[i // 2])

    if i * 2 + 2 < n :
        s += ', left key = ' + str(tree[i * 2 + 1]) + ', right key = ' + str(tree[i * 2 + 2])
    elif i * 2 + 1 < n :
        s += ', left key = ' + str(tree[i * 2 + 1])

    print(s, ', ', sep = '')

Solution (Python):
import sys
import math



H = int(input())

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

parent, left, right = [], [], []


for i in range(H):
    count = i+1
    if count == 1:
        parent.append(0)
    else:
        parent.append(A[math.floor(count/2)-1])

    if 2*count <= len(A):
        left.append(A[2*count-1])
    else:
        left.append(0)

    if 2*count+1 <= len(A):
        right.append(A[2*count])
    else:
        right.append(0)

for i in range(H):

    sys.stdout.write('node %d: key = %d, ' % (i+1, A[i]))

    if parent[i] != 0:
        sys.stdout.write('parent key = %d, ' % parent[i])

    if left[i] != 0:
        sys.stdout.write('left key = %d, ' % left[i])

    if right[i] != 0:
        sys.stdout.write('right key = %d, ' % right[i])
    print()

Solution (Python):
import sys
import math



H = int(input())

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

parent, left, right = [], [], []


for i in range(H):
    count = i+1
    if count == 1:
        parent.append(0)
    else:
        parent.append(A[math.floor(count/2)-1])

    if 2*count <= len(A):
        left.append(A[2*count-1])
    else:
        left.append(0)

    if 2*count+1 <= len(A):
        right.append(A[2*count])
    else:
        right.append(0)

for i in range(H):

    sys.stdout.write('node %d: key = %d, ' % (i+1, A[i]))

    if parent[i] != 0:
        sys.stdout.write('parent key = %d, ' % parent[i])

    if left[i] != 0:
        sys.stdout.write('left key = %d, ' % left[i])

    if right[i] != 0:
        sys.stdout.write('right key = %d, ' % right[i])
    print()

Solution (Python):
import sys
import math



H = int(input())

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

parent, left, right = [], [], []


for i in range(H):
    count = i+1
    if count == 1:
        parent.append(0)
    else:
        parent.append(A[math.floor(count/2)-1])

    if 2*count <= len(A):
        left.append(A[2*count-1])
    else:
        left.append(0)

    if 2*count+1 <= len(A):
        right.append(A[2*count])
    else:
        right.append(0)

for i in range(H):

    sys.stdout.write('node %d: key = %d, ' % (i+1, A[i]))

    if parent[i] != 0:
        sys.stdout.write('parent key = %d, ' % parent[i])

    if left[i] != 0:
        sys.stdout.write('left key = %d, ' % left[i])

    if right[i] != 0:
        sys.stdout.write('right key = %d, ' % right[i])
    print()

Solution (Python):
class Node():
    def __init__(self, id, key):
        self.id = id
        self.key = key

    def setKeys(self, data):
        length = len(data)
        if ( self.id // 2 ) != 0:
            self.pk = data[self.id // 2]
        else :
            self.pk = -1
        if self.id * 2 < length:
             self.lk = data[self.id * 2]
        else :
            self.lk = -1
        if self.id * 2 + 1 < length:
             self.rk = data[self.id * 2 + 1]
        else :
            self.rk = -1

    def printInfo(self):
        print( "node " + str(self.id) + ": key = " + str(self.key), end=", " )
        if self.pk != -1:
            print( "parent key = " + str(self.pk), end = ", ")
        if self.lk != -1:
            print( "left key = " + str(self.lk), end = ", ")
        if self.rk != -1:
            print( "right key = " + str(self.rk) , end=", ")
        print()

def main():
    n = int( input() )
    inputs = input().split()
    for tmp in inputs:
        tmp = int(tmp)
    data = [0]
    data.extend(inputs)
    Tree = []
    for i in range(1, n+1):
        Tree.append( Node(i, data[i]) )
    for node in Tree:
        node.setKeys( data )
        node.printInfo()

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
klist = list(map(int,input().split()))
plist = []
i = 0
k = 1
a = 2**i-1
while 1:
    if klist[a:a+2**i] != []:
        plist.append(klist[a:a + 2**i])
    else: break
    a += 2**i
    i += 1
for i in range(len(plist)):
    for j in range(len(plist[i])):
        try:
            if i==0:
                print("node "+str(k)+": key = "+str(plist[0][0])+
                      ", left key = "+str(plist[1][0])+
                      ", right key = "+str(plist[1][1])+", ")
            else :
                l = int(j/2)
                print("node "+str(k)+": key = "+str(plist[i][j])+
                      ", parent key = "+str(plist[i-1][l])+", left key = "+
                      str(plist[i+1][2*j]),end="")
                try:
                    print(", right key = "+str(plist[i+1][2*j+1])+", ")
                except:
                    print(", ")
        except:
            l = int(j/2)
            print("node "+str(k)+": key = "+str(plist[i][j])+
                  ", parent key = "+str(plist[i-1][l])+", ")
        k += 1

Solution (Python):


class MakeTree():
    def __init__(self, p=None, l=None, r=None):
        self.p = p
        self.l = l
        self.r = r


n = int(input())
List = list(map(int, input().split()))

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

Solution (Python):


import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

H=INT()
A=LIST()+[None]*(H*2)

for i in range(H):
    node=i+1
    key=A[i]
    parent=node//2
    left=node*2
    right=node*2+1
    print('node '+str(node)+': key = '+str(key)+', ', end='')
    if parent!=0:
        print('parent key = '+str(A[parent-1])+', ', end='')
    if A[left-1] is not None:
        print('left key = '+str(A[left-1])+', ', end='')
    if A[right-1] is not None:
        print('right key = '+str(A[right-1])+', ', end='')
    print('')

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

H = int(input())
heap = [0] * (H+1)
for i, num in enumerate(map(int, input().split())):
    heap[i+1] = num



def get_parent(heap, i):
    if i // 2 > 0:
        return heap[i // 2]
    else:
        return None

def get_left_child(heap, i):
    if 2 * i <= H:
        return heap[2 * i]
    else:
        None

def get_right_child(heap, i):
    if 2 * i + 1 <= H:
        return heap[2 * i + 1]
    else:
        return None
for i in range(1, H+1):
    msg = 'node ' + str(i) + ': key = ' + str(heap[i]) + ', '
    parent_key = get_parent(heap, i)
    
    if parent_key:
        msg += 'parent key = ' + str(parent_key) + ', '

    left_key = get_left_child(heap, i)
    if left_key:
        msg += 'left key = ' + str(left_key) + ', '

    right_key = get_right_child(heap, i)
    if right_key:
        msg += 'right key = ' + str(right_key) + ', '
    print(msg)

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)

Solution (Python):
import sys

def fun(arr):
	sz = 1
	while sz < len(arr): sz *= 2

	for idx, val in enumerate(arr):
		sys.stdout.write('node '+str(idx + 1) + ': key = ' + str(val) + ', ')
		if idx > 0:
		    sys.stdout.write('parent key = ' + str(arr[(idx - 1) // 2]) + ', ')
		if idx * 2 + 1 < len(arr):
		    sys.stdout.write('left key = ' + str(arr[idx * 2 + 1]) + ', ')
		if idx * 2 + 2 < len(arr):
		    sys.stdout.write('right key = ' + str(arr[idx * 2 + 2]) + ', ')
		sys.stdout.write('\n')
			
			
if __name__ == '__main__':
    n = sys.stdin.readline()
    n = int(n)
    arr = sys.stdin.readline()
    arr = map(lambda x:int(x), arr.split())
    fun(arr)

Solution (Python):

H = int(raw_input())
A = map(int,raw_input().split())

for i in range(H):
    parent_key = ""
    left_key = ""
    right_key = ""
    ans = ""
    if i == 0:
        if 2*(i+1) - 1 < H:
            left_key = "left key = " + str(A[2*(i+1)-1])  +", "
        if 2*(i+1) < H:
            right_key= "right key = " + str(A[2*(i+1)]) + ", "
    else:
        parent_key = "parent key = " + str(A[(i+1)/2-1]) + ", "
        if 2*(i+1) - 1 < H:
            left_key = "left key = " + str(A[2*(i+1)-1])  +", "
        if 2*(i+1) < H:
            right_key= "right key = " + str(A[2*(i+1)]) + ", "
    ans = "node " +str(i + 1) + ": key = "+ str(A[i]) + ", " +\
        parent_key + left_key + right_key
    print ans

Solution (Python):

H = int(raw_input())
A = map(int,raw_input().split())

for i in range(H):
    parent_key = ""; left_key = ""; right_key = ""; ans = ""
    if i > 0:
        parent_key = "parent key = " + str(A[(i+1)/2-1]) + ", "
    if 2*(i+1) - 1 < H:
        left_key = "left key = " + str(A[2*(i+1)-1])  +", "
    if 2*(i+1) < H:
        right_key= "right key = " + str(A[2*(i+1)]) + ", "
    ans = "node " +str(i + 1) + ": key = "+ str(A[i]) + ", " +\
        parent_key + left_key + right_key
    print ans

Solution (Python):



import sys

nary = int(sys.stdin.readline())
ary = [int(x) for x in sys.stdin.readline().rstrip().split(' ')]
assert nary == len(ary)

for (i, x) in enumerate(ary):
    id_ = i + 1
    key = x
    parent = id_ / 2
    left = id_ * 2
    right = id_ * 2 + 1
    str_ = 'node %d: key = %d, ' % (id_, key)
    if parent > 0:
        str_ += 'parent key = %d, ' % (ary[parent - 1])
    if left <= nary:
        str_ += 'left key = %d, ' % (ary[left - 1])
    if right <= nary:
        str_ += 'right key = %d, ' % (ary[right - 1])
    print str_

Solution (Python):
H = int(input())
heap = list(map(int, input().split()))
heap.insert(0, 0)
for i in range(1, len(heap)):
    p = i // 2
    l = i * 2
    r = i * 2 + 1
    s = 'node ' + str(i) + ': key = ' + str(heap[i]) + ', '
    if 1 <= p <= H:
        s += 'parent key = ' + str(heap[p]) + ', '
    if l <= H:
        s += 'left key = ' + str(heap[l]) + ', '
    if r <= H:
        s += 'right key = ' + str(heap[r]) + ', '

    print(s)

Solution (Python):
def parent(n):
    return (n+1)/2-1

def left(n):
    return (n+1)*2-1

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

heap = []

H = int(raw_input())
keys = map(int, raw_input().strip().split(' '))
i = 0
for k in keys:
    p = parent(i)
    l = left(i)
    r = right(i)
    print "node %s: key = %s," % (i+1, k),
    if p  >= 0: print "parent key = %s," % keys[p],
    if l < len(keys): print "left key = %s," % keys[l],
    if r < len(keys): print "right key = %s," % keys[r],
    print "",
    print
    i += 1

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

for i, key in enumerate(heap):
    i += 1
    pi, cli = i // 2, i * 2
    rel_nodes = ''
    if pi:
        rel_nodes += 'parent key = {}, '.format(heap[pi - 1])
    if cli <= h:
        rel_nodes += 'left key = {}, '.format(heap[cli - 1])
        if cli < h:
            rel_nodes += 'right key = {}, '.format(heap[cli])

    print('node {}: key = {}, '.format(i, key) + rel_nodes)

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

def left(i):
    return 2*i

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

def main():
    H = int(raw_input())
    A = map(int,raw_input().split())
    for i in xrange(1,H+1):
        print "node {}: key = {},".format(i, A[i-1]),
        if parent(i) >= 1: 
            print "parent key = {},".format(A[parent(i)-1]),
        if left(i) <= H: 
            print "left key = {},".format(A[left(i)-1]),
        if right(i) <= H:
            print "right key = {},".format(A[right(i)-1]),
        print '',
        print 
    return 0

main()

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

def right(i):
    return 2 * i + 1
 
if __name__ == '__main__':
    n = int(input().rstrip())
    a = [-1]
    a += [int(i) for i in input().rstrip().split(" ")]
    for i in range(1, n+1):
        print("node {}: ".format(i), end = "")
        print("key = {}, ".format(a[i]),end = "") 
        if parent(i) >= 1:
            print("parent key = {}, ".format(a[parent(i)]), end = "")
        if left(i) <= n:
            print("left key = {}, ".format(a[left(i)]), end = "")
        if right(i) <= n:
            print("right key = {}, ".format(a[right(i)]), end = "")
        print()

Solution (Python):
H = int(input())
heap = list(map(int, input().split()))
for i, h in enumerate(heap):
    index = i + 1
    print("node {}: key = {}, ".format(index, h), end='')
    if index // 2:
        print("parent key = {}, ".format(heap[index // 2 - 1]), end='')
    if index*2 <= H:
        print("left key = {}, ".format(heap[index*2 - 1]), end='')
    if index*2 < H:
        print("right key = {}, ".format(heap[index*2]), end='')
    print()

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

last_index = H + 1

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

for i in range(1, last_index):
    print('node {0}: '.format(i), end = '')
    print('key = {0}, '.format(keys[i]), end = '')
    if i != 1:
        print('parent key = {0}, '.format(keys[i // 2]), end = '')
    left_index = i * 2
    if left_index < last_index:
        print('left key = {0}, '.format(keys[left_index]), end = '')
    right_index = left_index + 1
    if right_index < last_index:
        print('right key = {0}, '.format(keys[right_index]), end = '')
    print('')

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

last_index = H + 1

keys = [None] + input().split()

for i in range(1, last_index):
    print('node {0}: '.format(i), end = '')
    print('key = {0}, '.format(keys[i]), end = '')
    if i != 1:
        print('parent key = {0}, '.format(keys[i // 2]), end = '')
    left_index = i * 2
    if left_index < last_index:
        print('left key = {0}, '.format(keys[left_index]), end = '')
    right_index = left_index + 1
    if right_index < last_index:
        print('right key = {0}, '.format(keys[right_index]), end = '')
    print('')

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

keys = [None] + input().split()

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

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

keys = [None] + input().split()

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

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

keys = input().split()

for i, x in enumerate(keys):
    h_i = i + 1
    print('node {0}: '.format(h_i), end = '')
    print('key = {0}, '.format(keys[i]), end = '')
    if h_i != 1:
        print('parent key = {0}, '.format(keys[h_i // 2 - 1]), end = '')
    left_index = h_i * 2
    if left_index <= H:
        print('left key = {0}, '.format(keys[left_index - 1]), end = '')
    right_index = left_index + 1
    if right_index <= H:
        print('right key = {0}, '.format(keys[right_index - 1]), end = '')
    print('')

Solution (Python):


n = int(raw_input())
l = map(int, raw_input().split())
A = [0]
for i in range(n):
    A.append(l[i])
print "node %d: key = %d, left key = %d, right key = %d, " %(1, A[1], A[2*1], A[2*1+1])
for i in range(2, int((n+1)/2)):
    print "node %d: key = %d, parent key = %d, left key = %d, right key = %d, " %(i, A[i], A[int(i/2)], A[2*i], A[2*i+1])
if not n%2:
    i = n/2
    print "node %d: key = %d, parent key = %d, left key = %d, " %(i, A[i], A[int(i/2)], A[2*i])
for i in range(int(n/2)+1, n+1):
    print "node %d: key = %d, parent key = %d, " %(i, A[i], A[int(i/2)])

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

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

Solution (Python):
import sys

if __name__ == "__main__":
    lines = sys.stdin.readlines()
    A = map(int, lines[1].split())
    H = len(A)

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

Solution (Python):


size = int(input())
heap = list(map(int, input().split()))

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

Solution (Python):
class complete_btree:
    def __init__(self, vals):
        self.vals=vals
        self.index_max = len(self.vals)

    def check(self, i):
        assert(i >= 1)
        assert(i <= self.index_max)

    def val_at(self, i):
        self.check(i)
        return self.vals[i-1]

    def left_idx(self, i):
        self.check(i)
        t = 2*i
        if t <= self.index_max:
            return t
        else:
            return None

    def right_idx(self, i):
        self.check(i)
        t = 2*i + 1
        if t <= self.index_max:
            return t
        else:
            return None

    def parent_idx(self, i):
        self.check(i)
        t = i//2
        if t > 0:
            return t
        else:
            return None


if __name__ == "__main__":
    H = int(input())   
    vals_str = input()   
    vals = vals_str.split(' ')

    cbt = complete_btree(vals)
    for i in range(1, H+1):
        print('node {i}: key = {k}, '.format(i=i, k=cbt.val_at(i)), end='')
        p_idx = cbt.parent_idx(i)
        l_idx = cbt.left_idx(i)
        r_idx = cbt.right_idx(i)
        if p_idx is not None:
            print('parent key = {k}, '.format(k=cbt.val_at(p_idx)), end='')
        if l_idx is not None:
            print('left key = {k}, '.format(k=cbt.val_at(l_idx)), end='')
        if r_idx is not None:
            print('right key = {k}, '.format(k=cbt.val_at(r_idx)), end='')
        print('')

Solution (Python):
def get_list(L,i): return L[i] if len(L) > i else None
def print_key(key_name,val): print(key_name,"key = {0},".format(val), end=" ")
H = int(input())
hp = list(map(int,input().split()))
if H > 0:
    print_key("node 1:", hp[0])
    l = get_list(hp,1)
    r = get_list(hp,2)
    if l: print_key("left",l)
    if r: print_key("right",r)
    print("")
if H > 1:
    for i in range(2,H+1):
        print_key("node {0}:".format(i), hp[i-1])
        print_key("parent", hp[i//2-1])
        l = get_list(hp,i*2 -1)
        r = get_list(hp,i*2   )
        if l: print_key("left",l)
        if r: print_key("right",r)
        print("")

Solution (Python):
n = int(raw_input())
heap = map(int,raw_input().split(' '))

for i in range(len(heap)):
    j = i+1
    nid = j
    key = heap[j-1]
    pid = j/2-1
    if pid >= 0:
        p = heap[pid]
    lid = j*2-1
    rid = j*2
    if lid < len(heap):
        l = heap[j*2-1]
    if rid < len(heap):
        r = heap[j*2]
    print "node %s:" % nid,
    print "key = %s," % key,
    if pid >= 0:
        print "parent key = %s," % p,
    if lid < len(heap):
        print "left key = %s," % l,
    if rid < len(heap):
        print "right key = %s," % r,
    print ''

Solution (Python):
n = int(input())
heap_keys = list(map(int, input().split()))
heap = []
for i,x in enumerate(heap_keys):
    node = i+1
    key = x
    parent_key = heap_keys[int(node/2)-1] if i > 0 else None
    left_key = heap_keys[2*node-1] if 2*node-1 <len(heap_keys) else None
    right_key = heap_keys[2*node] if 2*node <len(heap_keys) else None
    heap.append({"node":node, "key":x, "parent_key":parent_key, "left_key":left_key, "right_key":right_key})

for h in heap:
    out_str = "node %s: key = %s, " % (h["node"],h["key"])
    if h["parent_key"]:out_str += "parent key = %s, " % h["parent_key"]
    if h["left_key"]:out_str += "left key = %s, " % h["left_key"]
    if h["right_key"]:out_str += "right key = %s, " % h["right_key"]
    print(out_str)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = [-1] + a
for i, v in enumerate(a[1:], start=1):
    base = "node {0}: key = {1}, ".format(i, v)
    parent = "parent key = {0}, ".format(a[i//2]) if i != 1 else ""
    left = "left key = {0}, ".format(a[i*2]) if i * 2 <= n else ""
    right = "right key = {0}, ".format(a[i*2 + 1]) if i * 2 + 1 <= n else ""
    print(base + parent + left + right)

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

Solution (Python):


def complete_bin_tree(A, i):
    key = i - 1
    left = (i * 2) - 1 if (i * 2) - 1 < len(A) else -1
    right = i * 2 if i * 2 < len(A) else -1
    parent = int(i/2) - 1 if int(i/2) > 0 else -1

    print("node " + str(i) + ": key = " + str(A[key]) + ", ", end = "")
    if parent >= 0:
        print("parent key = " + str(A[parent]) + ", ", end = "")
    if left >= 0:
        print("left key = " + str(A[left]) + ", ", end = "")
    if right >= 0:
        print("right key = " + str(A[right]) + ", ", end = "")

    print()


if __name__ == "__main__":
    H = int(input())
    a = [int(x) for x in input().split()]

    for i in range(1, H + 1):
        complete_bin_tree(a, i)

Solution (Python):
import math
n = int(input())
heap = [None] + list(map(int, input().split()))
for i, x in enumerate(heap):
    if i == 0:
        continue
    p = math.floor(i / 2)
    parent = ''
    if 1 <= p:
        parent = "parent key = {}, ".format(heap[p])
    left = i * 2
    l = ''
    if left <= n:
        l = 'left key = {}, '.format(heap[left])
    right = i * 2 + 1
    r = ''
    if right <= n:
        r = 'right key = {}, '.format(heap[right])

    ans = "node {i}: key = {key}, {parent}{l}{r}".format(i=i, key=x, parent=parent, l=l, r=r)
    print(ans)

Solution (Python):

class Node:
    def __init__(self,point):
        self.n=point
        self.p=None
        self.left=None
        self.right=None

def CBT(A,n):
    nlist=[]
    for i in range(n+1):
        if i==0:
            nlist.append(Node(0))
        else:
            nlist.append(Node(A[i-1]))
    for i in range(n+1):
        if i==0:
            continue
        if i/2>=1:
            nlist[i].p=nlist[int(i/2)].n
        if 2*i<=n:
            nlist[i].left=nlist[2*i].n
        if 2*i+1<=n:
            nlist[i].right=nlist[2*i+1].n
    for i in range(n+1):
        if i==0:
            continue
        ans="node "+str(i)+": key = "+nlist[i].n+", "
        if i != 1:
            ans+="parent key = "+nlist[i].p+", "
        if nlist[i].left!=None:
            ans+="left key = "+nlist[i].left+", "
        if nlist[i].right!=None:
            ans+="right key = "+nlist[i].right+", "
        print(ans)
        
        
            
if __name__=="__main__":
    n=int(input())
    A=input().split(" ")
    CBT(A,n)

Solution (Python):



import sys


def get_parent(index):
    return index // 2


def get_left(index):
    return 2 * index


def get_right(index):
    return get_left(index) + 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    array.insert(0, None)
    for i in range(1, array_length + 1):
        parent, left, right = get_parent(i), get_left(i), get_right(i)
        print('node ' + str(i) + ': ', end='')
        print('key = ' + str(array[i]) + ',', end='')
        if parent:
            print(' parent key = ' + str(array[parent]) + ',', end='')
        if left <= array_length:
            print(' left key = ' + str(array[left]) + ',', end='')
        if right <= array_length:
            print(' right key = ' + str(array[right]) + ',', end='')
        print(' ')

Solution (Python):



import sys


def get_parent(index):
    return index // 2


def get_left(index):
    return 2 * index


def get_right(index):
    return get_left(index) + 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    array.insert(0, None)
    for i in range(1, array_length + 1):
        parent, left, right = get_parent(i), get_left(i), get_right(i)
        print('node ' + str(i) + ': ', end='')
        print('key = ' + str(array[i]) + ',', end='')
        if parent:
            print(' parent key = ' + str(array[parent]) + ',', end='')
        if left <= array_length:
            print(' left key = ' + str(array[left]) + ',', end='')
        if right <= array_length:
            print(' right key = ' + str(array[right]) + ',', end='')
        print(' ')

Solution (Python):



import sys


def get_parent(index):
    return index // 2


def get_left(index):
    return 2 * index


def get_right(index):
    return get_left(index) + 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    array_length = int(_input[0])
    array = list(map(int, _input[1].split()))
    array.insert(0, None)
    for i in range(1, array_length + 1):
        parent, left, right = get_parent(i), get_left(i), get_right(i)
        print('node ' + str(i) + ':', end='')
        print(' key = ' + str(array[i]) + ',', end='')
        if parent:
            print(' parent key = ' + str(array[parent]) + ',', end='')
        if left <= array_length:
            print(' left key = ' + str(array[left]) + ',', end='')
        if right <= array_length:
            print(' right key = ' + str(array[right]) + ',', end='')
        print(' ')

Solution (Python):
def has_child(data, n):
    data_len = len(data) - 1  
    if data_len < n * 2:
        return 0
    else:
        if data_len >= (n * 2) + 1:
            return 2
        else:
            return 1


if __name__ == '__main__':
    
    num_of_data = int(input())
    data =  [int(x) for x in input().split(' ')]

    
    data.insert(0, None)  

    
    for i in range(1, num_of_data+1):
        print('node {0}: key = {1}, '.format(i, data[i]), end='')
        if i > 1:
            print('parent key = {0}, '.format(data[i//2]), end='')
        child = has_child(data, i)
        if child == 1:
            print('left key = {0}, '.format(data[i*2]), end='')
        if child == 2:
            print('left key = {0}, right key = {1}, '.format(data[i*2], data[i*2+1]), end='')
        print('')