-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.py
48 lines (36 loc) · 912 Bytes
/
BinaryTree.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
class Node:
def __init__(self,key):
self.left=None
self.right = None
self.val = key
#Traverse preorder
def traversePreOrder(self):
print(self.val,end='')
if self.left:
self.left.traversePreorder()
if self.right:
self.right.traversePreOrder()
#Traverse Order
def traverseInorder(self):
if self.left:
self.left.traverseInorder()
print(self.val,end='')
if self.right:
self.right.traverseInorder()
#Traverse postorder
def traversePostOrder(self):
if self.left:
self.left.traversePostOrder()
if self.right:
self.right.traversePostOrder()
print(self.val,end='')
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
print('Pre Order Traversal',end = '')
root.traversalPreOrder()
print('\n Inorder Traversal:',end='')
root.traverseInorder()
print('\n Post order Traversal',end='')
root.traversePostOrder()