Chapter 4 Data Structure PDF
Chapter 4 Data Structure PDF
Chapter 4 Data Structure PDF
Chapter-4
DATA STRUCTURES
Introduction:
Data Structure is the way of collecting and organizing the data in such a way that we can
perform operation on these data in an effective way.
For Example: We have the data students Name Akash and age 16. Here Akash is a string
type data type and 16 are of integer. We can now organize this data as a record like a Student
Record. We can collect and store other Students Record in a file or a database as a data structure.
Data Representation:
Computer memory is used to store the data which is required for processing. This process is
known as data representation.
Data may be of same type or different type.
Data Structure provides efficient way of combining these data types and process data.
Data Structure
Linked List
Primitive Data structures:
Data structures that are directly operated upon the machine-level instructions are known as
primitive data structures.
The integers, float, character data, pointers are primitive data structures.
1|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
3. Deletion: The process of removing an existing data element from the given collection of data
elements is called deletion.
4. Searching: The process of finding the location of a data element in the given collection of data
elements is called as searching.
5. Sorting: The process of arrangement of data elements in ascending or descending order is called
sorting.
6. Merging: The process of combining the elements of two structures to form a single structure is
called merging.
Arrays:
An array is an ordered collection of elements of same data type that share common name.
The elements are arranged one after the other in adjacent memory location.
These elements are accessed by numbers called subscripts or indices.
The elements are accessed using subscripts; arrays are also called as subscripted variables.
There are three types of arrays
i. One-dimensional Array
ii. Two-dimensional Array
iii. Multi-dimensional Array
3|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
1002 C A[2]
1003 D A[3]
1004 E A[4] Upper Bound(UB)
Traversing an array:
Traversing is the process of visiting each subscript at least once from the beginning element to
last element.
For example, to find the maximum element of the array we need to access each element of the
array.
ALGORITHM: Traversal (A, LB, UB) A is an array with Lower Bound LB and
Upper Bound UB.
Step 1: for LOC = LB to UB do
Step 2: PROCESS A [LOC]
[End of for loop]
Step 3: Exit
4|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
ALGORITHM: Insert (A, N, ITEM, Pos) A is an array with N elements. ITEM is the
element to be inserted in the position Pos.
Step 1: for I = N-1 down to Pos
A[ I + 1] = A[I]
[End of for loop]
Step 2: A [Pos] = ITEM
Step 3: N = N+1
Step 4: Exit
For example: Let A[4] be an array with items 10, 20, 30, 40, 50 stored at consecutive locations.
Suppose item 30 has to be deleted at position 2. The following procedure is applied.
o Copy 30 to ITEM, i.e. Item = 30.
o Move Number 40 to the position 2.
o Move Number 50 to the position 3.
ALGORITHM: Delete (A, N, ITEM, Pos) A is an array with N elements. ITEM is the
element to be deleted in the position Pos and it is stored into variable Item.
Step 1: ITEM = A [Pos]
Step 2: for I = Pos down to N-1
A[ I ] = A[I+1]
[End of for loop]
Step 3: N = N-1
Step 4: Exit
Linear Search:
This is the simplest method in which the element to be searched is compared with each element of
the array by one from the beginning to end of the array.
Searching is one after the other, it is also called as sequential search.
6|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
LOC = I
Goto Step 3
[End if]
[End of for loop]
Step 3: if ( LOC >= 0 ) then
Print Element, Found in Location, LOC
else
Print Element, Not Found
Step 4: Exit
Example: Consider the following elements stored in an array and we are searching for the element
17. The trace of the algorithm is given below.
Index (I) Compare 17 Location
17 = 12
I=0 LOC = -1
(Does not match)
A[0] A[1] A[2] A[3] A[4] 17 = 23
I=1 LOC = -1
12 23 9 17 7 (Does not match)
17 = 9
I=2 LOC = -1
(Does not match)
17 = 17
I=3 LOC = 3
(Matches)
Binary Search:
When the elements of the array are in sorted order, the best method of searching is binary search.
This method compares the element to be searched with the middle element of the array.
If the comparison is searched either at the right half of the array or at the left half of the array.
Let the position of first (BEG) and last (END) elements of the array. The middle element A[MID]
can be obtained by find the middle location MID by MID= (BEG+END)/2.
If A[MID]= ELE, then search is successful. Otherwise a new segment is found as follows:
o If ELE<A[MID], searching is continued at left half of the array. Reset END=MID-1.
o If ELE>A[MID], searching is continued at right half of the array. Reset BEG=MID+1.
o If ELE not found then we get a condition BEG>END. This results in unsuccessful search.
MID = (BEG+END)/2
if ( ELE = A [ MID ] ) then
LOC = MID Important
Goto Step 3
else
if( ELE < A[MID])
END=MID-1;
else
BEG=MID+1;
[End if]
[End if]
[End of While loop]
Step 3: if ( LOC >= 0 ) then
Print Element, Found in Location, LOC
else
Print Element, Search is Unsuccessful
Step 4: Exit
Example: Consider the following elements stored in an array and we are searching for the element
47. The trace of the algorithm is given below.
MID =
BEG & END Compare Location
(BEG+END)/2
The search element i.e. 47 is greater than the element in the middle position i.e. 39 then
continues the search to the right portion of the middle element.
Since the element in the middle position is the same as the search element LOC gets the value 3.
Since the value of LOC is greater than 0, we get the output as 47 Found in location 3
8|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Example: Consider the following elements stored in an array and we are searching for the element
67. The trace of the algorithm is given below.
MID =
BEG & END Compare Location
(BEG+END)/2
A[0] A[1] A[2] A[3] A[4]
67>39
12 23 39 47 57 BEG = 0 MID = (0+4)/2 (Does not LOC = -1
END = 4 MID = 2
BEG MID END match)
The search element i.e. 67 is greater than the element in the middle position i.e. 39 then
continues the search to the right portion of the middle element.
The search element i.e. 67 is greater than the element in the middle position i.e. 47 then
continues the search to the right portion of the middle element.
A[0] A[1] A[2] A[3] A[4]
12 23 39 47 57 67>57
BEG = 4 MID = (4+4)/2 (Does not LOC = -1
BEGMID END = 4 MID = 4
match)
END
The search element i.e. 67 is greater than the element in the middle position i.e. 57 then
continues the search to the right portion of the middle element.
9|Page
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Insertion Sort:
In Insertion sort, the first element of the array is assumed to be in the correct position next element
is considered as the key element and compared with the elements before the key element and is
inserted in its correct position.
Example: Consider the following array contains 8 elements as follows:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
45 26 23 56 29 36 12 4
10 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Column-Major Method: All the first column elements are stored in sequential memory locations
and then all the second-column elements are stored and so on.
11 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Advantages of Array:
It is used to represent multiple data items of same type by using single name.
It can be used to implement other data structures like linked lists, stacks, queues, tree, graphs etc.
Two-dimensional arrays are used to represent matrices.
Many databases include one-dimensional arrays whose elements are records.
Disadvantages of Array:
We must know in advance the how many elements are to be stored in array.
Array is static structure. It means that array is of fixed size. The memory which is allocated to
array cannot be increased or decreased.
Array is fixed size; if we allocate more memory than requirement then the memory space will be
wasted.
The elements of array are stored in consecutive memory locations. So insertion and deletion are
very difficult and time consuming.
STACKS:
A stack is an ordered collection of items in which an element may be inserted or deleted only at
same end.
This end is called as the top of the stack.
The end opposite to top is known as the base.
Stacks are sometimes known as LIFO (Last In First Out).
Operation on Stacks:
Stack( ): It creates a new stack that is empty. It needs no parameter and returns an empty stack.
push(item): It adds a new item to the top of the stack.
pop( ): It removes the top item from the stack.
peek( ): It returns the top item from the stack but does not remove it.
isEmpty( ): It tests whether the stack is empty.
size( ): It returns the number of items on the stack. Important
PUSH Operation:
The process of adding one element or item to the stack is represented by an operation called as
the PUSH operation.
The new element is added at the topmost position of the stack.
ALGORITHM: PUSH (STACK, TOP, ITEM) STACK is the array with N elements. TOP is the
pointer to the top of the element of the array. ITEM to be inserted.
Step 1: if TOP = N-1 then [Check Overflow]
PRINT STACK is Full or Overflow
Exit
[End if]
Step 2: TOP = TOP + 1 [Increment the TOP]
Step 3: STACK[TOP] = ITEM [Insert the ITEM]
Step 4: Return
POP Operation:
The process of deleting one element or item from the stack is represented by an operation called
as the POP operation.
ALGORITHM: POP (STACK, TOP, ITEM) STACK is the array with N elements. TOP is the
pointer to the top of the element of the array. ITEM to be inserted.
Application of Stacks:
It is used to reverse a word. You push a given word to stack letter by letter and then pop letter
from the stack.
Undo mechanism in text editor.
Backtracking: This is a process when you need to access the most recent data element in a series of
elements. Once you reach a dead end, you must backtrack.
Language Processing: Compiler syntax check for matching braces in implemented by using stack.
Conversion of decimal number to binary.
Conversion of infix expression into prefix and postfix.
Quick sort
Runtime memory management.
Arithmetic Expression:
An expression is a combination of operands and operators that after evaluation results in a single
value.
Operand consists of constants and variables.
Operators consists of {, +, -, *, /, ), ] etc.
Expression can be
o Infix Expression
o Postfix Expression
o Prefix Expression
Infix Expression: If an operator is in between two operands, it is called infix expression.
Example: a + b, where a and b are operands and + is an operator.
Postfix Expression: If an operator follows the two operands, it is called postfix expression.
Example: ab +
Prefix Expression: an operator precedes the two operands, it is called prefix expression.
Example: +ab
14 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
15 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Sl No Infix Prefix
QUEUES:
A queue is an ordered collection of items where an item is inserted at one end called the rear
and an existing item is removed at the other end, called the front.
Queue is also called as FIFO list i.e. First-In First-Out.
In the queue only two operations are allowed enqueue and dequeue.
Enqueue means to insert an item into back of the queue.
Dequeue means removing the front item.
REAR FRONT
Dequeue
Enqueue
Types of Queues:
Queue can be of four types:
o Simple Queue
o Circular Queue
o Priority Queue
o Dequeue ( Double Ended Queue)
Simple Queue: In simple queue insertion occurs at the rear end of the list and deletion occurs at
the front end of the list.
Front Rear
A B C D Front = 1
Rear = 4
0 1 2 3 4 5
16 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Operation on Queues:
Queue( ): It creates a new queue that is empty.
enqueue(item): It adds a new item to the rear of the queue.
dequeue( ): It removes the front item from the queue.
isEmpty( ): It tests to see whether the queue is empty.
size( ): It returns the number of items in the queue.
A B C D Front = 1
Rear = 4
0 1 2 3 4 5
REAR = REAR + 1, QUEUE [REAR] = E
Front Rear
A B C D E Front = 1
Rear = 5
0 1 2 3 4 5
17 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
B C D E Front = 2
Rear = 5
0 1 2 3 4 5
Queue Insertion Operation (ENQUEUE):
Consider a queue Q with 5 elements.
Q[0] Q[1] Q[2] Q[3] Q[4] Initially Queue is Empty FRONT = -1, REAR= -1
If we want to add one element i.e. 8 in the queue, then FRONT and REAR Indicator are set to 0
and the element 8 would be stored at the position pointed by the REAR.
REAR = 0, FRONT = 0, Q[REAR]=Q[0]=8 Q[0] Q[1] Q[2] Q[3] Q[4]
8
If we want to add one more element i.e. 12 in the queue, then REAR Indicator would be
incremented by 1 and the element 12 would be stored at the position pointed by the REAR.
REAR = 1, FRONT = 0, Q[REAR]=Q[1]=12 Q[0] Q[1] Q[2] Q[3] Q[4]
8 12
ALGORITHM: ENQUEUE (QUEUE, REAR, FRONT, ITEM) QUEUE is the array with N
elements. FRONT is the pointer that contains the location of the element to be deleted and REAR
contains the location of the inserted element. ITEM is the element to be inserted.
If we want to delete an element i.e. 8 from the queue, then the value of FRONT will be
incremented by 1. Deletions are done from this end of the queue.
REAR = 2, FRONT = 1, Q[0] Q[1] Q[2] Q[3] Q[4]
12 4
If we want to delete an element i.e. 12 from the queue, then the value of FRONT will be
incremented by 1 i.e. 2.
REAR = 2, FRONT = 2 Q[0] Q[1] Q[2] Q[3] Q[4]
4
Observe that queue has only one element. When FRONT = REAR condition is true, then the queue
Q has only one element. If we want to this element also, then the queue Q becomes empty and set
the FRONT and REAR pointers to -1.
REAR = -1, FRONT = -1, Queue is Empty. Q[0] Q[1] Q[2] Q[3] Q[4]
ALGORITHM: DEQUEUE (QUEUE, REAR, FRONT, ITEM) QUEUE is the array with N
elements. FRONT is the pointer that contains the location of the element to be deleted and REAR
contains the location of the inserted element. ITEM is the element to be deleted.
19 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Application of Queue:
Simulation
Various features of Operating system
Multi-programming platform systems.
Different types of scheduling algorithms
Round robin technique algorithms
Printer server routines
Various application softwares is also based on queue data structure.
LINKED LIST:
A linked list is a linear collection of data elements called nodes.
Each nodes is divided into two parts:
o The first part contains the information of the element.
o The second part contains the memory address of the next node in the list. Also called Link
part.
In the above figure shows an representation if Linked List with three nodes, each node contains
two parts.
The left part of the node represents Information part of the node.
The right part represents the next pointer field. That contains the address of the next node.
A pointer START or HEAD gives the location of the first node.
The link field of the last node contains NULL.
AVAIL List
The operating system of the computer maintains a special list called AVAIL list that contains only
the unused deleted nodes.
Whenever node is deleted from the linked list, its memory is added to the AVAIL list.
AVAIL list is also called as the free-storage list or free-pool.
22 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
ALGORITHM: INS_BEG (START, P) START contains the address of the first node.
Step 1: P new Node;
Step 2: data(P) num;
Step 3: link(P) START
Step 4: START P
Step 5: Return
Garbage Collection:
The operating system of the computer periodically collects all the deleted space into the free-
storage list. This technique of collecting deleted space into free-storage list is called as garbage
collection.
ALGORITHM: DEL_END (P1, P2, START) This used two pointers P1 and P2. Pointer P2 is used to
traverse the linked list and pointer P1 keeps the location of the previous node of P2.
Step 1: START
Step 2: P2 START;
Step 3: while ( link(P2) ! = NULL)
P1 P2
23 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
P2 link(P2)
While end
Step 4: PRINT data(p2)
Step 5: link(P1) NULL
Free(P2)
Step 6: STOP
Trees:
A tree is a data structure consisting of nodes organized as a hierarchy.
Terminology of a tree:
Node: A node is a fundamental part of tree. Each element of a tree is called a node of the tree.
Root: The topmost node of the tree.
Edge: It connects two nodes to show that tree is a relationship between them.
Parent: It is an immediate predecessor a node. ( A is parent of B, C, D)
Child: A successor of parent node is called the child node. (E, F is a child of B)
Siblings: Nodes that are children of the same parent are called siblings. (B,C,D) (M,N)
Leaf or terminal node: Nodes that do not have any children are called leaf node. (J, K, L)
Internal Node: A node has one or more children is called an internal node. ( B, C, D, H )
24 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Path: A path is an ordered list of nodes that are connected by edges. In figure path A to O is A, D,
I, M, O.
Height: the height or depth of a tree is defined to be the maximum number of nodes in a branch of
tree. ( The height of tree is 5)
Ancestor: A node reachable by repeated proceeding from child to parent. (Ancestor of M is I, D,
and A)
Descendant: A node reachable by repeated proceedings from parent to child. (Descendent of I are
(M,O) and N)
Subtree: A Subtree is a set of nodes and edges comprised of parent and all the descendants of that
parent. In figure T1, T2 and T3 are subtrees.
Binary tree:
A binary tree is an ordered tree in which each internal node can have maximum of two child
nodes connected to it.
A binary tree consists of:
o A node ( called the root node)
o Left and right sub trees.
A Complete binary tree is a binary tree in which each leaf is at the same
distance from the root i.e. all the nodes have maximum two subtrees.
GRAPH
25 | P a g e
Chapter 4- Data Structures II PUC, MDRPUC, Hassan
Important Questions
1 Mark Questions:
1. Definition of Data Structure, Linear Data Structures, Array, Stack, Queue, Linked List,
Non Linear Data Structures.
2. Definition on Tree, Tree Terminologies, Binary Tree, Complete Binary Tree, Graph.
3 Marks Questions:
5 Marks Questions:
26 | P a g e