CS140: Homework 11
Do not be intimidated by the number of exercises in this homework. Most of
them require fairly short answers and can be performed quite quickly.
- Suppose I want to create a
searching application which supports the
following three operations:
- insert a record into the search structure
- find a record in the search structure
- delete a record from the search structure
Further suppose that:
- the data is inserted almost in ascending order (i.e.,
the data is not completely ordered, but is close to
- the number of insertions and deletions roughly equals the
number of finds.
- ordered traversals and find min/find max operations are never
Which data structure, a linked list, unbalanced tree, balanced tree,
or hash table, would you choose to implement this search
application? Justify your answer.
- Suppose I have a data set with an estimated size of 5000 elements and
I want to insert the elements into a hash table. What
tablesize should I choose if I want the tablesize to be:
- a prime number
- one less than the first greater power of 2
- Show the hash table that results
if the integers 22, 28, 17, 21, 32 are inserted
into the following hash table using:
- Linear probing, and
- the hash function h(k) = k % 11
- Show the hash table that results if the integers 22, 28, 17, 21, 32 are
inserted into the following hash table using:
You should append integers to the end of your lists and you may
show your lists as comma separated lists of integers.
- separate chaining, and
- the hash function h(k) = k % 11
- Exercise 5.1 in the Weiss text. Use the above representation for your
aways and represent linked lists as comma-separated lists of integers.
Only do separate chaining, linear probing, and quadratic probing.
- Weiss 6.2a
- Weiss 6.3
The rest of this homework assignment asks you to write a number of functions involving
bit operations. You do not have to compile or execute the functions and
you may assume that the parameters are error-free. All of the function bodies
should requires only a few lines of code. Three of them can be done with
a single line of code.
- Write a function that takes a non-negative integer, n,
as a parameter and that returns 2n as the result.
Your function should consist of a single line of code and hence
will need to make use of the bit operations you learned about in
- In class I indicated that one common way to choose a table size for
a hash table is to find an integer which is the smallest power
of two greater than the estimated number of elements in the
data set and then to subtract one from it. In other words, if n
is the estimated number of elements in the data set, determine
the smallest x such that n < 2x and then
use 2x-1 as your table size. For example, if
n is 11, then x is 4 and the table size should be
24-1 = 15.
Write a function that takes a non-negative integer, n,
as a parameter and returns 2x-1 as the result.
if n is 11, your function should return 15. If it is 32 your function
should return 63. Although you can
solve this problem by writing a for loop that repeatedly divides
n by 2, I want you to solve the problem using bit shifting.
It is legitimate to divide a number by 2 by bit-shifting it.
Hint: Think of n as a string of bits. For example, 13
can be represented as 1101 using bits. If bit positions are numbered
from right to left with the first bit position being numbered 1,
then x is equal to the bit position of the last 1 in the
string. In the above example the last 1 will be in bit position 4.
- Write a function that takes an unsigned integer, n, and two
non-negative integers, left, and right as parameters.
Your function should pretend that n is a bit string and
print the bits between and including left and right.
You should assume that bit positions are numbered from 31 to 0.
For example, if n can be represented as
10110111011000011101011011010010 and left is 29 and
right is 23, your function should print "1101110":
position: 2 2
Hint: Use bit-shifting. In addition to bit-shifting, you may
need to use bitwise-and (see the lecture notes on bits), a stack, or
recursion in order to print the bits in the correct order. Your solution
might use none of these three techniques but I thought of three
separate solutions, each of which uses one of these techniques.