Lecture1 data structure(introduction)
-
Upload
taibah-university-college-of-computer-science-engineering -
Category
Education
-
view
85 -
download
0
Transcript of Lecture1 data structure(introduction)
1
Introduction to Data
Structures
Lecture 1
Khalid Khankan and Abdisalam Issa-Salwe
Taibah University
College of Computer Science & Engineering
Computer Science Department
2
Outline
1. Choosing Data Structures
2. Selecting Data Structure
3. Data Structure Philosophy
4. Abstract Data Types
5. Logical vs. Physical Form
6. Programs
7. Algorithm Properties
8. References
2
3
Data Structures
A data structure is a scheme
for organizing data in the
memory of a computer.
Some of the more commonly
used data structures include
lists, arrays, stacks, queues,
heaps, trees, and graphs.
Binary Tree
4
Data Structures
The way in which the data is
organized affects the
performance of a program
for different tasks.
Computer programmers
decide which data structures
to use based on the nature
of the data and the
processes that need to be
performed on that data.
Binary Tree
3
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
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.
4
7
Example: A Queue
A queue is an example of commonly used simple data
structure. A queue has beginning and end, called the
front and back of the queue.
Data enters the queue at one end and leaves at the other.
Because of this, data exits the queue in the same order in
which it enters the queue, like people in a checkout line at
a supermarket.
8
Example: A Binary Tree
A binary tree is another
commonly used data
structure. It is organized like
an upside down tree.
Each spot on the tree, called
a node, holds an item of data
along with a left pointer and
a right pointer. Binary Tree
5
9
Example: A Binary Tree
The pointers are lined up
so that the structure forms
the upside down tree, with
a single node at the top,
called the root node, and
branches increasing on the
left and right as you go
down the tree. Binary Tree
10
Choosing Data Structures
By comparing the queue with
the binary tree, you can see
how the structure of the data
affects what can be done
efficiently with the data.
6
11
Choosing Data Structures
A queue is a good data structure
to use for storing things that need
to be kept in order, such as a set
of documents waiting to be
printed on a network printer.
.
12
Choosing Data Structures
The jobs will be printed in the
order in which they are received.
Most network print servers
maintain such a print queue.
.
7
13
Choosing Data Structures
A binary tree is a good data
structure to use for searching
sorted data.
The middle item from the list is
stored in the root node, with
lesser items to the left and greater
items to the right.
14
Choosing Data Structures
A search begins at the root. The
computer either find the data, or
moves left or right, depending on
the value for which you are
searching.
Each move down the tree cuts the
remaining data in half.
8
15
Choosing Data Structures
Items can be located very quickly in a tree.
Telephone directory assistance information is stored in a tree, so that a name and phone number can be found quickly.
16
Choosing Data Structures
For some applications, a queue is the best data structure to use.
For others, a binary tree is better.
Programmers choose from among many data structures based on how the data will be used by the program.
9
17
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.
18
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.
10
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 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.
11
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
Data Structure
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 C++, 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.
12
23
Abstract data types
we also looked at how to use an array to model a list
we call a list an abstract data type it has defined operations
add to the end of the list
find an item in the list
delete an item from the list etc.
it can be implemented in different ways array, piece of paper, tree, linked list
the operations have the same effect no matter how the list is implemented
other examples of abstract data types set, queue, stack, map
24
Sets
a set is an unordered
group of elements
duplicates are not
allowed
otherwise how would
you tell them apart?
the set of "foods I
like" contains the
elements
cereal, chicken, chips,
tomato soup, orange
juice and chocolate
Cereal
Chicken
Chocolate
set of foods I like
Chips
Orange
juice Tomato
soup
13
25
Lists
a list is a group of elements arranged in some order so we can talk about
the first element in the list
the last element in the list
element 3
the order could be meaningful alphabetical
by size
by time
or it could simply be the order the elements were added
duplicates are allowed they are distinguished by their position
Things I ate today
(in chronological order)
1. cereal
2. orange juice
3. chocolate
4. tomato soup
5. chocolate
6. chicken
7. chips
8. chocolate
26
Queue
a queue is a a group of items arranged in order of arrival
new items can only be added to the back of the queue
items can only be removed from the front of the queue shoppers at supermarket check-out
taxis in rank
computer processes awaiting execution
first-in, first-out (FIFO)
14
27
Stacks
a stack is a group of items
arranged in order
new items can only be
added to the top of the stack
items can only be removed
from the top of the stack
stack of chairs or books
plates in cafeteria
temporary data storage in
CPU
last in, first out (LIFO)
28
Maps
A map is a collection of
key/element pairs
each element is stored in
a position determined by
its key
can look up an element
using its key
also known as a
dictionary
key is the word
element is the definition
algorithm
confusion
university
15
29
Data Structure
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 C++, 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.
30
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
Ex1: transistors gates CPU.
In a program, implement an ADT, then think only
about the ADT, not its implementation.
16
31
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.
32
Data Type
ADT:
Type
Operations
Data Items:
Logical Form
Data Items:
Physical Form
Data Structure:
Storage Space
Subroutines
17
33
Problems, Algorithms and Programs
Programmers deal with:
problems,
algorithms and
computer programs.
34
Problems
Problem: a task to be performed.
Best thought of as inputs and matching
outputs.
Problem definition should include constraints
on the resources that may be consumed by
any acceptable solution.
18
35
Problems (cont)
Problems mathematical functions
A function is a matching between inputs (the domain) and outputs (the range).
An input to a function may be single number, or a collection of information.
The values making up an input are called the parameters of the function.
A particular input must always result in the same output every time the function is computed.
36
Algorithms and Programs
Algorithm: a method or a process followed to solve a problem. A recipe: The algorithm gives us a “recipe” for solving
the problem by performing a series of steps, where each step is completely understood and doable.
An algorithm takes the input to a problem
(function) and transforms it to the output. A mapping of input to output.
A problem can be solved by many algorithms.
19
37
A problem can have many algorithms
For example, the problem of sorting can be solved
by the following algorithms:
Insertion sort
Bubble sort
Selection sort
Shell sort
Merge sort
Others
38
Algorithm Properties
An algorithm possesses the following properties: It must be correct.
It must be composed of a series of concrete steps.
There can be no ambiguity as to which step will be performed next.
It must be composed of a finite number of steps.
It must terminate.
A computer program is an instance, or concrete representation, for an algorithm in some programming language.
20
39
Programs
A computer program is a concrete
representation of an algorithm in some
programming language.
Naturally, there are many programs that
are instances of the same algorithms,
since any modern programming language
can be used to implement any algorithm.
40
References
Michael Main, Data Structures and Other
Objects Using Java (Third Edition)