Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
0001
The crane’s algorithm to peck food was working on the wrong data
structure.
Why focus on Data Structures
The crane soon gave up, and took the fox’s leave.
Before leaving however, the crane invited the fox to
dinner again at it’s place. The fox was over confident
and accepted immediately.
At the crane’s place, the fox was served some fish in a
jar.
Why focus on Data Structures
Why focus on Data Structures
The fox’s mouth wasn’t long enough to reach the bottom,
where the fish were waiting invitingly. The thin jar
opening was perfect for the crane. It pecked out all the
fish while the fox hung with it’s mouth in the jar,
desperate to get a bite.
That’s what happens when you use the right data
structure.
A data structure defines a contract for storing, retrieving
and manipulating the data inside it. They can speed up
algorithms and are critical to their success.
Need for Data Structures
Data structures organize data more
efficient programs.
More powerful computers more
complex applications.
More complex applications demand more
calculations.
Organizing Data
Any organization for a collection of records
that 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
x[4]
x[5]
What is Array Name?
‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
For example, if we have the code
int a, b;
b = 2;
a = b;
a = 5;
2 = a;
What is Array Name?
‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
For example, if we have the code
int a, b;
b = 2;
a = b;
a = 5;
2 = a;
What is Array Name?
‘x’ is an array name but there is no variable x. ‘x’ is not an lvalue.
For example, if we have the code
int a, b;
b = 2;
a = b;
a = 5;
2 = a;
Array Name
‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Array Name
‘x’ is not an lvalue
int x[6];
int n;
x[0] = 5;
x[1] = 2;
x = 3; // not allowed
x = a + b; // not allowed
x = &n; // not allowed
Dynamic Arrays
You would like to use an array data structure
but you do not know the size of the array at
compile time.
You find out when the program executes that
you need an integer array of size n=20.
Allocate an array using the new operator:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Dynamic Arrays
We must free the memory we got using the
new operator once we are done with the y
array.
delete[ ] y;
1.Tree
2.Graph
Tree
Tree: is a non-linear data structure which is
mainly used to represent data containing a
hierarchical relationship between elements.
Binary Tree: A binary tree is a tree such that
every node has at most 2 child and each
node is labeled as either left of right child.
Operations on Tree
1.Insertion
2.Deletion
3.Searching
Tree Representation
Graph
• A graph data structure may also associate to
each edge some edge value, such as a
symbolic label or a numeric attribute (cost,
capacity, length, etc.).
• Operations performed Graph
1.Searching
2.Insertion
3.Deletion
Graph
• A graph data structure may also associate to
each edge some edge value, such as a
symbolic label or a numeric attribute (cost,
capacity, length, etc.).
• Operations performed Graph
1.Searching
2.Insertion
3.Deletion
Graph Representation
Algorithm
• An algorithm is a step by step method of
solving a problem. It is commonly used for
data processing, calculation and other related
computer and mathematical operations.
• An algorithm is also used to manipulate data
in various ways, such as inserting a new data
item, searching for a particular item or sorting
an item.
All Algorithms must satisfy the
following criteria -
1) Input : There are more quantities that are extremely
supplied.
2) Output: At least one quantity is produced.
3) Definiteness: Each instruction of the algorithm
should be clear and unambiguous.
4) Finiteness: The process should be terminated after a
finite number of steps.
5) Effectiveness: Every instruction must be basic
enough to be carried out theoretically or by using paper
and pencil.
Properties of Algorithm
1) Input : There are more quantities that are extremely
supplied.
2) Output: At least one quantity is produced.
3) Definiteness: Each instruction of the algorithm
should be clear and unambiguous.
4) Finiteness: The process should be terminated after a
finite number of steps.
5) Effectiveness: Every instruction must be basic
enough to be carried out theoretically or by using paper
and pencil.
Algorithm (or program) Specification
Specification is the process of translating a solution of a
problem into an algorithm. This can be done in three
ways:
1.Steps or instructions can be written by using a Natural
Language like English. In this case we have to ensure
that the resulting instructions are definite.
2. 2. Graphic representations called Flowcharts can also
be used for specifications, but they work well only if the
algorithm is small and simple.
3.3. Use of Pseudo Code is the most preferred option to
specify an algorithm. It is represented by using a Natural
Language (English) and C / C++.