Data Structures

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Type of Data Structure

Primitive data structure DATA STRUCTURES


Non-primitive data structure

Integer Linear data structure Non-linear data structure

Float Arrays Trees

Character Linked list Graphs

Boolean Stack

Queue

1. Introduction to Data Structures


1.1] What are the Data Structures?
― Data Structures are the different tools which used for storing
and organizing the data. The efficiency of any data processing is
directly proportional to how efficiently do we manage to store
the data.
― Every data is different and hence we cannot have same
organization for data. So, depending upon the requirement of
the system and the data to be processed we need to select the
respective data structure to store the data.
1.2] Primitive Data Structure (not necessary)
― They are basic structure and directly operated by machine
instructions.
― Primitive data structure is the data structure that allows you to
store only single data type values.
― Example – int, float, char
1.3] Non-primitive Data Structure (not necessary)
― These are derived from the primitive data structure.
― Non-Primitive data structure is a data structure that allows you
to store multiple data type values.

1
DATA STRUCTURES
― It is collection of same type or different type primitive data
structure.
― Example – Array, stack, tree.
1.4] Linear and Non-linear Sata Structure
Parameter Linear Data Non-Linear Data
Structure Structure
Definition The data items are It arranges the data
arranged in orderly in sorted order and
manner where the there exists a
elements are relationship between
attached adjacently. data and elements.
Transversing of data The data element Transversing of data
can be accessed in elements in one go is
one time. not possible.
Ease of Simpler Complex
implementation
Level involved Single level Multiple level
Example Array, stacked, Tree and graph
queue, linked list,
etc.
Memory utilization Ineffective Effective

1.5] Operation on Data Structure


The data which is stored in our data structure are processed by
some set of operation.
1) Insertion ― Add new data in data structure.
2) Deleting ― Remove the data from the data structure.
3) Sorting ― Arrange the data in increasing or decreasing order.
4) Searching ― Find the location of detain data structure.

2
DATA STRUCTURES
5) Merging ― Combining the data of two different sorted files into
single stored file.
6) Traversing ― Accessing each data exactly one in the data
structure so that each data item is traversed or visited.
1.6] Abstract Data Types (ADT)
― Abstract data types are used for defining the data structure
along with the set of operations that may or may not be
performed on that data structure.
― The aim of the ADT is to help the programmer understand the
structure of the Data structure for which the ADT is being
written. An ADT comprises of:
1) Value Definition ― Value definition defines the structure
(memory allocation) of the data structure for which the
ADT is being written. It helps the programmer to visualize
the memory organization of the data structure.
2) Operator Definition ― Operator definitions comprise of
the list of operations or functions that can be or cannot be
performed on the data structure for which the ADT is
being written. If needed the operator definition can
include,
2.1) Pre-Conditions ― These are the conditions that
should hold true for the operation to start successfully on
the data structure.
2.2) Post-Conditions ― These are the conditions which will
hold true after the successful completion of the
operation/function on the data structure for which the
ADT is being written.
― Depending on the magnitude and requirements of the system
to be designed, we can add or reduce the number of operations
or functions to be included in the operator definition. ADT are
not unique for the different data structures.
― Example : Represent a rational number as an ADT.

3
DATA STRUCTURES
Abstract typedef<integer,integer>rational
Condition rational[1] !=0
Abstract Rational makeRational(a,b)
Int a,b;
postcondition rational[0] = a;
rational[1] = b;
1.7] Recursion
― When a function call itself is called as recursive function.
Whenever we use recursive function, we have to define the
stopping condition so that unlimited function call inside
function is avoided.
― Example :
#include<stdio.h>
#include<conio.h>
Int factorial(int x);
Void main()
{
int n,fact;
clrscr();printf(“Enter any number”);scanf(“%d”,&n);
fact=factorial(n);
printf(“Factorial = &d”,fact);getch();
}
int factorial(int x)
{
if(x==0)
{return(1);}
else
{return(x*factorial(x-1));}
}
1.7.1] Advantages of Recursion

4
DATA STRUCTURES
1) It allows programmers to take advantages of the repetitive
structure present in many problems.
2) Complex case analysis and nested loops can be avoided.
3) It can lead to more readable and efficiency algorithm
description.
4) It is useful way for defining object that have a repeated
similar structure form.
1.7.2] Disadvantages of Recursion
1) slowing down execution time and storing on the run-time
more things than required approach are major limitation of
recursion.
2) If recursion is too deep, then there is a danger of running out
of space on the stack and ultimately program crashes.
3) Even if some recursive function repeats the computation for
some parameters, the run time can be prohibitively long
even for very single case.

2. Stacks
2.1] Introduction to Stack
― Computer System use large amount of data in modern day
processing. The efficiency of the system relies heavily on the
organization of the data, as the insertion, modification, update
and retrieval of data leads to a faster and a better system.
― Every type of processing needs a different kind of data
organization, hence the idea of various data structure.
― One of most widely used data structure amongst these is stack
organization.
― Definition: - Stack is organization that uses processing Last In
First Out (LIFO). The data is added at the end is always remove
first for processing.

5
DATA STRUCTURES
― Chain placing one over the other, (stack) stand of the CDs,
books placed one over another.
― For creation of stack, we need a data storage like an array or
linked list and variable pointer which will always keep pointing
to the last value added in this data.
2.2] Operation on Stack
1) Push() : Insertion/addition a value at the stack top.
2) Pop() : Deleting a value from the stack top.
3) Peep() : It returns the value from the stack top but does not
delete the value.
4) isEmpty() : It returns output as there if the stack is empty, else
returns false.
5) isFull() : It returns output as true if the stack is full, else it
returns false.
― There are two ways for memory allocation for the stack : Static
and Dynamic
1) Static memory allocation is done with the use of array.
2) Dynamic memory allocation is done with the use of linked
list.
2.3] ADT of a Stack
Abstract typedef < eltype > stack(s)
Abstract empty (s)
Stack < eltype > s
Postcondition empty == true
If (length(s)==0)
Abstract full (s)
Stack < eltype > s
Precondition empty == false

6
DATA STRUCTURES
Postcondition = true, If (length (s) == max capacity of the storage)
Abstract eltype pop (s)
Stack (eltype) s
Precondition empty (s) = false
Postcondition pop = first (s)
Abstract eltype push (x,s)
Stack (eltype) s
eltype x
postcondition: s = s + < x >
2.4] Implementing push, pop, peep, display operation on stack
using an array.
1) Push operation on stack
Step 1: If TOP=SIZE-1
PRINT“STACK OVERFLOW”
Go to the step 4
[END OF IF]
Step 2: SET TO=TOP+1
Step 3: SET STACK[TOP]=VALUE
Step 4: END
2) Pop operation on stack
Step 1: If TOP=-1
PRINT”STACK UNDERFLOW”
Go to the step 4
[END OF IF]
Step 2: SET VALUE=STACK[TOP]
Step 3: SET TOP=TOP-1
Step 4: END
3) Peep operation on stack
Step 1: If TOP=-1

7
DATA STRUCTURES
PRINT”STACK UNDERFLOW”
Go to the step3
[END OF IF]
Step 2: RETURN STACK[TOP]
Step 3: END
4) Display operation on stack
Step 1: If TOP=1
PRINT”STACK UNDERFLOW”
Go to the step 3
[END OF IF]
Step 2: For i=0 to i<=TOP
PRINT STACK [i]
[END OF FOR]
Step 3: END
2.5] Application of Stack
― Most important application of stack is Expression conversion.
― Any compiler, while evaluating the expression convert it from its
original infix form to its equivalent prefix or postfix form before
evaluating it immediately. A stack is used for this conversion.
1. Infix Expression
An expression where the operator are placed in the middle of
the operands is called as an infix expression.
Example: 2+3*5
2. Post Expression
An expression where the operator are placed after the operand
is called as an postfix expression.
Example: 235*+
3. Prefix Expression

8
DATA STRUCTURES
An expression where the operators are placed before the
operand is called as a prefix expression.
Example: +2*35
2.5] Converting Infix Expression to Postfix Expression
● Steps
1. Scan the infix string from left to right
2. Initialize an empty stack.
3. If the scanned character is an operand, add it to the postfix
string. If the scanned character is an operator and stack is
empty Push the character to the stack. if the scanned character
is an operand and stack is not empty, compare the precedence
of the character with element on the top of the stack (stack
top). If the stack top has higher precedence over the scanned
character pop the stack else push the scanned character to
stack. Repeat the step as long as the stack is not empty and
stack top has precedence over the character.
4. Repeat this step 3 till all the character are scanned.
5. If stack is not empty and stack top to postfix string and pop the
stack. Repeat this step as long as stack is not empty.
6. Return the postfix string.
● Algorithm
Step 1: INITIALISE INFIX [20], POSTFIX [20], STACK [20]
Step 2: FOR (i=0 to INFIX [i]!=‘\0’)
Step 3: IF INFIX [i] IS OPRAND
Step 4: SET j=j+1;
Step 5: SET POSTFIX [j] = INFIX [i]
Goto Step 25
[END OF IF]
Step 6: IF top = -1
Step 7: SET top = top + 1
Step 8: SET STACK [top] = INFIX [i]

9
DATA STRUCTURES
Goto Step 25
[END OF IF]
Step 9: IF INFIX [i] = ‘)’
Step 10:WHILE (STACK [top] != ‘(’ )
Step 11:SET j=j+1
Step 12:SET POSTFIX [j] = STACK [top]
Step 13:SET top = top – 1
[END OF WHILE]
Step 14:SET top = top – 1
Goto Step 25
[END OF IF]
Step 15:IF PRIORITY (INFIX [i] > PRIORITY (STACK [top])
Step 16:SET top = top + 1
Step 17:SET STACK [top] = INFIX [i]
Goto Step 25
[END OF IF]
Step 18:WHILE (PRIORITY (INFIX [i] <= PRIORITY (STACK [top]) &&
TOP != -1 && STACK [top] != ‘(’ && INFIX [i] != ‘(’ )
Step 19:SET j = j + 1
Step 20:SET POSTFIX [j] != STACK [top]
Step 21:SET top = top – 1
Step 22:IF top – 1
Goto Step 25
[END OF IF]
[END OF WHILE]
Step 23:SET top = top + 1
Step 24:SET STACK [top] = INFIX [i]
Step 25:FOR LOOP CONTINUE
[END OF FOR]
Step 26:WHILE (top != -1)
Step 27:SET j = j + 1
Step 28:SET POSTFIX [j] = STACK [top]
Step 29:SET top = top – 1

10
DATA STRUCTURES
[END OF WHILE]
Step 30:SET POSTFIX [j + 1] = ‘\0’
Step 31:END
2.6] Algorithm to evaluate any Postfix Expression
● Steps
1. Scan postfix string from left to right.
2. Initialise an empty stack.
3. If scanned character is an operand, add it to the stack. If the
scanned character is an operator, there will be at least two
operands in the stack. If scanned character is an operator, then
pop the two pop most element from the stack, apply the
operator and push result onto the stack.
4. Repeat this step till all character are scanned.
5. After all character are scanned, we will have only one element
in the stack. Return stack top.
● Algorithm
Step 1: FOR (I =0 TO POSTFIX[i] != ‘\0’ )
Step 2: IF POSTFIX [i] IS OPERAND THEN
Step 3: SET top = top + 1
Step 4: SET STACK[top] = (int) (POSTFIX[i] – ‘0’ )
GOTO step 7
END OF IF
Step 5: SET a = STACK [top] , top = top – 1 , b = STACK [top]
Step 6: SWITCH (POSTFIX [i])
1. Case ‘+’ : SET c = b + a
STACK [top] = c
Break
2. Case ‘-’ : SET c = b – a
STACK [top] = c

11
DATA STRUCTURES
Break
3. Case ‘*’ : SET c = b * a
STACK [top] = c
Break
4. Case ‘/’ : SET c = b / a
STACK [top] = c
Break
[END OF SWITCH]
Step 7: CONTINUE FOR LOOP
[END OF FOR LOOP]
Step 8: PRINT STACK [top]
Step 9: END

___Tree___
● Binary tree – A tree is a nonlinear structure, which is used for the
representing any given hierarchy.
A binary tree can be created in the memory using array and linked
list.
● Binary tree traversal – A traversal technique define the logical
sequence in which the nodes are required to be visited in case of
binary tree.
1) Pre order traversal (V-L-R)
2) In order traversal (L-V-R)
3) Post order traversal (L-R-V)
● Binary search tree – A binary search tree is a tree in which every
node having value lesser than the root node will be added to the left

12
DATA STRUCTURES
subtree and every node with the value greater to the root node will
be added to the right subtree.
● Expression tree – While evaluating expression the compiler
converts the given expression to its prefix or post fix equivalent these
are then converted into the tree, which is an evaluated to generate
the result all such binary trees which are used for evaluating
expressions are called expression tree.
● B-tree –
B-tree of order m is an m-way tree (a tree where each node may
have up to m children) in which:
(1) The number of keys in each non-leaf node is one less than the
number of its children and these keys partition the keys in the
children in the fashion of search tree.
(2) All leaves are on the same level
(3) All non-leaf nodes except the root have at least (m/2) children
(4) A leaf node contains no more than (m+1) keys
● B+ tree –
― A balance trees
― Each node can have at most m keys and m+1 pointer field
― Half - field must be satisfied (except root node)
m is even and m=2d
o Leaf node half full: at least d entries
o Non leaf node half full: at least d entries
M is odd and m=2d+1
o Leaf node half full: at least d+1 entries
o Non leaf node half full: at least d entries.
● Huffman encoding
 Encoding of data refers to representing the data on terms of
zeros and ones.

13
DATA STRUCTURES
 Example

Using above code we will represent the following data


AABACCDBAAAACDB. The bit sequence representing the above
data will be 000 000 001 000 010 010 111 001 000 000 000 000
010 111 001 This will need 45 bits.
 But in above practical data, the sum characters are used more
than the others. Huffman algorithm works on a concept that
there cannot be a code of the same length for the characters
that appears frequently a character, that does not appear so
frequently.
 Huffman code are designed in such a manner that the code for
the character depends upon the frequency at which the
characters appears. The characters with highest frequencies
should get the smaller codes and the ones with lower
frequency can have longer code. This can be reduced the
number of bits required to represent the data considerably.
___Searching___

14
DATA STRUCTURES
● Linear search.
Linear search or sequential search is the method for finding a key
element within an array. It sequentially compares each element of an
array with key element until the match is found or the whole lost has
been searched.

15
DATA STRUCTURES

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy