You are on page 1of 26

Chapter 1

I N T R OD UC TIO N TO D ATA S T R U C T UR ES A N D C + +
Contents
I. Lesson 1.1 - Introduction to Data Structures
• Languages in data Structures
• Costs and Benefits
• What are Data Structures

II. Lesson 1.2 - Data Structures Data Types


• Abstract Data Types
• Advantages of Abstract Data Typing
Lesson 1.1 - Introduction to Data Structures
• The information that is available to the computer consists of a
selected set of data about the actual problem, namely that set that is
considered relevant to the problem at hand, that set from which it is
believed that the desired results can be derived.
• The data represent an abstraction of reality in the sense that certain
properties and characteristics of the real objects are ignored because
they are peripheral and irrelevant to the particular problem.
• An abstraction is thereby also a simplification of facts.
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 supported. 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.
Language in Data Structures
• A programming language represents an abstract computer capable of
interpreting the terms used in this language, which may embody a
certain level of abstraction from the objects used by the actual
machine.
• Thus, the programmer who uses such a higher-level language will be
freed (and barred) from questions of number representation, if the
number is an elementary object in the realm of this language.
Costs and Benefits
• A data structure requires a certain amount of space for each data
item it stores, a certain amount of time to perform a single basic
operation, and a certain amount of programming effort.
Example 1.1
• A company is developing a database system containing information
about cities and towns in the United States. There are many
thousands of cities and towns, and the database program should
allow users to find information about a particular place by name
(another example of an exact-match query). Users should also be able
to find all places that match a particular value or range of values for
attributes such as location or population size. This is known as a range
query.
Hash Table – Figure 1.1.1
What are Data Structures?
• Data structures are software artifacts that allow data to be stored,
organized and accessed.
• They are more high-level than computer memory (hardware) and
lower-level than databases and spreadsheets (which associate meta-
data and meaning to the stored data).
• Ultimately data structures have two core functions: put stuff in, and
take stuff out.
Lesson 1.2 – Data Structures Data Types
• In computer science, an abstract data type (ADT) is a mathematical
model for a certain class of data structures that have similar behavior;
or for certain data types of one or more programming languages that
have similar semantics.
• An abstract data type is defined indirectly, only by the operations
that may be performed on it and by mathematical constraints on the
effects (and possibly cost) of those operations.
• A type is a collection of values.
• The integers also form a type. An integer is a simple type because its
values contain no subparts.
• A data item is a piece of information or a record whose value is drawn
from a type. A data item is said to be a member of a type.
• An abstract stack data structure could be defined by three
operations: push, that inserts some data item onto the structure, pop,
that extracts an item from it (with the constraint that each pop
always returns the most recently pushed item that has not been
popped yet), and peek, that allows data on top of the structure to be
examined without removal.
• When analyzing the efficiency of algorithms that use stacks, one may
also specify that all operations take the same time no matter how
many items have been pushed into the stack, and that the stack uses
a constant amount of storage for each element.
Abstract Data Types Defined
• An abstract data type (ADT) is the realization of a data type as a
software component. The interface of the ADT is defined in terms of a
type and a set of operations on that type.
• The behavior of each operation is determined by its inputs and
outputs. An ADT does not specify how the data type is implemented.
• Figure 1.2.1
The relationship
between data items,
abstract data types,
and data structures. The
ADT defines the logical
form of the data type.
The data structure
implements the
physical form of the
data type.
• A data structure is the implementation for an ADT. In an object-
oriented language such as C++, an ADT and its implementation
together make up a class.
Example:
• The mathematical concept of an integer, along with operations that
manipulate integers, form a data type. The C++ int variable type is a
physical representation of the abstract integer. The int variable type,
along with the operations that act on an int variable, form an ADT.
An ADT for a list of integers might specify the
following operations:

• Insert a new integer at a particular position in the list.


• Return true if the list is empty.
• Reinitialize the list.
• Return the number of integers currently in the list.
• Delete the integer at a particular position in the list.
• A data type is a type together with a collection of operations to
manipulate the type.
• . “Array” is commonly used in computer programming to mean a
contiguous block of memory locations, where each memory location
stores one fixed-length data item. By this meaning, an array is a
physical data structure. However, array can also mean a logical data
type composed of a (typically homogeneous) collection of data items,
with each data item identified by an index number.
• An abstract data type is defined as a mathematical model of the
data objects that make up a data type as well as the functions that
operate on these objects. There are no standard conventions for
defining them. A broad division may be drawn between "imperative"
and "functional" definition styles.
Imperative
• In the "imperative" view, which is closer to the philosophy of
imperative programming languages, an abstract data structure is
conceived as an entity that is mutable — meaning that it may be in
different states at different times.
Example: abstract stack (imperative)

As another example, an imperative definition of an abstract stack could


specify that the state of a stack S can be modified only by the
operations
• push(S,x), where x is some value of unspecified nature; and
• pop(S), that yields a value as a result; with the constraint that
• For any value x and any abstract variable V, the sequence of
operations { push(S,x); V ← pop(S) } is equivalent to { V ← x };
Functional
• In this view, any operation that modifies the ADT is modeled as a
mathematical function that takes the old state as an argument, and
returns the new state as part of the result.
• Unlike the "imperative" operations, these functions have no side
effects. Therefore, the order in which they are evaluated is
immaterial, and the same operation applied to the same arguments
(including the same input states) will always return the same results
(and output states).
Example: abstract stack (functional)
For example, a complete functional-style definition of a stack ADT
could use the three operations:
• push: takes a stack state and an arbitrary value, returns a stack state;
• top: takes a stack state, returns a value;
• pop: takes a stack state, returns a stack state;
with the following axioms:
• top(push(s,x)) = x (pushing an item onto a stack leaves it at the top)
• pop(push(s,x)) = s (pop undoes the effect of push)
• In a functional-style definition there is no need for a create operation.
Indeed, there is no notion of "stack instance".
• The stack states can be thought of as being potential states of a
single stack structure, and two stack states that contain the same
values in the same order are considered to be identical states. This
view actually mirrors the behavior of some concrete
implementations, such as linked lists with hash cons.
Advantages of Abstract Data Typing
• Encapsulation
• Localization of change
• Flexibility
Some common ADTs, which have proved useful in
a great variety of applications, are:

• Container • Set
• Deque • Stack
• List • String
• Map • Tree
• Multimap
• Multiset
• Priority queue
• Queue

You might also like