Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Data Structure Intro

A small introduction to data structure

Uploaded by

vinushirley
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Data Structure Intro

A small introduction to data structure

Uploaded by

vinushirley
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA STRUCTURES

DEFINE DATA STRUCTURE


A data structure is a data organization,
management, and storage format that enables
efficient access and modification. More precisely, a
data structure is a collection of data values, the
relationships among them, and the functions or
operations that can be applied to the data.

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
The way in which the data is organized affects the
performance of a program for different tasks
OPERATIONS ON DATA STRUCTURES
The basic operations that are performed on data structures are as
follows:
➢ Insertion: Insertion means addition of a new data
element in a data structure.
➢ Deletion: Deletion means removal of a data element
from a data structure if it is found.
➢ Searching: Searching involves searching for the
specified data element in a data structure.

➢ Traversal: Traversal of a data structure means


processing all the data elements present in it.
➢ Sorting: Arranging data elements of a data structure in a
specified order is called sorting.
➢ Merging: Combining elements of two similar data
structures to form a new data structure of the same type, is
called merging.
TYPES OF DATA
STRUCTURES
SIMPLE DATA STRUCTURES

These are normally built from primitive data types like


integers, real, character, Boolean. There are following two
types of Simple Data Structure :
● Array
● Structure
COMPOUND DATA STRUCTURES
Simple data structures can be combined in various ways to form more
complex structures called compound data structures. They are classified
into the following two types:

● Linear data structures:


These data structures are single level data structures. A data
structure is said to be linear if its elements form a sequence.
There are the following types:
1. Stack
2. Queue
3. Linked List

● Non-linear data structures :


These are multilevel data structures.
Example of non-linear data structure is Tree.
WHAT IS A STACK ?
Stack is a linear data structure which follows a particular order in which
the operations are performed. The order may be LIFO(Last In First Out)

Stack is a linear data structure which works on LIFO order. So that Last In First
Out .

In stack element is always added at top of stack and also removed from top of
the stack.

Stack is useful in recursive function, function calling, mathematical expression


calcualtion,reversing the string etc.
OPERATIONS ON STACK
● PUSH Operation :
Push operation refers to inserting an element in the stack. Since
there’s only one position at which the new element can be inserted
— Top of the stack, the new element is inserted at the top of the
stack.

● POP Operation :
Pop operation refers to the removal of an element. Again, since we
only have access to the element at the top of the stack, there’s only
one element that we can remove. We just remove the top of the
stack. We can choose to return the value of the popped element
back, it is at the choice of the programmer to implement this.
● PEEK Operation :
Peek operation allows the user to see the element on the top of the
stack. The stack is not modified in any manner in this operation.

● isEmpty Operation :
This Operation checks whether the stack is empty or not. To prevent
performing operations on an empty stack, the programmer is
required to internally maintain the size of the stack which will be
updated during push and pop operations accordingly. isEmpty()
conventionally returns a boolean value: True if size is 0, else False.
WHAT IS A QUEUE ?
A queue is a collection of entities that are
maintained in a sequence and can be modified
by the addition of entities at one end of the
sequence and the removal of entities from the
other end of the sequence.
● Queue is also a linear data structure which works onFIFO order.
So that First In First Out .

● In queue element is always added at rear of queue and removed


from front of queue.

● Queue applications are in CPU scheduling, Disk Scheduling,IO


Buffers, pipes, file IO.
Linked List
● a linked list is a linear collection of data elements, in which
linear order is not given by their physical placement in memory.

● Elements may be added in front, end of list as well as middle of


list.
● Linked List may be use for dynamic implementation of stack and
queue.
TREE
● A tree is a non linear data structure. a root value and subtrees of
children with a parent node, represented as a set of linked nodes.
● Nodes can be added at any different node.
● Tree applications includes:-
● Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
Graph

● A graph is a non linear data structure. A set of items connected by


edges. Each item is called a vertex or node. Formally, a graph is a
set of vertices and a binary relation between vertices, adjacency.
● Graph applications:-
● finding shortest routes, searching, social network connections,
internet routing.
Basic Concepts -Sub-Algorithm
● A sub-algorithm is an algorithmic module which is complete in itself and has
independent existence. It can be used or called by some main algorithm or by
some other sub-algorithm. It may or may not receive values from a calling
algorithm. These values are called arguments or parameters. The sub-algorithm
performs its task and then sends back the result to the calling algorithm. It can be
called by many different algorithms or called at different places in the same
algorithm. The relationship between an algorithm and a sub-algorithm is similar
to the relationship between a main program (function) and a sub-program (sub-
function) in a programming language.
● Usefulness
● When the problem is very complex, it is divided into several independent sub-
problems. Each sub-problem is called sub-algorithm and can be developed
independently. These sub-problems can be combined together to form the
solution for the entire problem. Thus by using sub-algorithms the complex
problems can be solved easily.
● A sub algorithm is an independent component of
an algorithm and for this reason is defined
separately from the main algorithm. The purpose
of a sub algorithm is to perform some
computation when required, under control of the
main algorithm. This computation may be
performed on zero or more parameters passed by
the calling routine.
A control structure is like a block of programming that analyses variables
and chooses a direction in which to go based on given parameters. The
term flow control details the direction the program takes (which way
program control "flows").

Flow of control through any given function is implemented


with three basic types of control structures:
 Sequential: default mode. ...
 Selection: used for decisions, branching -- choosing between 2 or more
alternative paths. ...
 Repetition: used for looping, i.e. repeating a piece of code multiple
times in a row.

“Sequence control structure” refers to the line-by-line execution by which


statements are executed sequentially, in the same order in which they
appear in the program. ... The sequence control structure is the simplest
of the three fundamental control structures
Control Structures

● Sequential execution
○ Statements executed in order
● Transfer of control
○ Next statement executed not next one in
sequence 19

● 3 control structures (Bohm and Jacopini)


○ Sequence structure

■ Programs executed sequentially by default

○ Selection structures

■ if, if/else, switch

○ Repetition structures

■ while, do/while, for


Control Structures

● Flowchart
○ Graphical representation of an algorithm

○ Special-purpose symbols connected by arrows


(flowlines)

○ Rectangle symbol (action symbol)

■ Any type of action

○ Oval symbol 20

■ Beginning or end of a program, or a section of code


(circles)
● Single-entry/single-exit control structures
○ Connect exit point of one to entry point of the next

○ Control structure stacking


if Selection Structure

● Selection structure
○ Choose among alternative courses of action

○ Pseudocode example:
If student’s grade is greater than or equal to 60

Print “Passed”

○ If the condition is true


21
■ Print statement executed, program continues to next
statement

○ If the condition is false

■ Print statement ignored, program continues

○ Indenting makes programs easier to read

■ C++ ignores whitespace characters (tabs, spaces,


etc.)
if Selection Structure

● Translation into C++


If student’s grade is greater than or equal to 60

Print “Passed”

if ( grade >= 60 )
cout << "Passed";
● Diamond symbol (decision symbol)
○ Indicates decision is to be made 22

○ Contains an expression that can be true or false

■ Test condition, follow path


● if structure
○ Single-entry/single-exit
if Selection Structure
● Flowchart of pseudocode statement

A decision can be made on


any expression.
zero - false
23
nonzero - true
true
grade >= print “Passed” Example:
60 3 - 4 is true

false
if/else Selection Structure

● if
○ Performs action if condition true
● if/else
○ Different actions if conditions true or false
● Pseudocode
if student’s grade is greater than or equal to 60
print “Passed” 24

else
print “Failed”
● C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
if/else Selection Structure
● Nested if/else structures

○ One inside another, test for multiple cases

○ Once condition met, other statements skipped


if student’s grade is greater than or equal to 90
Print “A”

else
if student’s grade is greater than or equal to 80
Print “B”
else 25
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else

Print “F”
if/else Selection Structure
● Example

if ( grade >= 90 ) // 90 and above


cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D"; 26
else // less than 60
cout << "F";
Importance of Curly Braces
● Print “We have a problem” if examGrade < 60
● Print “We have a real problem” if examGrade < 60
and quizGrade < 10
● Print “Ok” if examGrade >= 60
int examGrade, quizGrade;
if (examGrade < 60)
cout << “We have a problem” << endl;
if (quizGrade < 10)
cout << “We have a real problem” << endl;
else
cout << “Ok”; 27
Exam Grade Flowchart
int examGrade, quizGrade;
if (examGrade < 60)
cout << “We have a problem” << endl;
if (quizGrade < 10)
cout << “We have a real problem” << endl;
else
cout << “Ok”;

true
examGrade < 60
28

“We have a problem”

false true
quizGrade < 10

“We have a real problem”


“Ok”
Writing Cases

● Print “We have a problem” if examGrade < 60


● Print “We have a real problem” if examGrade < 60
and quizGrade < 10
● Print “Ok” if examGrade >= 60

29

examGrade quizGrade Action


< 60 < 10
Case true false “We have a
1 problem”
Case true true “We have a
2 problem” and
“We have a real
problem”
Case false true/false “Ok”
3
Putting it all together

examGrade quizGrade Action


< 60 < 10
Case true false “We have a
1 problem”
Case true true “We have a
2 problem” and 30

int examGrade,quizGrade;
int examGrade, quizGrade; “We have a real
if (examGrade
(examGrade< <60)
60) {
System.out.println(“We have a problem”);
problem”
cout << “We have a problem” << endl;
if (quizGrade < 10)< 10)
Case if false have true/false
(quizGrade
System.out.printl(“We “Ok”
a real problem”);
cout << “We have a real problem” << endl;
3}elseSystem.out.println(“Ok”);
else
cout << “Ok”;
boolean Operators
● Combines multiple boolean expressions
● If person’s age greater than or equal to 13 and
less than 17, he can go to G and PG-13 rated
movies, but not R rated movies
● if (age >= 13 && age < 17)
cout << “You can go to G and PG-13”
<< “ rated movies, but not R” +
<< “ rated movies.”) << endl;
● boolean operators 31

○ and - && (true if all conditions are true)


○ or - || (true if one condition is true)
○ not - ! (true if conditions is false)
Expression Combinations

The && (and) operator

Let age = 17
opera operan operand1 &&
nd1 d2 operand2
true true true
Let age = 16
true false false
false true false
false false false
Let age = 12

if (age >= 13 && age < 17)


cout << “You can go to G and PG-13”
<< “ rated movies, but not R” +
<< “ rated movies.” << endl;

32
Expression Combinations

The || (or) operator

opera operan operand1 ||


nd1 d2 operand2
true true true
true false true
33

false true true


false false false
Example
The ! (not) operator if ( !( grade == sentinelValue ) )
cout << "The next grade is "
opera ! Alternative:
<< grade << endl;

nd operan if ( grade != sentinelValue )


cout << "The next grade is "
d << grade << endl;

true false
false true
Playing Cards
● Exercise with playing cards
○ Numbers represent the rank and suit of cards
// Codes for suits
const int SPADES = 0;
const int HEARTS = 1;
const int DIAMONDS = 2;
const int CLUBS = 3;
34

// Codes for nonnumeric ranks


const int ACE = 1;
const int JACK = 11;
const int QUEEN = 12;
const int KING = 13;
Prints a Card Name
● Print “rank of suit”
● Consider just the rank part
if (rank == JACK)
cout << "Jack";
else if (rank == QUEEN)
cout << "Queen";
else if (rank == KING; Notice:
comparing rank
cout << "King"; 35
to a number of
else if (rank == ACE) different value

cout << "Ace";


else
cout << rank;
switch Multiple-Selection Structure

● switch
○ Test variable for multiple values

○ Series of case labels and optional default case


switch ( variable ) {
case value1: // taken if variable == value1
statements
break; // necessary to exit switch

case value2:
36
case value3: // taken if variable == value2 or ==
value3
statements
break;

default: // taken if variable matches no other cases


statements
break;
}
switch Multiple-Selection Structure

true
case a case a action(s) break

false

true
case b case b action(s) break

false 37

.
.
.

true
case z case z action(s) break

false

default action(s)
Converting if/else to a switch
switch (rank)
{
case JACK:
cout << "Jack";
if break;
(rank == JACK)
case QUEEN:
cout
cout<<
<<"Queen";
"Jack";
break;
case KING:
cout << "King";
elsebreak;
if (rank == QUEEN)
case ACE:
cout <<
cout << "Queen";
"Ace";
break;
default:
cout << rank;
} else if (rank == KING; 38

cout << "King";

else if (rank == ACE)


cout << "Ace";

else
cout << rank;
Complexity of an algorithm is a measure of the amount of time
and/or space required by an algorithm for an input of a given size (n).
Three types of complexity could be considered when analyzing algorithm
performance. These are worst-case complexity, best-case complexity, and
average-case complexity. Only worst-case complexity has found to be
useful.
Time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of the length of the input. Similarly, Space
complexity of an algorithm quantifies the amount of space or memory
taken by an algorithm to run as a function of the length of the input.
or any defined problem, there can be N number of solution. This is true in
general. If I have a problem and I discuss about the problem with all of my
friends, they will all suggest me different solutions. And I am the one who has
to decide which solution is the best based on the circumstances.
Similarly for any problem which must be solved using a program, there can be
infinite number of solutions. Let's take a simple example to understand this.
Below we have two different algorithms to find square of a number(for some
time, forget that square of any number n is n*n):
One solution to this problem can be, running a loop for n times, starting with
the number n and adding n to it, every time.
/*

we have to calculate the square of n

*/

for i=1 to n

do n = n + n

// when the loop ends n will hold its square

return n
Or, we can simply use a mathematical operator * to find the square.
/*

we have to calculate the square of n

*/

return n*n

In the above two simple algorithms, you saw how a single


problem can have many solutions. While the first solution
required a loop which will execute for n number of
times, the second solution used a mathematical operator * to
return the result in one line. So which one is the better approach,
of course the second one.

What is Time Complexity?


Time complexity of an algorithm signifies the total time required by
the program to run till its completion.
The time complexity of algorithms is most commonly expressed using
the big O notation. It's an asymptotic notation to represent the time
complexity. We will study about it in detail in the next tutorial.
Time Complexity is most commonly estimated by counting the
number of elementary steps performed by any algorithm to finish
execution. Like in the example above, for the first code the loop will
run n number of times, so the time complexity will be n atleast and as
the value of n will increase the time taken will also increase. While for
the second code, time complexity is constant, because it will never
be dependent on the value of n, it will always give the result in 1 step.
And since the algorithm's performance may vary with different types
of input data, hence for an algorithm we usually use the worst-case
Time complexity of an algorithm because that is the maximum time
taken for any input size.

Types of Notations for Time Complexity


Now we will discuss and understand the various notations used for
Time Complexity.

1. Big Oh denotes "fewer than or the same as" <expression>


iterations.
2. Big Omega denotes "more than or the same as"
<expression> iterations.
3. Big Theta denotes "the same as" <expression> iterations.
4. Little Oh denotes "fewer than' <expression> iterations.
5. Little Omega denotes "more than' <expression> iterations.
Understanding Notations of Time Complexity with
Example

O(expression) is the set of functions that grow slower


than or at the same rate as expression. It indicates the
maximum required by an algorithm for all input values. It
represents the worst case of an algorithm's time
complexity.

Omega(expression) is the set of functions that grow


faster than or at the same rate as expression. It indicates
the minimum time required by an algorithm for all input
values. It represents the best case of an algorithm's time
complexity.

Theta(expression) consist of all the functions that lie in


both O(expression) and Omega(expression). It indicates
the average bound of an algorithm. It represents the
average case of an algorithm's time complexity.
Variables
The most simple form of storage is called a variable. It's an area of
memory that stores one item of data, such as a number or a character.
They have two purposes - the programmer is able to choose the names
of the variables, making programming easier, and also, you can write
programs or functions that will work with any values. If you're familiar
with spreadsheets already, you can think of variables as being like the
cells, which you can then use in formulae regardless of the values they
contain. All procedural programming languages, such as C, BASIC and
Pascal, have variables, although they may support different types and
let you manipulate them in different ways.
Some languages are strongly typed (see below), whereas others aren't
typed at all. Some require that you declare a variable before you use it,
and others let you go straight in and define a variable's value without
declaring it first.
Integer (int) Integers are whole numbers, and integer variables are used when you know there is never going to be anything after the decimal point, e.g. if you're writing a lottery ball

generator, all the balls have whole numbers on them. The difference between short integers, integers and long integers is the number of bytes (see the number bases section for
Declaring a variable gives the variable a name, and, in most
Short integer details) used to store them. This will vary according to the operating system and hardware you're using, but these days you can assume that an integer will be at least 16 bits,

programming languages, gives it a type - in effect it creates the


and a long integer is probably at least 32. In a 32-bit environment, it is more efficient to use long integers (i.e. a whole word), and so many compilers will automatically use long

container that stores your value. See the Type section for examples of
Long integer integers unless you specify a short one.

declarations.
When you define a variable, you are simply giving it a value.
Type
Most procedural programming languages support some sort of typing -
that is variables can only store one type of value. Anyone who has
created a database will be familiar with this idea - each field in an
Access database is also given a type, be it number, text, memo, date,
etc. The types supported will vary from language to language, but will
include some, or all, of the following (and maybe more!):
Float Floating point numbers are ones that contain fractional parts - i.e. they are not
whole numbers. The single and double quantifiers are analagous to the short and
Single long quantifiers used with integers - i.e. they indicate how many bits are used to
store the variable. Floating point arithmetic can lead to problems with rounding
Double and precision, so if you're dealing with a limited number of decimal places, it is
probably more efficient to use integers and multiply all your values by a power of
10. For example, if you're dealing with money, it's probably better to work in pence
and use integers than to work in pounds and use floating point variables.

Char A char variable is a common sight in C or C++ programs (which can't handle
strings), and is used to store a single text character. The value it actually stores is
an integer representing the code (e.g. ASCII) for the character represented.

Boolean A boolean variable can store one of two values - either TRUE or FALSE.
Like char, this is usually an integer - in VisualBASIC, for example, FALSE is 0 and
TRUE is -1, and the TRUE and FALSE values themselves are constants (see
below).
Fixed- Strings are variables that contain text, and they come in two sorts. With a fixed-
length length string, you declare how many characters the string is going to hold. Certain
string API calls in Windows require the use of fixed-length strings, but generally they are
not used in BASIC. In C they are implemented as an array (or vector) of chars.
Variable- A variable-length string is one where you don't define the length. This is the
length default type in BASIC, and is useful for taking user input where you don't know
string what the response will be. The maximum length of the string will depend on your
environment, but it should be at least 255 characters.

These types may go by different names in different languages, for example an integer in one might be a short in another, or
a single in one might be a float elsewhere.
Note that there is no string type in C, but that you can use a char pointer
(char* - I'm not going to go into the complexities of pointers and
references here!), and nor is there a Boolean type. I've used a fixed-
length string in the BASIC example to represent a single character
(either M or F).
BASIC also allows you to declare the type of a variable by using a suffix
on its name, e.g. $ for strings, % for integers, etc.. In the above
example, I could have defined name$ and age%, and the variables
would have been set to set to a string and an integer automatically.
See how variables and types are used in Python on the
Advanced ICT YouTube channel.
Some languages, such as JavaScript, are not typed, but still have a
declaration statement, e.g.:
var name, age, height, sex, married;
This might sound easier, but can lead to confusing results when it can't
decide whether your variables are numbers or strings. For example, say
you have two variables, a = 123 and b = 456 - what would you expect c
= a + b to give you? The + operator can also be used to concatenate
strings, so the value of c will depend on the context. Sometimes c will
be 579, and sometimes it might be 123456! To be sure, you'd have to
use something like c = (a * 1) + (b * 1) (for addition) or c = a + "" + b (for
string concatenation).
Scope
A variable can't always be used throughout the whole of
your program - they have something called a scope which
determines where a value can be read or changed.
Global variables are variables that can be used through
your program - that is, the scope of a global variable is the
entire application. Most variables, however, will be local -
local variables can only be used in the function (or
procedure) in which they were declared, or any other
function called by that function. Scope, therefore, is
hierarchical, and generally only applies downwards (from
the main body of the program, to the functions it calls, and
from functions down to further sub-functions).
This means that if you give a variable at the top of your
program, you can't declare another variable with the same
name in a function. However, if you declare a variable in
one function, you can declare another variable with the
same name in another function, and they will effectively be
different variables and can have different variables.
Finally, by default a variable declared in a function only
exists for the time that the function is running - each time
you call the function, the variable is re-declared and reset. If
this isn't what you want, you can declare your variable as
a static - this means that its value will persist after the
function finishes, and its value will be the same next time
the function is run. Static variables still have a type (e.g.
integer, float, etc.).

You might also like