This python program involves constructing a complete binary tree from a given array in level order fashion. A binary tree is a complete binary tree if all leve will be filled in the tree level wise starting from level 0. Task is very simple. It can be done in python the following way.

Example :

>>> print(build([3, 4, 5, 6, 7, 8, 9, 13, 14])) __3__ / \ ___4 5 / \ / \ _6 7 8 9 / \ 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | class Node: def __init__(self, data): self.value = data self.left = None self.right = None class BST: def __init__(self, root): self.root = root def in_order(self, start, traversal): if start: traversal = self.in_order(start.left, traversal) traversal.append(start.value) traversal = self.in_order(start.right, traversal) return traversal def arr_to_tree(arr, root, i, n): if i < n: temp = Node(arr[i]) root = temp root.left = arr_to_tree(arr, root.left, 2*i+1, n) root.right = arr_to_tree(arr, root.left, 2*i+2, n) return root def main(): li = [ |

apoorv@apoorv:~/Desktop/classes_py/I_ship/binary_tree_DS$ python arr_to_complete_bt.py [8, 4, 2, 2, 3, 0, 3, 9, 6, 7, 8]

Visualization for more clarity:

>>> print(build([9, 2, 7, 4, 0, 6, 8, 8, 2, 3])) ____9__ / \ __2__ 7 / \ / \ 4 0 6 8 / \ / 8 2 3 >>>

## Comments