-
Notifications
You must be signed in to change notification settings - Fork 0
/
30_31_inorder_preorder_postorder.py
129 lines (105 loc) · 3.63 KB
/
30_31_inorder_preorder_postorder.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
class BinaryTree:
def __init__(self):
self.root = None
self.size = 0
# Return True if the element is in the tree
def search(self, e):
current = self.root # Start from the root
while current != None:
if e < current.element:
current = current.left
elif e > current.element:
current = current.right
else: # element matches current.element
return True # Element is found
return False
# Insert element e into the binary search tree
# Return True if the element is inserted successfully
def insert(self, e):
if self.root == None:
self.root = self.createNewNode(e) # Create a new root
else:
# Locate the parent node
parent = None
current = self.root
while current != None:
if e < current.element:
parent = current
current = current.left
elif e > current.element:
parent = current
current = current.right
else:
return False # Duplicate node not inserted
# Create the new node and attach it to the parent node
if e < parent.element:
parent.left = self.createNewNode(e)
else:
parent.right = self.createNewNode(e)
self.size += 1 # Increase tree size
return True # Element inserted
# Create a new TreeNode for element e
def createNewNode(self, e):
return TreeNode(e)
"""
# Return the size of the tree
def getSize(self):
return self.size"""
# Inorder traversal from the root
def inorder(self):
self.inorderHelper(self.root)
# Inorder traversal from a subtree
def inorderHelper(self, r):
if r != None:
self.inorderHelper(r.left)
print(r.element, end = " ")
self.inorderHelper(r.right)
# Postorder traversal from the root
def postorder(self):
self.postorderHelper(self.root)
# Postorder traversal from a subtree
def postorderHelper(self, root):
if root != None:
self.postorderHelper(root.left)
self.postorderHelper(root.right)
print(root.element, end = " ")
# Preorder traversal from the root
def preorder(self):
self.preorderHelper(self.root)
# Preorder traversal from a subtree
def preorderHelper(self, root):
if root != None:
print(root.element, end = " ")
self.preorderHelper(root.left)
self.preorderHelper(root.right)
# Return true if the tree is empty
def isEmpty(self):
return self.size == 0
# Remove all elements from the tree
def clear(self):
self.root == None
self.size == 0
# Return the root of the tree
def getRoot(self):
return self.root
class TreeNode:
def __init__(self, e):
self.element = e
self.left = None # Point to the left node, default None
self.right = None # Point to the right node, default None
####################### Main test binary tree
def main(size = 7):
int_list=input('Enter a list of integers seperated by spaces: ')
numbers =[int_list]
print ("\n\nInserting the following values:")
intTree = BinaryTree()
for e in numbers:
intTree.insert(e)
print("\nPreorder traversal:")
intTree.preorder()
print("\n\nInorder traversal:")
intTree.inorder()
print("\n\nPostorder traversal:")
intTree.postorder()
if __name__ == "__main__":
main()