Data Structures Module 5 Complete Solutions
Data Structures Module 5 Complete Solutions
Q1) The integers 1-1000 are stored in a binary search tree BST.
Suppose The Search Algorithm Is Implemented On The Key 363,one of
the following sequences is not a possible sequence of nodes that is
examined. It is
i. 2, 252, 401, 398, 330, 344, 397, 363
ii. 924, 220, 911, 244, 898, 258, 362, 363
iii. 925, 202, 911, 240, 912, 345, 245, 363
iv. 2, 399, 387, 219, 266, 382, 381, 278, 363
A. Sequence Analysis
i. 2, 252, 401, 398, 330, 344, 397, 363
The sequences iii. 925, 202, 911, 240, 912, 345, 245, 363 and iv. 2,
399, 387, 219, 266, 382, 381, 278, 363 are not valid. However, among
the options provided, sequence iii is the first invalid one.
Therefore, iii. 925, 202, 911, 240, 912, 345, 245, 363 is the sequence
that is not a possible sequence of nodes examined when searching for
the key 363 in a BST.
Q2) If h is any hashing function and is used to hash n keys into a table
of size m, where m>=n, the expected number of collisions involving a
particular key x is :
A. If h is chosen from a universal collection of hash functions and is
used to hash n keys into a table of size m, where n ≤ m, the expected
number of collisions involving a particular key x is less than 1.
Q3) Consider a hash table with slots.The hash function is h(k)=k mod 9.
The Collisions are resolved by chaining. The following 9 keys are
inserted in the order: 5, 28, 19, 15, 20, 33,12, 17, 10. Find the maximum,
minimum and average chain length in the hash table?
Q4) A binary search tree contains the numbers 1, 2, 3, 4, 5, 6, 7, 8.
When the tree is traversed in pre-order and the values in each node
printed out, the sequence of values obtained is 5, 3, 1, 2, 4, 6, 7, 8. Find
the post order traversal sequence of the tree?
A. The tree can be constructed uniquely from the inorder and preorder
traversal of the tree. The inorder traversal gives the sorted list i.e
1,2,3,4,5,6,7,8. So the unique BST is
Q5) A hash table contains 10 buckets and uses linear probing to resolve
collisions. The key values are integers and the hash function used is key
% 10. If the values 43, 165, 62, 123, 142 are inserted in the table, then
find the location of the key value 142 in the table?
Q6) Find the smallest number of keys that will force a B-tree of order 3
to have a height 2?
A. AB-tree of order m of height h will have the maximum number of keys
when all nodes are completely filled. So, the B-tree will have n = (mh+1–
1 keys in this situation. Therefore, the smallest number of keys is equal
to 26.
Q7) Suppose that the computer you will be using has disk blocks holding
4096 bytes, the key is 4 bytes long, each child pointer (which is a disk
block id)is 4 bytes,theparentis4byteslongand the data record reference
(which is a disk block id along with a offset within the block) is 8 bytes.
You have an application in which you want to store 1,000,000 items in
your B-tree. What value would you select for t? Show how you derived
it.) What is the maximum number of disk pages that will be brought to
main memory during a search? Remember that the root is kept in main
memory at all times.
A. We want to select so that a full node uses as much of a disk block as
possible. In a full node, there are 2t−1 keys 4 bytes each), 2t−1 data
record references 8 bytes each), 2t child pointers 4 bytes each), a parent
pointer 4 bytes), the number of keys 4 bytes) and the leaf bit (which we’ll
go ahead and assume takes 4 bytes through 1 bit would do). Hence we
want topicktas large as we can so that 122t−1 + 4(2t)+12 = 32t≤4096.
Solving for yields that we needt= 128. In class, we argued that the
number of disk pages that must be read (d-1using the notation from
class) is atmos log t(n+1)/2. Since log128(n+1)/2=log 128 2.7 and the
number of levels below the root must be an integer, at most 2 disk pages
will need to be brought into main memory during a search.
Q8) Show the B-tree such that the results when inserting elements
R,Y,F,X,A,M,C,D,E,T,H,V,L,W,G (in that order) branching factor of t=3.
You need only draw the trees just before and after each split.
A.
Q2) List out the operations of a binary search tree and write the
procedure to search for a key 45 in a given binary search tree containing
elements 25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24, 31, 44, 66, 90?
A. Basic operations on a BST Create: creates an empty tree. Insert:
insert a node in the tree. Search: Searches for a node in the tree.
Delete: deletes a node from the tree. Inorder: in-order traversal of the
tree. Preorder: pre-order traversal of the tree. Postorder: post-order
traversal of the tree.
Create
Initially an empty tree without any nodes is created. The
variable/identifier which must point to the root node is initialized with a
NULL value.
Search
You always start searching the tree at the root node and go down from
there. You compare the data in each node with the one you are looking
for. If the compared node doesn't match then you either proceed to the
right child or the left child, which depends on the outcome of the
following comparison: If the node that you are searching for is lower than
the one you were comparing it with, you proceed to the left child,
otherwise (if it's larger) you go to the right child. Why? Because the BST
is structured (as per its definition), that the right child is always larger
than the parent and the left child is always lesser.
Breadth-first search BFS Breadth-first search is an algorithm used to
traverse a BST. It begins at the root node and travels in a lateral manner
(side to side), searching for the desired node. This type of search can be
described as O(n) given that each node is visited once and the size of
the tree directly correlates to the length of the search.
Depth-first search DFS With a Depth-first search approach, we start with
the root node and travel down a single branch. If the desired node is
found along that branch, great, but if not, continue upwards and search
unvisited nodes. This type of search also has a big O notation of O(n).
Insert
It is very similar to the search function. You again start at the root of the
tree and go down recursively, searching for the right place to insert our
new node, in the same way as explained in the search function. If a
node with the same value is already in the tree, you can choose to either
insert the duplicate or not. Some trees allow duplicates, some don't. It
depends on the certain implementation.
Deletion There are 3 cases that can happen when you are trying to
delete a node. If it has,
No subtree (no children): This one is the easiest one. You can simply
just delete the node, without any additional actions required. One
subtree (one child): You have to make sure that after the node is
deleted, its child is then connected to the deleted node's parent. Two
subtrees (two children): You have to find and replace the node you want
to delete with its inorder successor (the leftmost node in the right
subtree).
Q3) Write the procedure for inserting an element 60 in a given binary
search tree containing elements 25, 15, 50, 10, 22, 35, 70, 4, 12, 18, 24,
31, 44, 66, 90?
A.
.
Q4) Explain the different possibilities that arise while deleting an
element from a given binary search tree containing elements 50, 30, 70,
20, 40, 60, 80? i. Delete 20 ii. Delete 30 iii. Delete 50
A.
Q5)Define an AVL tree and write the steps used to follow while inserting
an element 3into angivenAVLtreecontainingelements 13, 10, 15, 5, 11,
16, 4, 8
A.
To insert an element in the AVL tree, follow the following steps-
● Insert the element in the AVL tree in the same way the insertion is
performed in BST.
● After insertion, check the balance factor of each node of the resulting
tree. Now, following two cases are possible
Case-01
● After the insertion, the balance factor of each node is either 0 or 1 or
1.
● In this case, the tree is considered to be balanced.
● Conclude the operation.
● Insert the next element if any.
Case-02
● After the insertion, the balance factor of at least one node is not 0 or 1
or 1.
● In this case, the tree is considered to be imbalanced.
● Perform the suitable rotation to balance the tree.
● After the tree is balanced, insert the next element if any.
After Insertion
The difference between the heights of the left and right subtree is 1,
which means there is no need of rotation for balancing the AVL Tree.
Both the AVL Tree and the BST Tree in this example are identical.
Q8) Write the procedure for insertion and deletion operation in a B tree
with the following elements 10, 20, 30, 40, 50, 60, 70, 80, 90.
A. Insertion in B-Tree
1. Insert in Leaf Node:
○ If the tree is empty, create a root node and insert the key.
○ Otherwise, find the appropriate leaf node where the key should
be inserted.
2. Handle Overflow:
○ If the node has space, insert the key in sorted order.
○ If the node is full, split it into two nodes:
■ Move the median key up to the parent node.
■ Split the keys into two separate nodes.
3. Repeat:
○ Repeat the process up the tree if necessary, splitting the parent
nodes as needed.
○ If the root node is split, create a new root.
Deletion in B-Tree
Step-by-Step Insertion:
1. Insert 10:
○ Tree: [10]
2. Insert 20:
○ Tree: [10, 20]
3. Insert 30:
○ Tree: [10, 20, 30]
4. Insert 40:
○ Tree: [10, 20, 30, 40]
5. Insert 50:
○ Tree: [10, 20, 30, 40, 50] (Node full, needs to split)
○ Split: 30 moves up
○ Tree:
[30]
/ \
[10,20] [40,50]
Insert 60:
● Tree:
[30]
/ \
[10,20] [40,50,60]
Insert 70:
● Tree:
[30]
/ \
[10,20] [40,50,60,70] (Node full, needs to split)
Split: 50 moves up
Tree:
[30, 50]
/ | \
[10,20] [40] [60,70]
Insert 80:
● Tree:
[30, 50]
/ | \
[10,20] [40] [60,70,80]
Insert 90:
● Tree:
[30, 50]
/ | \
[10,20] [40] [60,70,80,90] (Node full, needs to split)
Split: 70 moves up
Tree:
[30, 50, 70]
/ | | \
[10,20] [40] [60] [80,90]
Step-by-Step Deletion:
For example, to delete key 70:
1. Delete 70:
○ 70 is in the root.
○ Replace 70 with the largest key in the left subtree (60).
○ Tree after deletion:
[30, 50, 60]
/ | | \
[10,20] [40] [ ] [80,90]
Q13) Show the B-tree the results when deleting A, then deleting V and
then deleting P from the following B-tree with a minimum branching
factor of t=2
A.
Q14) Which of the following are legal B-trees for when the minimum
branching factor t = 3? For those that are nolegal,give one or two
sentences very clearly explaining what property was violated?
A. i): Not legal since the height is not balanced. More specifically, both
the nodes with “BD” and “KS” are at the same level but “BD” is a leaf
and “KS” is not.
(ii): This is legal. Remember, that the root can have just a single key.
(iii): Not legal– the key “D” has less than the minimum allowable size of
2 keys.
(iv): This is legal.
(v): Not legal– there’s no leaf node corresponding to the keys between
G and L.
Q15) Create a binary search tree for the following elements 23, 32, 24,
36, 15, 12, 39, 2, 19. Discuss about the height of the above binary
search tree.
A.
Q16)Explain with examples different cases of deletion of elements in a
binary search tree?
A. There are three possible cases to consider deleting a node from BST:
Case :1 Deleting a node with no children: remove the node from the
tree.
Case 2 :Deleting a node with two children: call the node to be deleted N.
Do not delete N. Instead, choose either its in order successor node or its
in order predecessor node, R. Copy the value of R to N, then recursively
call delete on R until reaching one of the first two cases. If we choose
the in-order successor of a node, as the right subtree is not NULL (our
present case is a node with 2 children), then it's in order successor is a
node with the least value in its right subtree, which will have at a
maximum of 1 subtree, so deleting it would fall in one of the first 2 cases.
Case 3 Deleting a node with one child: remove the node and replace it
with its child.
Q17)Explain how M-way search trees differ from binary search trees with
an example
A. An m-way search tree is a m-way tree in which: Each node has m
children and m-1 key fields The keys in each node are in ascending
order. The keys in the first i children are smaller than the I th key the
keys in the last m-i children are larger than the ith key
An extension of a multiway search tree of order m is a B-tree of order m.
This type of tree will be used when the data to be accessed/stored is
located on secondary storage devices because they allow for large
amounts of data to be stored in a node.
A B-tree of order m is a multiway search tree in which:
The root has at least two subtrees unless it is the only node in the tree.
Each non root and each non leaf node has at most m nonempty children
and at least m/2 nonempty children. The number of keys in each non
root and each non leaf node is one less than the number of its nonempty
children. All leaves are on the same level.
Q18) Construct a M-way search tree of order 3 for the following nodes
20,70,110,210,130
A.
We will insert the keys one by one and calculate their hash values:
1. Key 5:
h(5)=5 mod 5=0 h(5) = 5 \mod 5 = 0
Insert into slot 0.
2. Key 28:
h(28)=28 mod 5=3h(28) = 28 \mod 5 = 3
Insert into slot 3.
3. Key 19:
h(19)=19 mod 5=4 h(19) = 19 \mod 5 = 4
Insert into slot 4.
4. Key 15:
h(15)=15 mod 5=0 h(15) = 15 \mod 5 = 0
Insert into slot 0 (chain with 5).
5. Key 20:
h(20)=20 mod 5=0 h(20) = 20 \mod 5 = 0
Insert into slot 0 (chain with 5, 15).
6. Key 33:
h(33)=33 mod 5=3h(33) = 33 \mod 5 = 3
Insert into slot 3 (chain with 28).
7. Key 12:
h(12)=12 mod 5=2h(12) = 12 \mod 5 = 2
Insert into slot 2.
8. Key 17:
h(17)=17 mod 5=2h(17) = 17 \mod 5 = 2
Insert into slot 2 (chain with 12).
9. Key 10:
h(10)=10 mod 5=0 h(10) = 10 \mod 5 = 0
Insert into slot 0 (chain with 5, 15, 20).
Hash Table Structure
1. Insert 43:
hash(43)=43 mod 15=13\text{hash}(43) = 43 \mod 15 = 13
Insert 43 at index 13.
2. Insert 165:
hash(165)=165 mod 15=0\text{hash}(165) = 165 \mod 15 = 0
Insert 165 at index 0.
3. Insert 62:
hash(62)=62 mod 15=2\text{hash}(62) = 62 \mod 15 = 2
Insert 62 at index 2.
4. Insert 123:
hash(123)=123 mod 15=3\text{hash}(123) = 123 \mod 15 = 3
Insert 123 at index 3.
5. Insert 142:
hash(142)=142mod 15=7\text{hash}(142) = 142 \mod 15 = 7
Insert 142 at index 7.