CS2233 Data Structure Assignment (2) - 1
CS2233 Data Structure Assignment (2) - 1
Max Marks 45
1 Coding Problems:
1. Construct an B-tree.
The B-tree should be constructed by calling the insert (root, key)
listed below. Each node of the tree should use the following struct data
type:
struct BTreeNode
{
1
int key[MAX + 1], count;
/* count stores the number of keys in the current node */
struct BTreeNode *children[MAX + 1];
};
Additionally, there are two variables MAX and MIN denoting the minimum
and maximum number of elements in a node, which is given as input. You
can assume that you have stored the pointer to the root node. Please,
write the functions for:
(a) search (root, key) – this function takes the pointer to the root
node, and key as input, and returns the pointer to the node where
key is present. If key is not present in the B-tree, then the code
should output an error message.
(b) insert (root, key) – this function takes the pointer to the root
node, and key as input, and inserts the node at the appropriate
position.
(c) delete (root, key) – this function takes the pointer to the root
node, and the key as input, and deletes the corresponding node.
(d) In points b, and c, the output should be the tree obtained after node
insertion/deletion. Please output the tree by printing the nodes level-
by-level.
2+3+3+2=10 Marks
Input format
• First line contains three space-separated integers n, M AX, M IN ,
which indicates the number of elements to be inserted into the tree,
minimum and maximum number of elements in a node, respectively.
• Second line contains n one-space-separated integers, which are tree
elements, where you should insert the elements according to the given
order.
• Next line contains k, which indicates number of queries.
• Next k line contains two space-separated integers where the first inte-
ger denotes the query and the second integer denotes the input value
corresponding to this query.
• Queries - Each line contains 2 integers, separated by space character.
1 for Search.
2 for Insert.
3 for Delete.
4 for Displaying the tree in level order.
2
– 1 112 =⇒ search for 112 in the tree. Output - “112 is present”
/ “112 is not present” if found / not-found
– 2 100 =⇒ insert 100 into the tree. If the element 100 is already
present, then print “100 is already present”. Otherwise, it should
be inserted and output print “100 is inserted”.
– 3 100 =⇒ delete 100 if present and output print “100 is deleted”.
Otherwise, print “100 is not present. So it can not be deleted”.
– 4 =⇒ Print the tree level by level. Each level must be printed
in each new line. Elements of the same node should be separated
by ‘,’ and a space should separate every node.
Example:
Input and Output:
17 3 1
9 8 5 4 99 78 31 34 89 90 21 23 45 77 88 112 32
8
1 56
56 is not present
(Output for this query. From now on, here, blue text represents the ex-
pected output)
2 21
21 is already present
2 56
56 is inserted
2 90
90 is already present
3 51
51 is not present. So it can not be deleted
1 32
32 is present
3 32
32 is deleted
4
9,34
5 23 77,89
4 8 21 31 45,56 78,88 90,99,112
3
(c) Find the k th smallest element in the tree, where k is an integer pro-
vided as input (1-based indexing).
(d) Find the rank of a given value in the tree (i.e., how many elements
are smaller than the given value).
2.5 × 4 = 10 Marks
Input format
• First line contains n, which indicates the number of elements to be
inserted into the tree.
• Second line contains n one-space-separated integers, which are tree
elements, where you should insert the elements according to the given
order.
• Next line contains k, which indicates number of queries.
• Next k line contains two space-separated integers where the first inte-
ger denotes the query and the second integer denotes the input value
corresponding to this query.
• Queries - Each line contains 2 integers, separated by space character.
1 for Insert.
2 for Delete.
3 for Find the k th smallest element.
4 for Find the rank.
Example:
Input and Output:
17
9 8 5 4 99 78 31 34 89 90 21 23 45 77 88 112 32
4
1 56
56 is inserted
2 51
51 is not present. So it can not be deleted
35
4
21
4 23
5
Output format
• Your output also should contain k lines, which indicates the output
of corresponding k enumerate queries.
• Note: Output the keys in sorted order as given in below example.
Example:
Input:
17
9 8 5 4 99 78 31 34 89 90 21 23 45 77 88 112 32
5
34 88
9 78
23 112
77 99
4 34
Output:
34 45 77 78 88
9 21 23 31 32 34 45 77 78
5
23 31 32 34 45 77 78 88 89 90 99 112
77 78 88 89 90 99
4 5 8 9 21 23 31 32 34
2 Theory questions:
(a) Let us define a relaxed red-black tree as a binary search tree that
satisfies the following properties. In other words, the root may be
either red or black. Consider a relaxed red-black tree T whose root
is red. If we color the root of T black but make no other changes to
T, is the resulting tree a red-black tree?
i. Every node is either red or black.
ii. Every leaf (NIL) is black.
iii. If a node is red, then both its children are black.
iv. For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes.
5 Marks
(b) Show that the longest simple path from a node x in a red-black tree
to a descendant leaf has a length at most twice that of the shortest
simple path from node x to a descendant leaf. 5 Marks
(c) Let T be a (2, 4) tree. Consider the SPLIT operator that takes the
root of a (2, 4) tree T , and a node x ∈ T as input, and creates a
(2, 4) tree T1 consisting of all elements in T − {x} whose keys are less
than key[x], and another (2, 4) tree T2 that consist of all elements in
T − {x} whose keys are greater than key[x].
Given a O(log n) time algorithm for SPLIT operator. Also, write
a theorem which proves that your algorithm is correct (that is, it
outputs the desired result) and its running time is O(log n). 2+3=5
Marks
Instructions on theory problems: Please neatly write the solu-
tion using pen-paper 1) to give a proof of the algorithm in Question
5 in Section 1, and 2) Questions 1 and 2 of this section; and submit
the scan copy in the google classroom page.