-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree Traversal.py
39 lines (29 loc) · 1.13 KB
/
Tree Traversal.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
def prefix(list,i):
node = list[i]
print("index:" + str(i) + " value:"+str(node)) #can modify to return value in list
left_node = i*2+1
right_node = i*2+2
if left_node > len(list) or right_node > len(list):
return
prefix(list,left_node)
prefix(list,right_node)
def infix(list,i):
node = list[i]
left_node = i * 2 + 1
right_node = i * 2 + 2
if left_node > len(list) or right_node > len(list):
print("index:" + str(i) + " value:" + str(node)) #can modify to return value in list
return
infix(list, left_node)
print("index:" + str(i) + " value:" + str(node)) #can modify to return value in list
infix(list, right_node)
def postfix(list,i):
node = list[i]
left_node = i * 2 + 1
right_node = i * 2 + 2
if left_node > len(list) or right_node > len(list):
print("index:" + str(i) + " value:" + str(node)) #can modify to return value in list
return
postfix(list, left_node)
postfix(list, right_node)
print("index:" + str(i) + " value:" + str(node)) #can modify to return value in lists