Data sturcture and algorithm week 7
Data sturcture and algorithm week 7
Semester – II Semester
EA2331201010152
1. Derive the Post-order, Pre-order and In-order traversal for the following
binary tree and write the algorithm for the above traversal.
Pre-order traversal:
Post-order traversal:
Algorithms for the Binary Tree Traversal (assuming a Node class with data and left/right child
pointers):
Python
def in_order(self, root):
if root is None:
return
self.in_order(root.left)
print(root.data, end=" ") # You can replace print with any operation you want to
perform on the data
self.in_order(root.right)
EA2331201010152
def post_order(self, root):
if root is None:
return
self.in_order(root.left)
self.in_order(root.right)
print(root.data, end=" ") # You can replace print with any operation
Here's the step-by-step process of inserting them into an initially empty BST:
3
/ \
1 4
/ \ \
2 5 6
\ \
7 9
Note: The in-order traversal of a BST will always result in a sorted list of elements. In this case, the in-order
traversal would be: 1, 2, 3, 4, 5, 6, 7, 9.
EA2331201010152