-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary Tree with Travelsels.py
55 lines (41 loc) · 1.23 KB
/
Binary Tree with Travelsels.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
class _Node:
__slots__ = '_element','_left','_right'
def __init__(self,element, left=None,right=None) ->None:
self._element = element
self._left = left
self._right = right
class BinaryTree:
def __init__(self):
self._root = None
def maketree(self, e,left,right):
self._root = _Node(e,left._root,right._root)
def inorder(self, troot):
if troot:
self.inorder(troot._left)
print(troot._element,end=' ')
self.inorder(troot._right)
def preorder(self, troot):
if troot:
print(troot._element,end=' ')
self.preorder(troot._left)
self.preorder(troot._right)
def postorder(self, troot):
if troot:
self.postorder(troot._left)
self.postorder(troot._right)
print(troot._element,end=' ')
x = BinaryTree()
y = BinaryTree()
z = BinaryTree()
a = BinaryTree()
x.maketree(20,a,a)
y.maketree(30,a,a)
z.maketree(10,x,y)
print("Inorder Traversal")
z.inorder(z._root)
print()
print("Preorder Traversal")
z.preorder(z._root)
print()
print("Postorder Traversal")
z.postorder(z._root)