Assignment 4 - Technische Universiteit Eindhoven

technische universiteit eindhoven
2IL50 Data Structures
Assignment 4
Spring 2015
Due at 23:59 on 08–03–2015
Requirements: Assignments have to be handed in by each student separately. Please write
your name and student number on the top of the first sheet that you hand in. Remember
that assignments have to be typeset in English and sent by email as .pdf to your instructor
on the due date. Always justify your answers!
Note: Whenever you are asked to describe an algorithm, you should present three things:
the algorithm, a proof of its correctness, and a derivation of its running time. Do not use
pseudo-code in your answers unless you are explicitly asked to do so.
Exercise 1:
(a) [3 points] Use the hash function h(k, i) = ( h0 (k)+3i+i2 ) mod 11 with h0 (k) = k mod 13
to insert the numbers 13, 23, and 40 into this hash table:
0
1
27
2
3
4
5
6
18
7
8
9
10
10
(b) [2 points] We want to store a set S of n numbers in a hash table T [0..m − 1] of size
m and we are choosing m = n + 2. Should we resolve collisions with chaining or open
addressing? Explain your answer.
(c) [2 points] Assume that we are using a hash table of size m. Show that if the universe
U has size |U | > nm, there is a subset of U of size n consisting of keys that all hash to
the same slot, so that the worst-case searching time for hashing with chaining is Θ(n).
Exercise 2: Suppose you are given an array A of n integers and you can use a hash table T of
size n. For this question you may assume the existence of a hash function h : Z → {1, . . . , n}
with simple uniform hashing.
(a) [1 point] Describe an algorithm that determines whether there are two equal numbers
in A in O(n) time on average.
(b) [2 points] Now suppose you are also given a positive integer k. Describe an algorithm
that determines whether there exist two numbers x and y in A such that |x − y| ≤ k.
Again, this algorithm must run in O(n) time on average (and should not depend on k).
(Hint: consider hashing bx/kc instead of x for every x.)
Exercise 3:
(a) [2 points] Insert items with the following keys (in the given order) into an initially empty
binary search tree: 23, 27, 12, 42, 34, 19, 5, 7. Draw the tree after any two insertions.
The best way to draw figures for inclusion in LATEX documents is to use ipe.
/department of mathematics and computer science
1
technische universiteit eindhoven
2IL50 Data Structures
(b) [2 points] Suppose that we have numbers between 1 and 1000 in a binary search tree
and want to search for the number 363. Which of the following sequences could not be
the sequence of nodes examined? Why?
(a)
(b)
(c)
(d)
(e)
2, 252, 401, 398, 330, 344, 397, 363.
924, 220, 911, 244, 898, 258, 362, 363.
925, 202, 911, 240, 909, 245, 363.
2, 399, 387, 219, 266, 382, 381, 257, 363.
935, 278, 347, 621, 299, 392, 358, 363.
(c) [1 point] Is the operation of deletion “commutative” in the sense that deleting x and
then y from a binary search tree leaves the same tree as deleting y and then x? Argue
why it is or give a counterexample.
(d) [2 points] What is the difference between the binary-search-tree property and the minheap property? Can the min-heap property be used to print out the keys of an n-node
tree in sorted order in O(n) time? Explain how or why not.
Exercise 4: [3 points] You are given an array A of n distinct integers and a binary search
tree T with n internal nodes of which the keys of the nodes have been removed. Describe
an O(n log n) algorithm that puts all integers of A into T (as keys) such that T becomes a
correct binary search tree (see the figure below; the leaves of T have not been drawn). You
may use the structure of the tree as described on slide 8 of Lecture 7, but your algorithm may
not change the structure of the tree.
13
T
19
5
2
8
16
22
A 13 8 22 16 5 2 19
2
/department of mathematics and computer science