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

Data Structures

Uploaded by

smyyan.shah7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Structures

Uploaded by

smyyan.shah7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

: Mrs. Rama M.

Warang
 A data structure is a particular way of storing and organizing
data in a computer so that it can be used efficiently.

 The model should reflect the actual relationships of data in


real world.

 It should be simple enough so that data can be processed


whenever required.

 Eg., Arrays, Records, Link Lists, Trees, Stacks, Queues.


 Data: They are simply values or set of values.
 Information: Data that is meaningful. Data with
given attributes.
 Data item: Single unit of value.
 Group items: Data items that can be subdivided
into sub items are called group items.
 Elementary items: Data items that cannot be
subdivided into sub items are called elementary
items.
eg., Name is group item & Pincode is
elementary item.
 Entity: It is something that has attributes or
properties which may be assigned values. Values
can be numeric or non-numeric.
Eg., Employee is an entity.

 Entity Set: Collection of all entities form an


entity set.
Eg., All employees of an organization.
 Field: It is a single elementary unit of information
representing an attribute of an entity.
Eg., Name, Age, Sex values for an employee

 Record: It is a collection of field values for an


entity.
Eg., All the above fields together gives one record
of that employee.

 File: It is a collection of all records of entities.


Eg., All records of employees form a file.
 Primary Key: A field with a unique value is called
primary key.
Eg., Employee ID is a primary key
 Fixed length records: A file where all the records
are of the same length is said to have fixed length
records.
Advantage: Access is fast because the computer
knows where each record starts.
 Variable length records : One or more of the fields
can be of differing lengths in each record,
called variable length records
Advantage: Records will be smaller and will need
less storage space.
 Arrays: An array stores a number of elements of the
same type in a specific order. They are accessed
using an integer to specify which element is required.
Arrays may be fixed-length or expandable.
 Records: A record is collection of fields & may
contain elements of different type.
 Link Lists: A linked list consists of a group
of nodes which together represent a sequence. Each
node is composed of a datum and a reference (in
other words, a link) to the next node in the sequence.
 Trees: A tree is a non-linear data structure representing
hierarchical relationship between elements.
 Stacks (LIFO): A stack is a linear data structure where
items are added or removed at one end.
 Queues (FIFO): A queue is a linear data structure
where items are added at one end and removed at other
end.
 Graphs: A graph represents relationship between pair
of elements.
Six operations:
 Traversing: Accessing each record (element) only once,
so that it can be processed.
 Searching: Finding location of each record (element)
with a given key or finding all records that satisfy a given
condition.
 Sorting: Arranging records (elements) in some logical
order.
 Merging: Combining records from two different files
into one single file.
 Inserting: Adding new record (element).
 Deleting: Removing existing record (element).
It is a finite step-by-step list of well defined
instructions for solving a particular problem.

 1st Part: Purpose of program & list of variables and


input data is given.
 2nd Part: List of steps are given so as to execute
sequentially. Control may be transferred to step n by
using statement like ‘Go to step n’.
 3rd Part: Exit/Stop statements are given that
completes the algorithm.
 Data may be assigned by using read statement and
data can be displayed using write statement.
3 types of control flow :

 Sequential flow (Sequential Logic)

 Conditional flow (Selection Logic)

 Repetitive flow (Iteration Logic)


Sequential flow (Sequential Logic)
 Modules(set of steps) are executed one after the
other.

Module A
Module B
Module C
.
.
.
Conditional flow (Selection Logic)
 One or more modules(set of steps) are selected
depending on condition.

There are 3 conditional structures.


1. Single Alternative:
If condition, then:
[statement_Set]
[End of IF structure]
2. Double Alternative:
If condition then:
[ statement_Set1]
else
[statement_Set2]
[End of IF structure]
3. Multiple Alternative:
If condition then:
[ statement_Set1]
else if condition then:
[statement_Set2]
else if condition then:
[statement_Set3]
else
other_statements
[End of IF structure]
Repetitive flow (Iteration Logic)
 Certain module is executed repeatedly until condition
satisfies.
There are 2 repetitive structures.

1. Repeat while loop:


Repeat while condition:
[Module]
[End of WHILE loop]
2. Repeat for loop:
Repeat for k=R to S by T
[Module]
[End of FOR loop]
Consider the array A, which records no. of automobiles
from 1932 to 1984. Suppose Base(A)=200 and W=4
words. Then, LOC(A[1932])=200, LOC(A[1933])=204,
LOC(A[1934])=208. Calculate address at which 1965’s
record is stored.

Sol: Given : K=1965, Base(A)=200, W=4, LB= 1932


According to the formula,
LOC(A[1965]) = Base(A)+W(K-LB)
= 200+4(1965-1932)
= 200+4(33)
=200+132=332
[Linear Search] LINEAR(DATA,N,ITEM,LOC)
Here DATA is a linear array with N elements and ITEM is given element. This
algorithm finds the location LOC of ITEM in DATA or sets LOC=0, if search
is unsuccessful.

Step1: [Insert ITEM at the end of DATA]


Set DATA[N+1]:=ITEM
Step2: [Initialize counter]
Set LOC:=1
Step3: [Search for ITEM]
Repeat While DATA[LOC]≠ITEM
Set LOC:=LOC+1
[End of loop]
Step4: If LOC=N+1
Set LOC:=0
Step5: Exit
• An array is called pointer array, if each element of the array is a
pointer.
• The variable is called pointer variable, if it points to another
variable i.e., it contains memory address of another variable.
Pointer array(array of pointers):
• An array is called pointer array, if each element of the array is a
pointer.

Pointer to an array:
• Pointer to an array is a pointer variable which holds the address of
the element in the array.

Example –
• An array of 8 pointers to float
float *a[8];

• A pointer to an array of 8 floats


float a[8], * ptr;
ptr=&a[0];
• A record is a collection of relative data items, each of
which is called as field or attribute.
• A record may contain non-homogeneous data i.e., data
items of the record need not be of the same data type.
• Elements in the record can be described by level
number.
• Eg., An organization keeps records of its employees. It
contains data items – Name, Sex, Address, Phone No.
Name is a group item consisting of first name and last
name. Also, Address is a group item consisting of city
and pin code .
• The structure of record -
1. Employee
2. Name The number to the left of each
3. First Name variable indicates level number.
3. Last Name Employee(30)
2. Sex This indicates file of 30 records.
2. Address To access first name of 3rd
3. City employee, we should write
3. Pin code Employee(3).Name.First Name
2. Phone No. In this way, we can access
variables in records.
• The structure of record -
1. Employee To represent records in memory,
2. Name linear arrays are used.
3. First Name One separate linear array for
3. Last Name
2. Sex
each elementary item.
2. Address The records are stored in
3. City memory using parallel linear
3. Pin code arrays.
2. Phone No.

If one wants to access Kth record, he will


access First Name[K], Last Name[K], Sex[K]..
Difference between records and linear arrays

Records Linear Arrays


It is a collection of It is a linear list of
fields items
It may contain non- It always contains
homogeneous data homogeneous data
Natural ordering of Array elements are
elements not possible naturally ordered
Record elements are Array elements are
referenced by level referenced by an index
number set consisting of n
consecutive numbers
Linked List
Linked List
Difference between Linked list and linear array

Linked List Linear Array


Consecutive memory Consecutive memory
locations not required locations required
Dynamic size Fixed Size
Memory is not wasted as it is Memory may be wasted if
allocated dynamically array is not full
Insertion and deletion are Insertion and deletion are
efficient inefficient
i.e., No shifting required i.e., elements need to be
shifted
Representation of Linked list in memory
Stacks & Queues
Tree
• A tree is a nonlinear data structure, compared to arrays, linked
lists, stacks and queues which are linear data structures.

• A tree can be empty with no nodes or a tree is a structure


consisting of one node called the root and zero or one or more
subtrees.
Tree
• Root – A node which has no parent. Generally, first node is
root node.
• Leaf – The node which has no child or children. It is also
called terminal node.
• Child – The node which is reachable from a node is called
child node.
• Siblings – Children of same parent are called siblings.
Tree
• Level – In a tree, each step from top to bottom is called as
level of a tree. The level count starts with 0 and increments by
1 at each level or step. Here level of root is 0.
• Depth/Height – Height of a tree is the maximum level of any
node in the tree. If the root is level 0 then depth or height of
tree is equal to 1+ largest level number.
• Degree – The number of subtrees of a node is called degree of
a node. The maximum degree of node in a tree is called degree
of tree.
Binary Tree

In Binary tree, there is no node with degree greater than two.


Basic terminology describing family
relationship between elements of Binary Tree

The binary tree has 9 nodes (A to I). Root A is at the top of the tree.
• Left successor – B is the left successor of node A.
• Right successor – C is the right successor of node A.
• Left subtree – Left subtree consists of nodes B,D,E,G.
• Right subtree – Right subtree consists of nodes C,F,H,I.
• Terminal node – The node with no successor is called terminal node. D,G,H,I
are terminal nodes.
Any node N in a binary tree T has either 0,1 or 2
successors.
Relation between total no. of nodes & depth of
Binary Tree
Types of Binary tree
• Complete Binary Tree - A complete
binary tree is a binary tree in which all the
levels are completely filled except possibly
the lowest one, which is filled from the left.
• Extended binary tree or 2-tree –
Extended binary tree is a binary tree in
which every node has 0 or 2 children. The
nodes with 2 children are called internal
nodes. The nodes with 0 children are called
external nodes.
• Binary search tree – Binary search tree is
a binary tree in which each node N has the
property that its value is greater than every
node value in left subtree of N and is less
than or equal to every node value in right
subtree of N
Representation of Binary tree in Memory
Representation of Binary tree in Memory
Drawing a Binary tree for given expression
Drawing a Binary tree for given expression

You might also like