-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBinaryTree_UsingListofLists.py
66 lines (57 loc) · 1.29 KB
/
BinaryTree_UsingListofLists.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def binary_tree(n):
return [n,[],[]]
def insert_left(root,new_value):
t = root.pop(1)
if len(t) > 1:
root.insert(1,[new_value,t,[]])
else:
root.insert(1,[new_value,[],[]])
return root
def insert_right(root,new_value):
t = root.pop(2)
if len(t) > 1:
root.insert(2,[new_value,[],t])
else:
root.insert(2,[new_value,[],[]])
return root
def get_root_val(root):
return root[0]
def set_root_val(root, new_val):
root[0] = new_val
def get_left_child(root):
return root[1]
def get_right_child(root):
return root[2]
def build_tree():
x = binary_tree('a')
insert_left(x,'b')
insert_right(x,'c')
insert_right(get_left_child(x),'d')
insert_left(get_right_child(x),'e')
insert_right(get_right_child(x),'f')
return x
print(build_tree())
"""
x = binary_tree('a')
insert_left(x,'b')
insert_right(x,'c')
print x
insert_right(get_right_child(x), 'd')
print x
insert_left(get_right_child(get_right_child(x)), 'e')
print x
# another tree
r = binary_tree(3)
print(r)
insert_left(r, 4)
insert_left(r, 5)
insert_right(r, 6)
insert_right(r, 7)
print(r)
l = get_left_child(r)
print(l)
set_root_val(l, 9)
print(r)
insert_left(l, 11)
print(r)
print(get_right_child(get_right_child(r)))"""