-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.py
More file actions
33 lines (27 loc) · 743 Bytes
/
tree.py
File metadata and controls
33 lines (27 loc) · 743 Bytes
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
class Node:
def __init__(self, data):
self.data = data
self.left_child = None
self.right_child = None
def preorder(root):
if root:
print(root.data)
preorder(root.left_child)
preorder(root.right_child)
def postorder(root):
if root:
postorder(root.left_child)
postorder(root.right_child)
print(root.data)
def inorder(root):
if root:
inorder(root.left_child)
print(root.data)
inorder(root.right_child)
if __name__ == "__main__":
root = Node(5)
root.left_child = Node(2)
root.right_child = Node(10)
preorder(root)
postorder(root)
inorder(root)