Bab-001_Pengantar_ADT
Transcript of Bab-001_Pengantar_ADT
-
8/7/2019 Bab-001_Pengantar_ADT
1/27
Semester Genap 2010/2011
Beni Suranto, ST.
-
8/7/2019 Bab-001_Pengantar_ADT
2/27
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 storagethan a boolean
-
8/7/2019 Bab-001_Pengantar_ADT
3/27
A data type is characterized by:
a set ofvalues
a data representation, which is common to all these values, and
a set ofoperations, which can be applied uniformly to all thesevalues
Data Types
-
8/7/2019 Bab-001_Pengantar_ADT
4/27
4
Primitive Types in Java
Java provides eight primitive types:
boolean
char, byte, short, int, long
float, double
Each primitive type has
a set ofvalues
a data representation
a set ofoperations
These are set in stonethere is nothing
the programmer can do to change anything
about them
-
8/7/2019 Bab-001_Pengantar_ADT
5/27
5
Primitive Types as Data Types
Type Values Representation Operations
boolean true, false Single byte &&, ||, !
char, byte,short, int,long
Integers ofvarying sizes
Twos complement +, -, *, /,others
float,double
Floating pointnumbers ofvarying sizesandprecisions
Twos complementwith exponent andmantissa
+, -, *, /,others
-
8/7/2019 Bab-001_Pengantar_ADT
6/27
6
The Need for Data Structures
Data structures organize data
more efficient programs.
More powerful computersmore complex applications.
More complex applications demand more calculations.
Complex computing tasks are unlike our everyday experience.
-
8/7/2019 Bab-001_Pengantar_ADT
7/27
7
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 organizationfor a collection of data items.
-
8/7/2019 Bab-001_Pengantar_ADT
8/27
8
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/7/2019 Bab-001_Pengantar_ADT
9/27
9
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 thesolution consumes.
-
8/7/2019 Bab-001_Pengantar_ADT
10/27
10
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.
-
8/7/2019 Bab-001_Pengantar_ADT
11/27
11
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
-
8/7/2019 Bab-001_Pengantar_ADT
12/27
12
Teller and ATM transactions are expected to take little time.
Opening or closing an account can take much longer (perhapsup to an hour).
Example: Banking Application
-
8/7/2019 Bab-001_Pengantar_ADT
13/27
13
When considering the choice of data structure to use in thedatabase 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
Example: Banking Application
-
8/7/2019 Bab-001_Pengantar_ADT
14/27
14
One data structure that meets these requirements is the hashtable.
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).
Example: Banking Application
-
8/7/2019 Bab-001_Pengantar_ADT
15/27
15
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 (orrange of values), such as location or population size (called arange query).
-
8/7/2019 Bab-001_Pengantar_ADT
16/27
16
The database must answer queries quickly enough to satisfythe 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 totake longer, perhaps on the order of a minute.
Example: City Database
-
8/7/2019 Bab-001_Pengantar_ADT
17/27
17
The hash table is inappropriate for implementing the citydatabase because: It cannot perform efficient range queries
The B+ tree supports large databases:
InsertionDeletionRange queries
If the database is created once and then never changed, a
simple linear index would be moreappropriate.
Example: City Database
-
8/7/2019 Bab-001_Pengantar_ADT
18/27
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.
-
8/7/2019 Bab-001_Pengantar_ADT
19/27
19
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 israndom access allowed?
-
8/7/2019 Bab-001_Pengantar_ADT
20/27
20
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.
-
8/7/2019 Bab-001_Pengantar_ADT
21/27
21
Each problem has constraints on available space and time.
Only after a careful analysis of problem characteristics can weknow the best data structure for the task.
Bank example:
Start account : a few minutesTransactions : a few secondsClose account : overnight
Data Structure Philosophy
-
8/7/2019 Bab-001_Pengantar_ADT
22/27
22
Abstract Data Types
Abstract Data Type (ADT): a definition for a data type solely interms of a set of values and a set of operations on that datatype.
Each ADT operation is defined by its inputs and outputs.Encapsulation: Hide implementation details.
-
8/7/2019 Bab-001_Pengantar_ADT
23/27
23
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 implementationtogether make up a class.
Data structure usually refers to an organization for data inmain memory.
File structure: an organization for data on peripheral storage,such as a disk drive.
Abstract Data Types
-
8/7/2019 Bab-001_Pengantar_ADT
24/27
24
Labeling collections of objects
Humans deal with complexity by assigning a label to anassembly 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.
-
8/7/2019 Bab-001_Pengantar_ADT
25/27
25
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 datastructure.
Ex: 16/32 bit integers, overflow.
-
8/7/2019 Bab-001_Pengantar_ADT
26/27
Data Type
ADT:TypeOperations
Data Items:Logical Form
Data Items:Physical Form
Data Structure:Storage Space
Subroutines
-
8/7/2019 Bab-001_Pengantar_ADT
27/27