COP 3530 – CS3 Fall 1999
Exam # 1
Name:
Key
10
1.
Consider an Abstract Data Type,
IntBag
, defined by the following protocol
public IntBag( )
– constructs the
IntBag
with an empty state.
We will assume that each state item keeps
track of a pair  (value, repeat count).
public int insert(int x)
– adds an integer
x
to the
IntBag
.
Duplicates are allowed.
Returns the number of
times this integer appears in the
IntBag
.
(This count includes the instance we just added.)
public int remove(int x)
– removes an integer
x
from the
IntBag
.
An attempt to remove an item not in the
IntBag
results in no changes.
Returns the remaining number of times this integer appears in the
IntBag
.
public int howMany(int x)
– Returns the number of times
x
appears in the
IntBag
.
public boolean thisMany(int count)
– Returns true if at least one value in
IntBag
has a repeat count of
count
.
public int median()
–
returns the median item in the
IntBag
, based on a sorted low to high order of the
values.
Note: repeats must be considered here. The state variable
M
, below, may help.
Several abstract implementations (data models) seem appropriate candidates for representing such an ADT.
Moreover, each such abstract implementation might need to be evaluated in terms of a specific data structure.
We will always assume that these implementations keep track of the current number of distinct integers (
N
)
and the total number of items (
M
) in the
IntBag
, but we will assume no additional state information, except
that implied by the particular data model/structure.
Fill in the order of complexities in terms of
N
of each of the last five services provided for the
IntBag
ADT,
given the following four approaches to implementation.
In all cases, assume that you are concerned with
expected, not worst case performance.
You should not be surprised if one or more of these suggested
approaches are poor choices.
Note: items are stored as pairs (value, repeat count), where the value is used in
the orderings of the BST and Hash Table, but the repeat count is used for ordering items in the Sorted List.
i.)The state of the
IntBag
is represented as a
Binary Search Tree
(
BST
). Assume a right child / left child
linked list data structure with your only direct access being to the root.
value
is the sort key.
ii.)
The state of the
IntBag
is represented by a
Hash Table
(
HT
).
You may assume that collisions are
handled by using sorted buckets and that the hash function evenly distributes the
N
elements over
B
buckets.
value
is the sort and hash key. Your answer should be in terms of two possibly independent
parameters,
N
and
B
.
iii.)
The state of the
IntBag
is represented by an
Unsorted List
(
UL
).
Assume a simple array data
structure, storing data in positions
0
to
N1
.
iv.)
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Computer Science, LG, Array data structure

Click to edit the document details