This document provides an introduction and overview of data structures. It discusses how data structures organize data in computer memory and some common data structures like lists, arrays, stacks, queues, trees and graphs. It explains that programmers select appropriate data structures based on the nature of the data and operations needed. The document also covers topics like abstract data types, logical vs physical forms of data, algorithms, and problems versus programs.
1. Lecture 1
Introduction to Data
Structures
Abdisalam Issa-Salwe
Taibah University
College of Computer Science & Engineering
Computer Science Department
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
1
2. 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
3
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 Binary Tree
processes that need to be
performed on that data.
4
2
3. 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.
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
3
4. 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.
7
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
8
4
5. 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
9
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.
10
5
6. 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.
.
11
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.
.
12
6
7. 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.
13
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.
14
7
8. 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.
15
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.
16
8
9. 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.
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.
18
9
10. 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.
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
10
11. 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.
21
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.
22
11
12. 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
23
Sets
a set is an unordered
group of elements
duplicates are not
allowed
set of foods I like
otherwise how would
you tell them apart? Chips
Cereal
the set of "foods I Chicken
like" contains the Orange Tomato
elements juice soup
cereal, chicken, chips,
tomato soup, orange Chocolate
juice and chocolate
24
12
13. Lists
a list is a group of elements
arranged in some order Things I ate today
so we can talk about (in chronological order)
the first element in the list
the last element in the list 1. cereal
element 3 2. orange juice
the order could be meaningful 3. chocolate
alphabetical
by size 4. tomato soup
by time 5. chocolate
or it could simply be the order the 6. chicken
elements were added 7. chips
duplicates are allowed 8. chocolate
they are distinguished by their position 25
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)
26
13
14. 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)
27
Maps
A map is a collection of confusion
key/element pairs algorithm
each element is stored in
a position determined by
its key
can look up an element
using its key
also known as a
dictionary university
key is the word
element is the definition
28
14
15. 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.
29
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.
30
15
16. 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.
31
Data Type
ADT:
Data Items:
Type
Logical Form
Operations
Data Structure: Data Items:
Storage Space Physical Form
Subroutines
32
16
17. Problems, Algorithms and Programs
Programmers deal with:
problems,
algorithms and
computer programs.
33
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.
34
17
18. 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.
35
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.
36
18
19. 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
37
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.
38
19
20. 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.
39
References
Michael Main, Data Structures and Other
Objects Using Java (Third Edition)
Abdisalam Issa-Salwe, Taibah University,
Madinah, Saudi Arabia.
40
20