You are on page 1of 2

Data Structures and Algorithms

INSOFE and NMIMS


Instructor: Prof. Ashwin Ganesan

Practice Problems: Hash Tables and Binary Search


Trees

1 Hash tables
1. Suppose that a dynamic set S is represented by a direct-address table T
of length m. Describe a procedure that finds the maximum element of S.
What is the worst-case performance of your procedure? [CLRS, Ex.
11.1-1]

2. A bit vector is simply an array of bits (0’s and 1’s). A bit vector of
length m takes much less space than an array of m pointers. Describe
how to use a bit vector to represent a dynamic set of distinct elements
with no satellite data. Dictionary operations (i.e SEARCH, INSERT and
DELETE) should run in O(1) time. [CLRS, Ex. 11.1-2]

3. Demonstrate what happens when we insert the keys

5, 28, 19, 15, 20, 33, 12, 17, 10

into a hash table with collisions resolved by chaining. Let the table have 9
slots, and let the hash function be h(k) = k mod 9. Your answer should
show the final hash table. [CLRS, Ex. 11.2-2]
4. Suppose we insert the keys 5, 14, 21, 6, 1, 19, 33, 9, 16 into a hash table with
collisions resolved by chaining. Let the table have 11 slots, and let the
hash function be h(k) = k mod 11. For each slot, give the elements that
hash to that slot.

2 Binary search trees (BST’s)


5. For the set of {1, 4, 5, 10, 16, 17, 21} of keys, draw binary search trees of
heights 2, 3, 4, 5 and 6. [CLRS, Ex. 12.1-1]

6. Draw all possible binary search trees containing the four elements 1, 2, 3
and 4. [AHU, Ex. 5.1]
7. Insert the integers 7, 2, 9, 0, 5, 6, 8, 1 into a binary search tree. Show the
final binary search tree. [AHU, Ex. 5.2]
8. Using asymptotic notation, give an upper bound for the height of a binary
search tree containing n nodes.

Page 1 of 2
9. Given an initially empty binary search tree, give an insertion order for
1,2,3,4,5,6,7 that minimizes height.

10. When the entries 7, 4, 6, 1, 2, 3, 8, 5 are successively inserted into an


initially empty binary search tree, what is the height of the resulting tree?
11. The height of a tree is the maximum number of edges from the root to
a leaf. What is the minimum and maximum possible height of a binary
search tree with 15 nodes?

12. The preorder traversal sequence of a binary search tree is 30, 20, 10, 15,
25, 23, 39, 35, 42. What is the postorder traversal sequence of the same
tree?
13. We would like to check if a given binary tree is a binary search tree.
Consider the following greedy algorithm: simply traverse the tree, and
for each node x, just check if the value at x is larger than the value at
x.leftchild and smaller than the value at x.rightchild.
Given an example of a binary tree for which the above greedy algorithm
fails. [Wikipedia]
14. *Give an algorithm that checks if a given binary tree is a binary search tree.
[Hint: Write a function IsBST (T, min, max) that takes as its input a
pointer T to the root node of a binary tree and two further arguments. The
function can make recursive function calls IsBST(T.leftchild, min, T.element)
and IsBST(T.rightchild, T.element, max). The main() function can
make a call IsBST(T, INT_MIN, INT_MAX). ] [Wikipedia]

15. Starting with am empty binary search tree, insert the following elements in
the given order: 40, 60, 50, 33, 53, 12. Your answer should show the binary
search tree after each new number is inserted.
16. (a) Insert the following sequence of numbers into an initially empty bi-
nary search tree: 6, 3, 7, 1. Your answer should show the binary search
tree after each new number is inserted.
(b) Starting with the binary search tree constructed in (a), delete 7.

Page 2 of 2

You might also like