0% found this document useful (0 votes)
11 views3 pages

Assignment-2 DSA

Uploaded by

divyajainxyz723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Assignment-2 DSA

Uploaded by

divyajainxyz723
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 2 Divya Jain

06320802821
ECE-B

Q1) The following list of numbers is inserted into an empty binary search tree (BST): 45, 32, 90,
34, 68, 72, 15, 24, 30, 66, 11, 50, 10.
(a) Construct the Binary Search Tree (BST):
A BST is constructed by inserting nodes based on the rule:
 Smaller values go to the left of the parent.
 Larger values go to the right of the parent.
Resulting Tree Structure:
45
/ \
32 90
/ \ /
15 34 68
/ \ / \
11 24 50 72
/ \
10 30
\
66

(b) Find the Inorder, Preorder, and Postorder Traversals of the BST:
 Inorder (Left, Root, Right): 10, 11, 15, 24, 30, 32, 34, 45, 50, 66, 68, 72, 90
 Preorder (Root, Left, Right): 45, 32, 15, 11, 10, 24, 30, 34, 90, 68, 50, 66, 72
 Postorder (Left, Right, Root): 10, 11, 30, 24, 15, 34, 32, 66, 50, 72, 68, 90, 45

Q2) Find the depth of a complete tree if the number of nodes is 1,000,000.
 Formula for depth of a complete binary tree: Depth=⌈log2(Number of nodes)⌉
log2(1,000,000)≈19.93, so rounding up gives a depth of 20.

Q3) Construct the binary tree T from the given sequence:


Preorder: ABDGCEHIF
Inorder: DGBAHEICE
Steps to Construct the Tree:
1. The first node in preorder (A) is the root.
2. Use inorder to split the left and right subtrees:
o Left subtree: DGBAH
o Right subtree: EICE
3. Repeat recursively for each subtree.
Resulting Tree:
A
/ \
B C
/ \ /\
D GE I
\ \
H F

Q4) Construct a Max Heap from the following keys: 11, 6, 2, 3, 8, 10, 13.
Steps to Construct the Max Heap:
1. Insert keys one by one while maintaining the max-heap property.
2. At each step, compare the inserted key with its parent and swap if necessary.
Resulting Max Heap:
13
/ \
11 10
/ \ / \
3 8 2 6

Q5) Construct the binary tree T using given traversals:


Inorder: BGDKHAEICJF
Postorder: GKHDBEIJFCA
Steps to Construct the Tree:
1. The last node in postorder (A) is the root.
2. Use inorder to split the left and right subtrees:
o Left subtree: BGDKH
o Right subtree: EICJF
3. Repeat recursively for each subtree.
Resulting Tree:
A
/ \
B E
/\ /\
G D I C
/\ /
K H J

You might also like