0% found this document useful (0 votes)
2 views

Data sturcture and algorithm week 7

Uploaded by

hm896980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data sturcture and algorithm week 7

Uploaded by

hm896980
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT WEEK – 7

Name – HIMANSHU RAJ

Register No. - EA2331201010152

Program - B.C.A.-Data Science

Subject – Data Structure & Algorithm (DSA)

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.

Ans. Binary Tree Traversal Algorithms


In-order traversal:

• Visit the left subtree.


• Visit the root node.
• Visit the right subtree.

Pre-order traversal:

• Visit the root node.


• Visit the left subtree.
• Visit the right subtree.

Post-order traversal:

• Visit the left subtree.


• Visit the right subtree.
• Visit the root node.

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)

def pre_order(self, root):


if root is None:
return
print(root.data, end=" ") # You can replace print with any operation you want to
perform on the data
self.in_order(root.left)
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

2. Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty


binary search tree

Ans. Inserting elements into a Binary Search Tree (BST)


Given the elements 3, 1, 4, 6, 9, 2, 5, 7:

Here's the step-by-step process of inserting them into an initially empty BST:

1. Insert 3: The tree becomes (3).


2. Insert 1: As 1 is less than 3, it becomes the left child. The tree becomes (3, 1).
3. Insert 4: As 4 is greater than 3, it becomes the right child. The tree becomes (3, 1, 4).
4. Insert 6: As 6 is greater than 3, it becomes the right child of 3. The tree becomes (3, 1, 4, None,
None, 6).
5. Insert 9: As 9 is greater than 3 and all nodes in its right subtree (6 currently), it becomes the right
child of 6. The tree becomes (3, 1, 4, None, None, 6, None, 9).
6. Insert 2: As 2 is less than 3, it becomes the left child of 1. The tree becomes (3, 1(2), 4, None, None,
6, None, 9).
7. Insert 5: As 5 is greater than 3 and less than 6, it becomes the right child of 3. The tree becomes (3,
1(2), 4, 5, None, 6, None, 9).
8. Insert 7: As 7 is greater than 3, less than 6 and greater than all nodes in its left subtree, it becomes the
right child of 5. The tree becomes (3, 1(2), 4, 5(7), None, 6, None, 9).

Resulting Binary Search Tree:

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

You might also like