Вы находитесь на странице: 1из 2

Next, for those of you who haven't yet been exposed to an object-oriented language, we'll

briefly explain enough about OOP to get you started. Finally, for C++ programmers who
don't know Java, we'll point out some of the differences between these languages.

Overview of Data Structures


Another way to look at data structures is to focus on their strengths and weaknesses. In
this section we'll provide an overview, in the form of a table, of the major data storage
structures we'll be discussing in this book. This is a bird's-eye view of a landscape that
we'll be covering later at ground level, so don't be alarmed if it looks a bit mysterious.
Table 1.1 shows the advantages and disadvantages of the various data structures
described in this book.

Table 1.1: Characteristics of Data Structures

Data Structure Advantages Disadvantages

Array Quick insertion, very fast Slow search, slow deletion, fixed
access if index known size.

Ordered array Quicker search than Slow insertion and deletion, fixed
unsorted array. size.

Stack Provides last-in, first-out Slow access to other items.


access.

Queue Provides first-in, first-out Slow access to other items.


access.

Linked list Quick insertion, quick Slow search.


deletion.

Binary tree Quick search, insertion, Deletion algorithm is complex.


deletion (if tree remains
balanced).

Red-black tree Quick search, insertion, Complex.


deletion. Tree always
balanced.

2-3-4 tree Quick search, insertion, Complex.


deletion. Tree always
balanced. Similar trees
good for disk storage.

Hash table Very fast access if key Slow deletion, access slow if key
known. Fast insertion. not known, inefficient memory
usage.

Heap Fast insertion, deletion, Slow access to other items.access


to largest item.

Graph Models real-world Some algorithms are slow and


situations. complex.

- 12 -
Comparing the General-Purpose Storage Structures
Table 15.1 summarizes the speeds of the various general-purpose data storage
structures using Big O notation.

Table 15.1: GENERAL-PURPOSE DATA STORAGE STRUCTURES

Data Structure Search Insertion Deletion Traversal

Array O(N) O(1) O(N) —

Ordered array O(logN) O(N) O(N) O(N)

Linked list O(N) O(1) O(N) —

Ordered linked list O(N) O(N) O(N) O(N)

Binary tree (average) O(logN) O(logN) O(logN) O(N)

Binary tree (worst case) O(N) O(N) O(N) O(N)

Balanced tree (averageand worst O(logN) O(logN) O(logN) O(N)


case)

Hash table O(1) O(1) O(1) —

Insertion in an unordered array is assumed to be at the end of the array. The ordered array
uses a binary search, which is fast, but insertion and deletion require moving half the items
on the average, which is slow. Traversal implies visiting the data items in order of
ascending or descending keys; the — means this operation is not supported.

Special-Purpose Data Structures


The special-purpose data structures discussed in this book are the stack, the queue, and
the priority queue. These structures, instead of supporting a database of user-accessible
data, are usually used by a computer program to aid in carrying out some algorithm.
We've seen examples of this throughout this book, such as in Chapters 13, "Graphs," and
14, "Weighted Graphs," where stacks, queues, and priority queues are all used in graph
algorithms.

Stacks, queues, and priority queues are abstract data types (ADTs) that are implemented
by a more fundamental structure such as an array, linked list, or (in the case of the
priority queue) a heap. These ADTs present a simple interface to the user, typically
allowing only insertion and the ability to access or delete only one data item. These items
are

• For stacks: the last item inserted

• For queues: the first item inserted

- 514 -

Вам также может понравиться