-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.py
More file actions
53 lines (40 loc) · 1.02 KB
/
binary_tree.py
File metadata and controls
53 lines (40 loc) · 1.02 KB
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
class Node:
def __init__(self,value):
self.value = value
self.left = None
self.right = None
def preorder(object):
if object== None:
return None
if root:
preorder(object.left)
print object.value
preorder(object.right)
def inorder(object):
if object== None:
return None
if root:
print object.value
inorder(object.left)
inorder(object.right)
def postorder(object):
if object== None:
return None
if root:
postorder(object.left)
postorder(object.right)
print object.value
if __name__ =="__main__":
root = Node(15)
root.left = Node(1)
root.right = Node(37)
root.left.left = Node(61)
root.left.right = Node(26)
root.right.left = Node(59)
root.right.right = Node(48)
print "Preorder Traverse"
preorder(root)
print "Inorder Traverse"
inorder(root)
print "Postorder Traverse"
postorder(root)