Download Data structure

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Mohammed I DAABO
COURSE CODE: CSC 355
COURSE TITLE: Data Structures
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.
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.
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.
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
 Are all data inserted into the data structure at the
beginning, or are insertions interspersed with
other operations?
 Can data be deleted?
 Are all data processed in some well-defined
order, or is random access allowed?
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.
Data Structure Philosophy cont..
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
Goals of this Course
Reinforce the concept that costs and benefits
exist for every data structure.
2. Learn the commonly used data structures.
1.

3.
These form a programmer's basic data structure
``toolkit.'‘
Understand how to measure the cost of a data
structure or program.

These techniques also allow you to judge the
merits of new data structures that you or others
might invent.
Objectives At the end of the study, the student should be able to:
 Define a data structure.
 Define basic elementary data structures (arrays, records sets and files).
 Define an array as a data structure and how it is used to store a list of data items.
 Understand sequential and link representations of: stacks, queues, tables, binary trees









and graphs. Sorting and searching algorithms.
Understand data abstraction and its computer representation: concepts, data models,
and levels of abstraction.
Define the Big O notation and Recursion
Distinguish between the name of an array and the names of the elements in an
array.
Describe operations defined for an array.
Define a record as a data structure and how it is used to store attributes
belonging to a single data element.
Distinguish between the name of a record and the names of its fields.
Define a linked list as a data structure and how it is implemented using pointers.
Understand the mechanism through which the nodes in an array are accessed.
Describe operations defined for a linked list.
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.
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.
 Data structure usually refers to an organization
for data in main memory.
 File structure is an organization for data on
peripheral storage, such as a disk drive.
Metaphors
An ADT manages complexity through abstraction:
metaphor.
 Hierarchies of labels
Ex: transistors  gates  CPU.
In a program, implement an ADT, then think only
about the ADT, not its implementation.
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.
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
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
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.
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
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
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.
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.
.
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.
.
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.
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.
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.
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.
Data Structures in Alice
Alice has two built-in data structures
that can be used to organize data, or
to create other data structures:
• Lists
• Arrays
Lists
A list is an ordered set of data. It is often used to store
objects that are to be processed sequentially.
A list can be used
to create a queue.
Arrays
An array is an indexed set of variables, such as
dancer[1], dancer[2], dancer[3],… It is like a set of
boxes that hold things.
A list is a set of items.
An array is a set of
variables that each
stores an item.
Arrays and Lists
You can see the difference between arrays and
lists when you delete items.
Arrays and Lists
In a list, the missing spot is filled in when
something is deleted.
Arrays and Lists
In an array, an empty variable is left behind
when something is deleted.
Lists
A list is created in Alice by checking the make a
list box when creating a new variable.
Make a list box
Lists
The For all in order and For all together tiles can
be used to work with lists. They are at the
bottom of the editor area.
Arrays
Arrays can be created in a similar manner, but
more often they are created using the array
visualization object from the Alice local gallery.
The Array Visualization object
has special properties and
methods for manipulating
the elements in an array.
Arrays
Alice has a set of built-in functions that can be
performed on arrays.