Java Reference
In-Depth Information
Given sufficient space to store a collection of data items, it is always possible to
search for specified items within the collection, print or otherwise process the data
items in any desired order, or modify the value of any particular data item. Thus,
it is possible to perform all necessary operations on any data structure. However,
using the proper data structure can make the difference between a program running
in a few seconds and one requiring many days.
A solution is said to be efficient if it solves the problem within the required
resource constraints. Examples of resource constraints include the total space
available to store the data — possibly divided into separate main memory and disk
space constraints — and the time allowed to perform each subtask. A solution is
sometimes said to be efficient if it requires fewer resources than known alternatives,
regardless of whether it meets any particular requirements. The cost of a solution is
the amount of resources that the solution consumes. Most often, cost is measured
in terms of one key resource such as time, with the implied assumption that the
solution meets the other resource constraints.
It should go without saying that people write programs to solve problems. How-
ever, it is crucial to keep this truism in mind when selecting a data structure to solve
a particular problem. Only by first analyzing the problem to determine the perfor-
mance goals that must be achieved can there be any hope of selecting the right data
structure for the job. Poor program designers ignore this analysis step and apply a
data structure that they are familiar with but which is inappropriate to the problem.
The result is typically a slow program. Conversely, there is no sense in adopting
a complex representation to “improve” a program that can meet its performance
goals when implemented using a simpler design.
When selecting a data structure to solve a problem, you should follow these
steps.
1. Analyze your problem to determine the basic operations that must be sup-
ported. Examples of basic operations include inserting a data item into the
data structure, deleting a data item from the data structure, and finding a
specified data item.
2. Quantify the resource constraints for each operation.
3. Select the data structure that best meets these requirements.
This three-step approach to selecting a data structure operationalizes a data-
centered view of the design process. The first concern is for the data and the op-
erations to be performed on them, the next concern is the representation for those
data, and the final concern is the implementation of that representation.
Resource constraints on certain key operations, such as search, inserting data
records, and deleting data records, normally drive the data structure selection pro-
cess. Many issues relating to the relative importance of these operations are ad-
dressed by the following three questions, which you should ask yourself whenever
you must choose a data structure:
Search WWH ::




Custom Search