-
Notifications
You must be signed in to change notification settings - Fork 43
/
binary_tree.py
49 lines (36 loc) · 1.02 KB
/
binary_tree.py
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
41
42
43
44
45
46
47
48
49
#---------------------------------------------------------------
# BINARY TREE (V1)
#---------------------------------------------------------------
# ARRAY IMPLEMENTATION
# https://www.geeksforgeeks.org/binary-tree-array-implementation/
tree = [None] * 10
def root(key):
if tree[0] != None:
print("Tree already had root")
else:
tree[0] = key
def set_left(key, parent):
if tree[parent] == None:
print("Can't set child at", (parent * 2) + 1, ", no parent found")
else:
tree[(parent * 2) + 1] = key
def set_right(key, parent):
if tree[parent] == None:
print("Can't set child at", (parent * 2) + 2, ", no parent found")
else:
tree[(parent * 2) + 2] = key
def print_tree():
for i in range(10):
if tree[i] != None:
print(tree[i], end="")
else:
print("-", end="")
print()
# Driver Code
# root('A')
# set_left('B', 0)
# set_right('C', 0)
# set_left('D', 1)
# set_right('E', 1)
# set_right('F', 2)
# print_tree()