Sie sind auf Seite 1von 27

Semester Genap 2010/2011

Beni Suranto, ST.


beni.suranto@fti.uii.ac.id
Data Types
• We type data--classify it into various categories--such as int,
boolean, String, Applet
– A data type represents a set of possible values, such as
{..., -2, -1, 0, 1, 2, ...}, or {true, false}

• By typing our variables, we allow the computer to find some of our


errors
– Some operations only make sense when applied to certain kinds
of data--multiplication, searching

• Typing simplifies internal representation


– A String requires more and different storage
than a boolean
Data Types
• A data type is characterized by:
– a set of values
– a data representation, which is common to all these values, and
– a set of operations, which can be applied uniformly to all these
values
Primitive Types in Java
• Java provides eight primitive types:
– boolean
– char, byte, short, int, long
– float, double
• Each primitive type has
– a set of values
– a data representation
– a set of operations
• These are “set in stone”—there is nothing
the programmer can do to change anything
about them

4
Primitive Types as Data Types
Type Values Representation Operations

boolean true, false Single byte &&, ||, !

char, byte, Integers of Two’s complement +, -, *, /,


short, int, varying sizes others
long
float, Floating point Two’s complement +, -, *, /,
double numbers of with exponent and others
varying sizes mantissa
and
precisions

5
The Need for Data Structures
Data structures organize data
⇒ more efficient programs.

More powerful computers ⇒ more complex applications.


More complex applications demand more calculations.

Complex computing tasks are unlike our everyday experience.

6
What is a data structure?

• In a general sense, any data representation is a data structure.


Example: An integer

• More typically, a data structure is meant to be an organization


for a collection of data items.

7
Organizing Data
Any organization for a collection of records can be searched,
processed in any order, or modified.

The choice of data structure and algorithm can make the


difference between a program running in a few seconds or
many days.

8
Efficiency

A solution is said to be efficient if it solves the problem within its


resource constraints.
– Space
– Time

• The cost of a solution is the amount of resources that the


solution consumes.

9
Costs and Benefits

A data structure requires a certain amount of:

• space for each data item it stores

• time to perform a single basic operation

• programming effort.

10
Example: Banking Application

• Operations are (typically):

– Open accounts (far less often than access)


– Close accounts (far less often than access)
– Access account to Add money
– Access account to Withdraw money

11
Example: Banking Application

• Teller and ATM transactions are expected to take little time.

• Opening or closing an account can take much longer (perhaps


up to an hour).

12
Example: Banking Application

• When considering the choice of data structure to use in the


database system that manages the accounts, we are looking
for a data structure that:

– Is inefficient for deletion


– Highly efficient for search
– Moderately efficient for insertion

13
Example: Banking Application
• One data structure that meets these requirements is the hash
table.
• Records are accessible by account number (called an exact-
match query)
• Hash tables allow for extremely fast exact-match search.
• Hash tables also support efficient insertion of new records.
• Deletions can also be supported efficiently (but too many
deletions lead to some degradation in performance –
requiring the hash table to be reorganized).

14
Example: City Database
• Database system for cities and towns.

• Users find information about a particular place by name


(exact-match query)

• Users also find all places that match a particular value (or
range of values), such as location or population size (called a
range query).

15
Example: City Database

• The database must answer queries quickly enough to satisfy


the patience of a typical user.

• For an exact-match query, a few seconds is satisfactory

• For a range queries, the entire operation may be allowed to


take longer, perhaps on the order of a minute.

16
Example: City Database
• The hash table is inappropriate for implementing the city
database because:
– It cannot perform efficient range queries

• The B+ tree supports large databases:


– Insertion
– Deletion
– Range queries
• If the database is created once and then never changed, a
simple linear index would be more
appropriate.

17
Selecting a Data Structure
Select a data structure as follows:

1. Analyze the problem to determine the resource constraints


a solution must meet.
2. Determine the basic operations that must be supported.
Quantify the resource constraints for each operation.
3. Select the data structure that best meets these
requirements.
Some Questions to Ask
• Are all data inserted into the data structure at the beginning,
or are insertions intersparsed with other operations?

• Can data be deleted?

• Are all data processed in some well-defined order, or is


random access allowed?

19
Data Structure Philosophy
Each data structure has costs and benefits.

Rarely is one data structure better than another in all situations.


A data structure requires:
– space for each data item it stores,
– time to perform each basic operation,
– programming effort.

20
Data Structure Philosophy
Each problem has constraints on available space and time.

Only after a careful analysis of problem characteristics can we


know the best data structure for the task.

Bank example:
– Start account : a few minutes
– Transactions : a few seconds
– Close account : overnight

21
Abstract Data Types

Abstract Data Type (ADT): a definition for a data type solely in


terms of a set of values and a set of operations on that data
type.
Each ADT operation is defined by its inputs and outputs.
Encapsulation: Hide implementation details.

22
Abstract Data Types
• A data structure is the physical implementation of an ADT.
– Each operation associated with the ADT is implemented by
one or more subroutines in the implementation.
• In a OO language such as Java, an ADT and its implementation
together make up a class.
• Data structure usually refers to an organization for data in
main memory.
• File structure: an organization for data on peripheral storage,
such as a disk drive.

23
Labeling collections of objects
Humans deal with complexity by assigning a label to an
assembly of objects. An ADT manages complexity through
abstraction.
– Hierarchies of labels
Example : transistors ⇒ gates ⇒ CPU.

In a program, implement an ADT, then think only about the ADT,


not its implementation.

24
Logical vs. Physical Form
Data items have both a logical and a physical form.
Logical form: definition of the data item within an ADT.
– Ex: Integers in mathematical sense: +, -
Physical form: implementation of the data item within a data
structure.
– Ex: 16/32 bit integers, overflow.

25
Data Type

ADT:
Type Data Items:
Operations Logical Form

Data Structure: Data Items:


Storage Space Physical Form
Subroutines

Das könnte Ihnen auch gefallen