-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.py
66 lines (57 loc) · 1.13 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Node(object):
def __init__(self, value, left_child = None, right_child = None):
self.value = value
self.left = left_child
self.right = right_child
self.visited = False
def visit(self):
print(self.value, end = " ")
self.visited = False
def inorder(node: Node):
if node is not None:
inorder(node.left)
node.visit()
inorder(node.right)
def preorder(node: Node):
if node is not None:
node.visit()
preorder(node.left)
preorder(node.right)
def postorder(node: Node):
if node is not None:
postorder(node.left)
postorder(node.right)
node.visit()
def main():
inputs = [
Node(8,
Node(4,
Node(2),
Node(6)
),
Node(10,
None,
Node(20)
)
),
Node(10,
Node(5,
Node(3),
Node(7)
),
Node(20,
None,
Node(30)
)
),
]
for root_node in inputs:
print("Inorder:", end = " ")
inorder(root_node)
print()
print("Pre-order:", end = " ")
preorder(root_node)
print()
print("Post-order:", end = " ")
postorder(root_node)
print()