Data Structure in Hindi PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 85

Coding Talks

UNIT – I
Data Structure
fdlh Hkh Program esa Data dks fofHkUu izdkj ls Arrange fd;k tk ldrk gSaA fdlh Hkh ,d
Representation dk Logical ;k Arithmetical Representation gh Data Structure dykrk gSaA
fdlh Data Representation dks Choose djuk nks concepts ij fuHkZj djrk gSaA
(1) Data bruk Arranged gksuk pkfg, fd og Actual Relation dks iznf'kZr djrk gSaA
(2) og bruk Easy gksuk pkfg, fd vko';drk iM+us ij mls vklkuh ls le>k tk ldrk gSaA
(3) Data Structure dks eq[;r% nks Parts esa Divide fd;k x;k gSaA
(i) Linear Data Structure: - ;g ,d ,slk data structure gSa ftlesa Elements
Sequence esa Store jgrs gSa rFkk gj Element ,d Unique Pre-decessor vkSj
Success gksrk gSaA Linear Data Structure ds mnkgj.k fuEu gSaA Arrays,
Linked List, Stack rFkk Queue
(ii) Non Linear Data Structure: - ,d ,slk Data Structure gSa ftlesa
Elements Sequence esa ugha jgrs gSaA vkSj blds fy, fdlh Unique Pre-
decessor ;k Successor dh vko';drk ugha gksrh gSaA Example: - Trees,
Graphs etc.
fofHkUu Data Structures fuEu gSaA
(1) Arrays: - Array Ltd. No. of Elements rFkk Same Data Type dh ,d List dks
Represent djrs gSaA Array dks Individual elements dks Index ds }kjk Access fd;k tk
ldrk gSaA Array 3 izdkj ds gksrs gSaA
(i) One Diemensional Array: - bl izdkj ds Array esa ,d Index dks Access
djus dh vko';drk gksrh gSaA
(ii) Two Diemensional Array: - ,d Individual element dks Access djus ds
fy, nks Indexes dh vko';drk gksrh gSaA (Matrix)
(iii) Multi Diemensional Array: - blds fy, nks ;k nks ls vf/kd Indexes dks
Store fd;k tkrk gSaA

(2) Linked List: - Link List Data Elements dk ,d Linear Collection gSaA ftls Node
dgrs gSaA Linear Order dks Pointers ds }kjk Maintain fd;k tkrk gSaA gj Node dks nks
Parts esa Divide djrs gSaA ,d Link List Linear Link List ;k Doubly Link List gks
ldrh gSaA
(i) Linear Link List esa gj Node dks nks Parts esa divide fd;k tkrk gSa
• First Part esa Information dks j[kk tkrk gSaA
• Second Part dks Link Part ;k Next Pointer Field dgk tkrk gSa
ftlesa List ds next Node dk Address jgrk gSaA
List dh First Element dks ,d Pointer Point djrk gSa ftls Head dgrs gSaA Linear
Link List dks ,d Direction esa Traverse fd;k tkrk gSaA
START

INFO INFO X

START

Prev Info Next Prev Info Next Prev Info Next


Nodes Nodes
Linked List

(ii) Doubly Link List: - Doubly Link List esa gj Node dks 3 Parts esa Divide fd;k
x;k gSaA
Coding Talks

• First Part esa Information dks j[kk tkrk gSaA


• Second Part dks Previous Pointer field dgk tkrk gSa ftlesa List ds fiNys
(Pre-decessor) Element dk Address jgrk gSaA
• Third Part dks Next Pointer Field dgk tkrk gSa ftlesa List ds next
element dk address jgrk gSaA
nks Pointer Variable dks Perform fd;k tkrk gSa ftls Head vkSj Tail dgrs gSa tks Doubly Link
List ds First vkSj Last Element ds Address dks Contain djrs gSaA First Element ds Previous
Pointer esa NULL Store fd;k tkrk gSa ftlls ;g irk yxrk gSa fd blds ihNs dksbZ Node ugha gSaA
Last Element ds Next Pointer esa NULL dks Store fd;k tkrk gSaA ftls ;g irk yxrk gSa fd
blds vkxs dksbzZ Node ugha gSaA Doubly Link List dks nksuks Direction esa Traverse fd;k tk
ldrk gSaA

Stacks: - Stack dks Last In First Out (LIFO) System Hkh dgk tkrk gSaA ;g ,d Linear Link
List gSa ftlesa Insertion vkSj Deletion ,d gh End ls fd;k tkrk gSaA ftls Top dgrs gSaA Stack
dks cukus vkSj feVkus ds fy, dsoy nks gh Function gS
(1) Push: - Stack esa fdlh Hkh lwpuk dks tksM+us ds fy,
(2) Pop: - Stack ls fdlh Hkh Item dks feVkus ds fy,A
Stack ,d izdkj dh List gSa ftls nks izdkj ls iznf'kZr fd;k tk ldrk gSa (1) Array ds }kjk vkSj
(2) Link List ds }kjk

54 ←Top
13 ←Top 13
20 ←Top 20 20
Stack Empty ← Top Inserting First Inserting Second Inserting Third
Element Element Element

Queues: - Queue dks First In Frist Out (FIFO) System dgk tkrk gSaA queues ,d fo'ks"k izdkj
dh Data Structure gSa ftlesa Stack dh rjg gh Information dks Add djus vkSj Delete djus dh
izfØ;k ugha gksrh cfYd blesa ,d LFkku ls Information dks Add fd;k tkrk gSa vkSj nwljs LFkku ls
Information dks Delete fd;k tkrk gSaA ftl txg ij Information dks Queue esa tksM+k tkrk gSa
og Position Rear (Queue dk vUr) dgykrh gSaA ftl LFkku ls queue esa ls Information dks
Delete fd;k tkrk gSaA oks Front dgykrk gSaA Front vkSj Rear ls tqM+h dqN Information ugha gSaA
(i) Front vkSj Rear dsoy nks gh Condition esa cjkcj gksrs gSaA
(a) tc Queue [kkyh gks ;kuh Front = 0 vkSj Rear = -1
(b) tc Queue esa dsoy ,d gh element gks vr% front = 0 vkSj Rear = 0
(ii) Queue [kkyh gksxh tc Front rFkk Rear nksuksa gh –1 dks iznf'kZr djsaA
(iii) Queue iwjh Hkjh gqbZ gksxh tc
(a) Queue dk Front 0 rFkk Rear Max –1 dks iznf'kZr djsxk
(b) tc Queue dk Front = Rear + 1
(iv) Queue dks Hkh nks izdkj ls cuk;k tk ldrk gSaA
(a) Array ds }kjkA
(b) Linked List ds }kjkA
12 20 44 55 50
↑ ↑
front rear
Queue with 5 Elements
Coding Talks

Trees: - Tree ,d Important Concept gS Tree esa Stored Information dks Node dgrs gSaA
Tree ds lHkh Nodes Edges ds }kjk tqM+s gksrs gSaA Tree ,d ,slk Data Structure gSa ftls fofHkUu
Hierarchical Relations gSa ds }kjk iznf'kZr fd;k tkrk gSaA Tree dk fuekZ.k mlds Root ls fd;k
tkrk gSaA tree ds izR;sd Node ds mij (Root dks NksM+dj) ,d Node gksrh gSaA ftls Parent Node
dgk tkrk gSaA Node ds lh/ks uhps okyh Node dks child node dgk tkrk gSaA ,d tree vertices vkSj
Edges dk Collection gSa tks fuf'pr vko';drkvksa dks Satisfy djrk gSaA
E

H I

K L M

Vertex: - Vertex ,d Object ;k Node gS tks uke ;k vU; Relative Information dks Store
djrk gSaA
Edge: - tc nks Vertices ds chp Connection Establish fd;k tkrk gSa rks mls H dgrs gSaA

Graph: - dHkh & dHkh Data Elements ds e/; relationship dks iznf'kZr fd;k tkrk gSa tks izd`fr
ls Hierarchical gks ;g vko';d ugha vr% ,slk Data Strcutre tks ,d fo'ks"k Relationship dks
iznf'kZr djrk gSaA mls Graph Data Structure dgk tkrk gSaA Graph ds varxZr set of notes rFkk
set of edges dks j[kk tkrk gSaA Graph dks 2 Hkkxksa esa ckaVk x;k gSaA
(1) Direct Graph (2) In Direct Graph

DATA STRUCTURE OPERATION


fdlh Hkh Data Structure ij different operation dks process fd;k tk ldrk gSaA ,d special
data structure tks nh gqbZ condition ds fy, pquk tkrk gSaA og operation perform djus dh
la[;k ij fuHkZj djrk gSaA Normally fdlh Hkh data structure dks 5 operations dks perform fd;k
tkrk gSaA
(1) Traversing : - tc ,d ckj data structure rS;kj gks tkrk gSa rks dHkh&dHkh izR;sd
element dks Access djus dh vko';drk gksrh gSaA bl izdkj ds Concept dks Traversing
dgk tkrk gSaA
(2) Searching: - tc dHkh nh xbZ key value ds lkFk Record dh ;k Node Value dks Locate
djuk gks rks blds fy, Hkh Link list ij Searching Operation Perform fd;k tkrk gSaA
(3) Insertion: - tc fdlh Hkh u;s Node dks List esa Add djuk gks rks mls List esa Item
Insert djuk dgk tkrk gSaA
(4) Deletion: - tc Structure esa ls fdlh Node dks delete djuk gks rks bls List Deletion
Operation dgk tkrk gSaA
(5) Updation: - tc fdlh Structure esa fdlh Existing Record dh Value dks Change
djuk gks rks mlds fy, List esa Updation Operation dks Perform fd;k tkrk gSaA
dHkh&dHkh ,d ls vf/kd Operation dks mi;ksx esa ysus dh vko';drk gksrh gSaA
(1) Sorting: - bl rduhd ds }kjk Records dks Arrange fd;k tkrk gSaA ;s Records
Logically Arrange gksrs gSaA Sorting Ascending (vkjksgh) ;k descending (vojksgh)
Order esa gksrh gSaA
(2) Merging: - tc vyx&vyx Records dks 2 Qkbyksa ls fdlh ,d file esa ykuk gks rks blds
fy, List Merging Operation dks Perform fd;k tkrk gSaA

ADT (Abstract Data Type) or (Abstract Data Structure)


fdlh Hkh Data Type dh Logical Properties dks Specify djus ds fy, Abstract Data Type dks
mi;ksx esa fy;k tkrk gSaA Data Type cgqr lh Values rFkk Operations dk Collection gSaA ftlls
Hardware rFkk Software Data Structure }kjk Implement fd;k tkrk gSaA Abstract Data
Coding Talks

Type fdlh Hkh Data Type ds Mathematical Concept dks define djrs gSaA tc Abstract Data
Type ds Mathematical Concept dks Define fd;k tkrk gSa rks Time o space ij /;ku ugha fn;k
tkrk gSaA ADT dks Specify djus ds fy, ds cgqr ls Methods gSaA ftlds }kjk budk
Implementation fd;k tkrk gSaA eq[;r% ADT dks 2 Parts esa ckaVk tkrk gSaA
(1) Value Definition
(2) Operator Definition
Value Definition collection of Values dks ADT ds fy, Define djrh gSaA ftlds 2 Parts gksrs
gSaA
(1) Definition
(2) Condition
fdlh Hkh Operator dks Define djus ds fy, 3 Parts dh vko';drk gksrh gSaA
(1) Header
(2) Optional Pre-condition
(3) Post Conditions
Abstract Typedef ds }kjk Value Definition dks Define fd;k tkrk gSa rFkk Condition
Keyword ds }kjk fdlh Hkh Condition dks Specify djrs gq, ges'kk Value Definition ds ckn
Operator Definition dks Define fd;k tkrk gSaA

ADT For Strings


ADT Specification dks Strings ij Hkh Perform fd;k tkrk gSaA 4 Basic Operation dks Strings
Support djrs gSaA
(1) Length: - ;g Function Current Strings dh Length dks Return djrk gSaA
(2) Concat: - ;g Function 2 Inputted Strings dks tksM+ dj ,d Single String Return
djrk gSaA
(3) Substr: - ;g Function fdlh Hkh String dk Substring Return djrk gSaA
(4) Pos: - ;g Function fdlh ,d String dh First Position dks Return djrk gSaA

Arrays dks Hkh Abstract Data Type dh rjg Represent fd;k tkrk gSaA Arrays dks Normally 3
Parts esa Define fd;k tk ldrk gSaA
(1) One dimensional Array (1-D Array)
(2) Two dimensional Array (2-D Array)
(3) Multi dimensional Array

Analysis Of Algorithm
dksbZ Hkh Program Data Structure rFkk Algorithm nksuks dk Combination gksrk gSaA Algorithm
fdlh Hkh specific Problem dks Solve djus ds fy, fofHkUu Steps dk combination gksrk gSaA
Algorithm dks fdlh Hkh Problem dks Read djus o mldh Solution dks define djus ds fy, Use
esa fy;k tkrk gSaA ;g Operations ftUgsa Perform fd;k tk;sxk lkFk gh Example ds lkFk ml
Problem ds Solution dks contain djrk gSaA Algorithm ds }kjk Time vkSj Space Complexity
dks Check fd;k tkrk gSaA Algorithm ds Complexity function gksrh gSa tks Run time ;k Input
size dks consider djrh gSaA

Stack
Stack ,d Non primitive data Structure gSa ;g ,d Sequence List esa Data dks Add djrk gSa
vkSj ogha ls Data Item dks gVk;k tkrk gSaA Stack esa Single Entry vkSj Single Exit Point gksrk
gSaA Stack Lifo concepts (Last in First out) ij dk;Z djrh gSaA
Coding Talks

tc fdlh Data Item dks Stack esa Insert djuk gks rks blds fy, ftl Operation dks Perform
fd;k tkrk gSa mls Push Operation dgrs gSaA Item dks Remove djus ds fy, Pop Operation dks
Perform fd;k tkrk gSaA blds vUrxZr ,d Pointer Stack ds Last esa Mkys x;s Item dks Point
djrk gSaA ftlls Top dgk tkrk gSaA tc Stack esa dksbZ u;k Item Add fd;k tkrk gSa rks mldk
Top c<+rk gSa vkSj tc fdlh Item dks remove fd;k tkrk gSa rks Top ?kVrk gSaA Insert Element In
The Stack

54 ←Top
13 ←Top 13
20 ←Top 20 20
Stack Empty ← Top Inserting First Inserting Second Inserting Third
Element Element Element
Remove Element From The Stack

54 ←Top
13 13 ←Top
20 20 20 ←Top
Stack Initially Element 54 Element 13 Element 20 ← Top
Deleted Deleted Deleted
Stack Operation
Stack ij eq[;r% 2 izdkj ds Operations dks Perform fd;k tkrk gSaA
(1) Push Operation: -Top ij dksb Hkh u;k Element Mkyuk ;k tksM+uk Push Operation
dgykrk gSaA tc Hkh Push Operation gksrk gSa] Top esa ,d la[;k Mkyh tkrh gSa vkSj Top
,d Level c<+rk gSaA ,slk djrs djrs ,d Level ij vkdj og iwjk Hkj tkrk gSa rks og fLFkfr
STACK-FULL fLFkfr dgykrh gSaA
(2) Pop Operation: - Top ij ls Element dks Delete djus dh fØ;k dks Pop Operation
dgrs gSaA gj Pop Operation ds ckn Stack ,d Level de gks tkrk gSaA lkjs Element
[kRe gksus ds ckn Stack Under Flow fLFkfr esa igq¡p tkrk gSaA

Stack Implementation With Array


lcls igys Stack Array dks fuEu izdkj Define fd;k tk;sxkA
(1) Define the Maximum Size of the Stack Variable
(1) ,d Int Stack Variable dks Array define dj Maximum Size Declare djukA
#define MAXSIZE 10
int stack [MAXSIZE];

(2) ,d int Top dks –1 ls Initialized fd;k tk;sxkA


(2) Define the Integer Variable Top and Initialized it with -1

Algorithm for Push Operation


Let Stack[Max size] is an array for implementing stack.
1. [Enter for stack overflow ?]
if Top = MAXSIZE –1, then : print : overflow and exit.
Coding Talks

2. Set Top = Top+1[Increase Top by 1]


3. Set STACK[TOP] : = ITEM [Insert new element]
4. Exit.

Function for Push Operation


void push( )
{ int item;
if (top= =MAXSIZE-1)
{ printf("Stack is full");
getch( );
exit(0);
}
else
{
prinf("Enter the element to be inserted");
scanf("%d", & item);
top=top+1;
stack[top] = item;
}
}

Algorithm for Deleting an Element from the Stack


1. [Check for the Stack under flow]
if Top<0, then
printf("Stack under flow") and exit
else [Remove the Top element]
Set item=Stack [Top]
2. Decrement the stack top
set Top = Top-1
3. Return the deleted item from the stack.
4. Exit.

Function for Pop Operation


int pop( )
{
if(top = = -1)
{ printf ("The stack is Empty")
getch( );
exit(0);
}
else
{ item = stack[top];
top = top-1;
}
return(item);
}

Stack Implementation with Link List


Algorith For Push Function
Step1- struct node n = (struct node) malloc (size of (struct node))
Coding Talks

(malloc Function ds }kjk ,d struct node izdkj ds Pointer n dks Memory


fnyk,aA)
Step2- set n → info = data (vFkkZr~ n dh info esa data dks lqjf{kr djsaA)
Step3- n → next = top (n ds next {ks= esa top dk address (irk) HkstsaA)
Step4- set top = n (top dks n ij HkstsaA)
Step5- Exit

Function to Insert an Item in the Stack


void push (stack **top, int value)
{
stack *ptr;
ptr = (stack*) malloc (sizeof(stack));
if (ptr = = NULL)
{
printf("\nUnable to allocate to memory for new node...");
printf("\nPress any key to exit ...");
getch( );
return;
}
ptr->info = value;
ptr->next = *top;
*top = ptr;
}
Algorithm for Pop
Step1- set int data (,d data uked Variable dks intialize djk,aA)
Step2- set struct *t = top (Stack ds VkWi ij struct node izdkj ds Varialbe t dks
HkstsaA)
Step3- check whether top = NULL (tkapas fd D;k top, NULL ij gSA)
if yes then (;fn gk¡ rks
Process step 4 to 5 4 ls 5 rd ds step dks djsa
else vU;Fkk
process step 6 to 9 6 ls 9 rd ds step dks djsaA)
Step4- print ("underflow") (underflow dks fizUV djk,aA)
Step5- Exit (0) (Main() Function ls ckgj vk tk,aA)
Step6- set data = t → info (t ds info {ks= esa lqjf{kr lwpuk dks MkVk esa lqjf{kr djk,aA)
Step7- set top = top → next (top dks mlds vxyh Node ij HkstsaA)
Step8- free (t) (t esa lqjf{kr Memory dks eqDr djk,aA
Step9- return(data) (data esa lqjf{kr lwpuk dks main( ) Function esa
HkstsaA
Step10- Exit

Function to Delete an Item From the Stack


int pop (stack **top)
{
int temp;
stack *ptr;
temp = (*top)->info;
ptr = *top;
*top = (*top)->next;
Coding Talks

free(ptr);
return temp;
}
MULTIPLE STACKS
;fn Stack dks Array ds }kjk Implement fd;k tkrk gSa rks Stack esa Sufficient Space dks
Allocate fd;k tkuk pkfg, ;fn Array ds }kjk allocate Space de gksxh rks Overflow
Condition Occur gks tk;sxh bls nwj djus ds fy, Array esa vkSj Memory dks allocate djuk
gksxkA ;fn Number of Overflow dks de djuk gSa rks blds fy, Large Space Of Array dks
Allocate djuk gksxk ftlls Memory ds waste gksus dh lEHkkouk Hkh c<+ tkrh gSaA
bl izdkj dh Condition ds fy, ,d ls T;knk Stack dks ,d Array esa cuk;k tk ldrk gSaA

Representing Two Stack: -


,d gh Array esa 2 Stack dks Implement fd;k tk ldrk gSaA bu nksuks Stack dh Size 0 ls
N-1 rd gksxh tc nksuks Stacks dks Combine fd;k tk;sxk rks budh Size N (Array Size) ls
T;knk
ugha gksxhA
0 1 2 →
.... .←
... n-3 n-2 n-1
Stack-A Stack - B
Stack-A Left ls right dh rjQ c<+rh gS rFkk Stack-B Right ls Left dh rjQ c<+rh gSaA

Representing More Than Two Stack: -


,d gh Array esa 2 ls T;knk Stack dks Hkh Implement fd;k tk ldrk gSaA gj Stack dks Array esa
Equal Space fn;k tkrk gSa vkSj Array Indexes dks Define dj fn;k tkrk gSaA
b[1] t[1] b[2] t[2] b[3] t[3] b[4] t[4] b[5] t[5] b[6]
t[6]
→ → → → → →
Stack 1 Stack 2 Stack 3 Stack 4 Stack 5
Stack 6

APPLICATION OF STACKS
Stack dks Main Data Structure dh rjg Use esa fy;k tkrk gSaA bldh dqN Application fuEu gSaA
(1) Stack dks Function ds chp Parameters Pass djus ds fy, Use esa fy;k tkrk gSaA
(2) High level Programming Language tSls Pascal, C vkfn Stack dks Recursion ds fy,
Hkh Use esa ysrs gSaA

Stack dks cgqr lh Problems dks solve djus ds fy, Use esa fy;k tkrk gSaA eq[;r% 3 Notation esa
Stack dks iznf'kZr fd;k tkrk gSaA
(1) Prefix Notation
(2) Infix Notation
(3) Post Fix Notation

Infix Notation : - Normally fdlh Hkh Expression dks ;k Instruction dks cukus ds fy,
2Values dh vko';drk gksrh gSaA
(1) Operand
(2) Operator
vr% Operator o Operand ls feydj ,d Expression ;k Instruction curh gSaA tSls
A+B ;gka ij A vkSj B Operand gSa rFkk + ,d Operator gSaA vr% Infix Notation esa Operands
ds chp Operators gksrs gSaA
Coding Talks

Precedence of Operator: -
Highest: ↑)
Exponentiation (↑
Next highest: Multiplication (*) and division
(/)
Lowest: Addition (+) and subtraction ( -
)

Prefix Notation: - Prefix Notation og Notation gSa tgka ij Operator Operand ls igys vkrk
gSaA tSls +AB

Post Fix Notation: - ;g og Notation gSa tgka Operator Operand ds ckn vkrs gSa tSls AB+
Post Fix Notation dks Suffix Notation ;k Reverse Police Notation gh dgk tkrk gSaA
Conversion of Notation: - ;fn fdlh Expression dks Infix Notation esa fy[kk x;k gS vkSj bUgsa
Solve djuk gSa rks blds fy, Bodmass ds Rule dks Use esa fy;k tkrk gSaA tSls
A+B*C ;g A=4, B=3 vkSj C = 7
4+3 x 7 = 7 x 7 = 49
mijksDr Expression dk Solution xyr gSa D;ksafd Bodmass ds fglkc ls igys * gksuk pkfg, ckn
esa Edition gksuk pkfg,

Conversion Into Infix To Postfix


A+B * C (Infix Notation)
A+ (B * C)
A + (BC *) → (B * C dks Postfix Notation esa cnyk x;k)
vc BC* ,d Operand gSa vkSj A ,d Operand gSaA vr% vc bls Postfix Notation esa Convert
fd;k tk;sxkA
ABC*+ → ;g A+B*C dk Postfix Notation gSaA

(1) Example of Postfix Notation: - A + [(B+C)+(D+E)*F]/G ds fy;s Postfix Form iznku


dhft;sA
Solution: -
lcls igys Bracket dks solve fd;k tk;sxk
A+[(BC+)+(DE+)*F]/G
Bracket ds vUnj Multiplication dh Precedence High gSa vr% igys Multiplication dks Solve
fd;k tk;sxkA
A+[(Bc+)+(DE+F*]/G
A+[BC+DE+F*+]/G
Plus o Division ds chp Division dh Precedence High gSa vr%
A+BC+DE+F*+G/
ABC+DE+F*+G/+

(2) A+B-C ds fy;s Postfix Form iznku dhft;sA


Solution: -
(A+B)-Cx
(AB+)-C
AB+C-

(3) A*B+C/D
;g Multiplication dh Precedence High gSaA vr% lcls igys Multiplication dks Solve fd;k
tk;sxkA
Coding Talks

Solution: -
(A*B)+C/D
(AB*) + C/D
Divide dh Precedence High gSaA
(AB*) + (C/D)
(AB*) + (CD/)
AB*CD/+ (Post Fix Notation)

(4) (A+B)/(C-D)
oSls rks Division dh Precedence High gSa ijUrq mlls Hkh High Bracket dh Precedence gksrh gSa
vr% lcls igys Bracket dks Solve fd;k tk;sxkA
(AB+)/(CD-)
AB+CD-/

(5) (A+B)*C/D+E^F/G
;gka lcls High Precedence (^) Operator dh gSaA vr% lcls igys Bracket dks vkSj mlds ckn
(^) dks solve fd;k tk;sxk
(AB+)*C/D+E^F/G
(AB+)*C/D+EF^/G
AB+C*/D+EF^/G
AB+C*D/+EF^G/
AB+C*D/EF^G/+

Conversion Into Infix To Prefix Notation


Prefix Expression esa igys Operators vkrs gSa rFkk ckn esa Operands dks fy[kk tkrk gSaA Prefix
dks fuEu izdkj Solve djrs gSaA

1. A*B+C
Multiplication dh Precedence High gSa vr% igys Multiplication dks Solve fd;k
tk;sxkA
(A*B)+C
(*AB)+C
+*ABC

2. A/B^C+D
^Operator dh Precedence High gSa vr% lcls igys ^ dks Solve fd;k tk;sxkA
A/(B^C) + D
A/(^BC) + D
/A^BC + D
+/A^BCD
3. (A-B/C)*(D*E-F)
Left to Right Associativity ds According lcls igys First Bracket dks Solve fd;k
tk;sxkA blds vUnj Hkh Operator Precedence ds According Solution fd;k tk;sxkA
(A-(B/C))*((D*E)-F)
(A-/BC)*((D*E)-F)
-A/BC*(*DE-F)
-A/BC*(-*DEF)
*-A/BC-*DEF
Coding Talks

Introduction to Queue
Queue Logical First in First Out (FIFO) rjg dh List gSa bls Normally Use esa fy;k tkrk gSaA
Queue dks nks rjg ls Implement fd;k tk ldrk gSaA
(1) Static Implementation
(2) Dynamic Implementation
ftl Queue dks Array ds }kjk Implement fd;k tkrk gSaA og Static gksrh gSa vkSj ftl Queue
ds fy, Pointers dks Use esa ysrs gSaA mls Dynamic Queue dgk tkrk gSaA fdlh Hkh queue ij nks
rjg ds Operation dks Perform fd;k tkrk gSaA
(1) Queue esa Value Insert djukA
(2) fdlh Element dks Queue ls Delete djukA
fdlh Hkh Element dks tc Queue esa Add djuk gks rks mlds fy, Queue ds Last esa Add fd;k
tkrk gSaA Queue nds Last dks Rear dgrs gSa rFkk tgka ls Data Element dks Remove fd;k tkrk
gSaA og Front dgykrk gSaA

Insert Element In The Queue: -


1. Initialize front = 0 and rear = -1
2. if Rear>= MAXSIZE
write Queue Overflow and return
else;
Set Rear = rear +1
3. Queue [Rear] = item ;
4. if Front = -1 [Set the Front Pointer]
5. Return.

Function for Insert an Item in the Queue: -


void insert (int item)
{
if(rear>=maxsize)
{ printf("Queue overflow");
break;
}
else
{
Rear = Rear+1;
Queue [Rear] = item;
}
}

Delete Element From the Queue: -


1. If front <0 [Check UnderFlow Condition]
Write Queue is Empty and return
2. else ; item = Queue [front] [Remove Element from Front]
3. Find new Value of front
if (front = = Rear) [Checking for Empty Qeueu]
Set Front = 0 ; rear = -1; [Re-initialize the Pointer]
else
Front = Front+1;
Function for Delete an Item from the Queue: -
void delete( )
{
Coding Talks

if (Front<0)
{ printf("Queue is empty");
break;
}
else
{
item=Queue[Front];
Front=Front+1;
printf("item deleted=%d",item);
}
}
Queue Implementation with Link List
Algorithm for Insertion
1. Struct_Queue NEW PTR, *temp [Declare variable type struct_queue]
2. temp=start [Initialize temp]
3. NEW = new node [Allocate memory for new element]
4. NEW→ →no = value [Insert value into the data filed of
element];
5. NEWPTR→ →NEXT=NULL
6. If (Rear = = NULL) [Queue is empty, and element to be
entered is first element]
Set Front = NEW PTR
Set Rear = NEW PTR
else :
while(temp→ →next = NULL)
tem=temp→ →next
7. Temp = temp→→NEWPTR
8. End.
Algorithm for Deletion
1. if(front = = NULL)
write Queue is empty and exit
else
temp = start
value = temp→ →no
Start = Start→→next
free(temp) [End else]
return(value) [End if]
2. Exit.

Circular Queue
Linear Queue dh rjg gh Circular Queue esa Hkh Insertion, Initialization Underflow
Condition dh Testing vkfn dks Perform fd;k tkrk gSaA vU; Operations dqN Different gksrs
gSaA
Testing of Overflow Condition in Circular Queue: - fdlh Hkh u;s Element dks tc
Circular Queue esa Insert djuk gks rks mlls igys Circular Queue esa Space Allocate djuk
iM+rk gSa vkSj mlls igys Circular Queue Full gSa ;k ugha mls Check fd;k tk;sxkA ;fn Queue
Full ugha gSa rks Queue ds Rear esa Insert Operation Perform fd;k tk;sxkA
front = 0 and rear = capacity –1
front = rear + 1
Coding Talks

vxj buesa ls ;s nksuks Conditions Satisfy gksrh gSa rks Circular Queue Full gksxhA

Function For Checking The Full Condition Of The Queue: -


boolean isFull(queue *pq)
{
if(((pq->front = = 0) && (pq->rear = = CAPACITY-1))
||(pa->front = = pq->rear+1))
return true;
else
return false;
}
Circular queue esa ;fn Data Item Insert djuk gks vkSj Circular Queue Full ugha gSa rks fuEu
Operations dks Perform fd;k tk;sxkA

Insert Item in a Circular Queue


blds fy, 3 Conditions dks /;ku esa j[kk tkrk gSaA ;fn Circular Queue full ugha gSa rks dkSulh
Condition Occur gksxh og fuEu gSa % &
(1) ;fn Queue Empty gSa rks Front vkSj Rear dks –1 ;k 0 ls Set dj fn;k tkrk gSaA
(2) ;fn Queue Empty ugha gSa rks Rear dh Value Queue ds Last Element dks Pointer
djsaxs vkSj Rear Increment dj fn;k tk;sxkA
(3) ;fn Queue Full ugha gSa rks Rear Variable dks Capacity –1 ds = gksxk rFkk Rear
Variable dks 0 ls Initialize dj fn;k tk;sxkA

void enqueue (queue *pa, int value) {


/* adjust rear variable
if (pq->front = = -1)
pq->front = pq->rear = 0;
else if (pq->rear = = CAPACITY-1)
pq->rear = 0;
else
pq->rear++;
/* store element at new rear */
pq->elements [pq->rear] = value;
}

void enqueue(queue *pq, int value) {


/* adjust rear variable */
if (pq->front = = -1)
pq->front = pq->rear = 0;
else
pq->rear = (pq->rear + 1) % CAPACITY;
/* store element at new rear */
pq->elements[pq->rear] = value;

DEQUEUE
Dequeue ,d izdkj dh Queue gSa ftlesa Elements dks fdlh Hkh End ls Remove vkSj Add fd;k
tk ldrk gSa ijUrq Middle ls Insert ugha fd;k tk ldrkA Dequeue dks Double Ended Queue
dgk tkrk gSaA Dequeue nks izdkj dh gSaA
Coding Talks

(1) Input Restricted Dequeue: - blesa Insertion rks Rear ls gksrk gSa ijUrq Deletion Front
vkSj Rear nksuks ls fd;k tk ldrk gSaA
(2) Output Restricted Dequeue: - blesa Insertion Front vkSj Rear nksuks ls gksrk gSa ijUrq
Deletion flQZ Front ls fd;k tkrk gSaA

PRIORITY QUEUE
Priority Queue og Queue ftlesa gj element dh Priority dks Assign fd;k tkrk gSa vkSj fdl
Order esa Element dks Delete fd;k tk;sxk ;k Process fd;k tk;sxk blds fy, fuEu Rules dks
Follow fd;k tkrk gSaA
(1) Highest Priority Element dks lcls igys Process fd;k tk;sxkA
(2) nks ;k nks ls vf/kd Elements ftudh Priority Same gSa mUgsa Queue esa os fdl vk/kkj ij
Add fd;s x;s gSa ds }kjk Process fd;k tk;sxkA
Priority Decide djus ds fy, dqN vU; rjhds Hkh Define fd;s tk ldrs gSa
(1) cM+h Jobs ij NksVh Jobs dh Priority High gksrh gSa vr% lcls igys Small Jobs dks
Execute fd;k tk;sxkA
(2) Amount ds vk/kkj ij gh fdlh Hkh Job dh Priority dks Set fd;k tk ldrk gSaA
(3) ;fn dksbZ Job High Priority ds lkFk Queue esa Enter gksrh gSa rks lcls igys mls gh
Execute fd;k tk;sxkA Memory esa Priority queue dks 3 izdkj ls iznf'kZr fd;k tk ldrk
gSaA
(i) Linear Link List
(ii) Using Multiple Queue
(iii) Using heap

Link List Representation: - bl izdkj dh Queue dks tc Link List dh rjg iznf'kZr fd;k tkrk
gSa rks List ds izeq[k 3 Fields (gj Node ds) gksrs gSaA
(1) Information Field ftlesa Data Elements dks Store fd;k tkrk gSaA
(2) Priority Field ftlesa Element ds Priority Number dks Store fd;k tkrk gSaA
(3) Next Pointer Link ftlesa Queue ds Next Element dk Address gksrk gSaA

Multiple Queue Representation


Multiple Queue ;k Priority Queue dks Ascending Priority Queue dk Descending Priority
Queue esa Hkh divide fd;k tkrk gSaA
(1) Ascending Priority Queue : - bl izdkj dh queue esa lcls NksVs Item dh Priority
lcls vf/kd gksrh gSaA vr% lcls NksVs Item dks lcls igys Delete fd;k tk;sxkA
(3) Descending Priority Queue: - bl izdkj dh queue esa lcls cM+s Item dh Priority lcls
vf/kd gksrh gSaA vr% lcls cM+s Item dks lcls igys Delete fd;k tk;sxkA

Heap Representation of Priority Queue


Heap ,d Complete Binary Tree gSa ftldh dqN Additional Property gksrh gSaA tSls ;k rks
Root Element Small gksxk ;g Largest gksxkA ;fn Root Element lcls NksVk gks mls
Minimum Heap dgk tkrk gSaA ;fn Root Element Largest gks rks mls Maximum Heap dgk
tk;sxkA

Application of Queue
Queue dh dqN Application fuEu gSa %&
(1) Queue ds }kjk cgqr lh Problem dks vklkuh ls Solve dj fn;k tkrk gSa tSls BFS
(Breadth First Search) ;k DFS (Depth First Search)
(2) tc Jobs dks Network Printer ij Submit fd;k tkrk gSa rks os Arrival ds Order esa
Arrange gksrk gSaA
Coding Talks

(3) ;g ,d izdkj dk Computer Network gSa tgka ij Disk ,d Computer ls Attach gksrh
gSa ftls File Server dgk tkrk gSaA vU; Computers mu Files dks FCFS (First Come
First Server) ds vk/kkj ij access djrs gSaA
(4) Queue Application dks Real Life esa Hkh Use esa fy;k tkrk gSa tSls Cinema Hall,
Railway Reservation, Bus Stand vkfnA
Coding Talks

UNIT – II
Link List
;fn fdlh Programs ds Execution ds igys Memory dks Divide fd;k tkrk gS rks og Static gks
tkrh gS vkSj mls Change ugha fd;k tk ldrk ,d fo'ks"k Data Structure ftls Link List dgrs
gSaA og Flexible Storage System iznku djrk gSaA ftldks Array ds iz;ksx dh vko';drk ugha gksrh
gSaA Stack vkSj Queue dks computer memory esa iznf'kZr djus ds fy, ge Array dk iz;ksx djrs
gSaA ftlls Memory dks igys ls Declare djuk iM+rk gSA blls Memory dk Wastage gksrk gSaA
Memory ,d egRoiw.kZ Resource gSa vr% bls de ls de Use esa ysdj vf/kd ls vf/kd Programs
dks Run djuk pkfg,A
Link List ,d fo'ks"k izdkj ds Data Element dh List gksrh gSa tks ,d & nwljs ls tqM+h gksrh gSaA
Logical Ordering gj Element dks vxys Element ls Point djrs gq, Represent djrs gSaA
ftlds nks Hkkx gksrs gSaA
(1) Information Part
(2) Pointer Part

START

INFO INFO X

START

Prev Info Next Prev Info Next Prev Info Next


Nodes Nodes
Link List

Singe Link List esa ,d Node esa nks Parts gksrs gSaA
(1) Information Part
(2) Link Part
tcfd Doubly Link List esa nks Pointer Part rFkk ,d Information Part gksrk gSaA
Operation on Link List
Link List esa fuEu Basic Operation dks Perform fd;k tkrk gSaA
(1) Creation : - ;g Operation Link List cukus ds fy, Use esa ysrs gSaA tc Hkh vko';drk
gks fdlh Hkh Node dks List esa tksM+ fn;k tkrk gSaA
(2) Insertion : - ;g Operation Link List esa ,d u;h Node dks fo'ks"k fLFkfr esa fo'ks"k LFkku
ij tksM+us ds fy, dke esa fy;k tkrk gSaA ,d u;h Node dks fuEu LFkkuksa ij Insert fd;k tk
ldrk gSaA
(i) List ds izkjEHk esa
(ii) List ds vUr esa
(iii) fdlh List ds e/; esa
(iv) vxj List iwjh [kkyh gSa rc u;k Node Insert fd;k tkrk gSaA
(3) Deletion: - ;g Operation Link List ls fdlh Node dks Delete djus ds fy, iz;ksx esa
fy;k tkrk gSaA Node dks ftu rjhdksa ls Delete fd;k tkrk gSa og fuEu gSaA
(i) List ds izkjEHk esa
(ii) List ds vUr esa
(iii) vkSj List ds e/; esa
(4) Traversing : - ;g izfØ;k fdlh Link List dks ,d Point ls nwljs Point rd Values dks
Print djokus ds dke vkrh gSaA vxj igys Node ls vk[kjh Node dh vksj Traversing djrs
gSa rks ;g Forward Traversing dgykrh gSaA
Coding Talks

(5) Con Catenation: - ;g izfØ;k nwljs List ls Node dks tksM+us ds dke vkrh gSa ;fn nwljh
List ij Node gksrk gSa rks Concatenation Node esa M+N Node gksxk
(6) Display: - ;g Operation izR;sd Node dh lwpuk dks Point djus ds iz;ksx esa vkrk gSaA
vr% Information Part dks Print djuk gh List Display dgykrk gSaA

TYPES OF LINK LIST


lk/kkj.kr;k% List dks 4 Part esa ckaVk tkrk gSaA
(1) Single Link List
(2) Doubly Link List
(3) Circular Link List
(4) Circular Double Link List
(1) Single Link List : - Single Link List ,slh Link List gksrh gSa ftlesa lkjs Node ,d
nwljs ds lkFk Øe esa tqM+s gksrs gSa blfy, bldks Linear Link List Hkh dgrs gSaA bldk izkjEHk
vUr gksrk gSaA blesa ,d leL;k ;g gSa fd List ds vkxs okys Node rks Access fd;k tk
ldrk gSa ijUrq ,d ckj Pointer ds vkxs tkus ds ckn ihNs okys Node dks Access ugha fd;k
tk ldrk gSaA

START

10 20 30
Single Link List

(2) Doubly Link List: - Doubly Link List og Link List gksrh gSa ftlesa lkjs Node ,d
nwljs ds lkFk Multiple Link ds }kjk tqM+s gksrs gSaA blds vUrxZr List ds vkxs okys Node o
List ds ihNs okys Node nksuks dks Access fd;k tk ldrk gSaA blhfy, Doubly Link List esa
igys Node dk Left vxys Node ds Right dk Address j[krk gSaA
START
Prev Data Succ Prev Data Succ Prev Data Succ
10 20 30
Doubly Link List

(3) Circular Link List: - Circular Link List ,slh Link List gksrh gSa ftldh dksbZ 'kq:vkr
o vUr ugha gksrk gSaA ,d Single Link List ds igys Node ls vfUre Node dks tksM+ fn;k
tkrk gSaA vr% List ds Last Node esa List ds First Node dk Address j[kk tkrk gSaA
START
Node 1 Node 2 Node 3 Node 4
10 20 30 40

Circular Link List

(4) Circular Doubly Link List: - ;g ,d ,slh List gksrh gSa ftlesa nksuks Pointer (1)
Successor Pointer (2) Pre-Decessor Pointer Circle Shape esa gksrs gSaA
START

10 20 30
Coding Talks

Circular Doubly Link List

HEADER NODE
dHkh&dHkh ,d Extra Node dks List ds Front esa j[kuk vko';d gks tkrk gSaA bl Node esa fdlh
Item dks Represent ugha fd;k tkrk ;g Node Header Node ;k List Header dgykrk gSaA bl
Node dk Information Part Unused gksrk gSaA bl Node ds Information Part esa List dh
Global Information dks j[kk tkrk gSa tSls Header Node ds vUrxZr ds List esa fdrus Nodes gSa
dh Information dks Store fd;k tkrk gSaA (Not Including Header Node) bl izdkj ds Data
Structure esa fdlh Hkh Data Item dks add vkSj Delete djus ds fy, Header Node dks Hkh
Adjust djuk iM+rk gSaA Number of Nodes fdrus gSaA bls Directly Header Node ls Access dj
fy;k tkrk gSA blds fy, List Traversing dh vko';drk ugha iM+rh gSaA
Header Node dk ,d vU; mnkgj.k fuEu gSaA ,d Particular Machine dk cukus ds fy, fuEu
Parts dks Use esa fy;k tkrk gSaA tSls B841, K321, A087, J492 vkfnA bl Assembly dks List
}kjk iznf'kZr fd;k tkrk gSaA Empty List dks NULL Pointer ds }kjk iznf'kZr djrs gSaA

Operation of Single Link List


Single List esa Structure dks Use esa fy;k tkrk gSaA D;ksafd ,d Node nks vyx Data Types dh
Value dks Store djrk gSaA
(1) Information Part
(2) Link Part
vr% Link List cukus ds fy, lcls igys Link List ds Structure dks cuk;k tk;sxkA
struct node
{
int info;
struct node * link;
};

Characteristics of Link List


(1) bl izdkj dh Link List esa ,d Node esa nks vyx Data Type dh Values dks Store fd;k
tkrk gSaA
(i) Information Part : - ftlesa fdlh Hkh (int, char, double, long vkfn Data
Type dh Values dks Store fd;k tk ldrk gSa)
(ii) Pointer Part: - Pointer Part ftlesa Next Node dk Address gksrk gSaA vr%
Link Part ds }kjk gh Next Node dks Access fd;k tk ldrk gSaA
(2) List ds ftl Node ds Link Part esa NULL gksrk gSaA og List dk Last Node dgykrk gSaA
(3) ;fn List [kkyh gS rks Information vkSj Link Part nksuks NULL gksxsaA
(4) ;fn Information Part esa Value gS rFkk ml Node dk Link Part NULL gSa vr% List esa
,d Item gSaA
(5) ;fn Link Part esa nwljs Node dk Address gSa vr% List [kkyh ugha gSaA
(6) Link List ds vUrxZr ,d Pointer List ds First Node dks Point djrk gSaA ftlls List ds
Last Node rd igqapk tk ldrk gSaA
Coding Talks

Operations on Link List


(1) Create an Empty Link List: -
(1) ,d Node Pointer head cuk;k tk;sxk ftlesa fdlh Value dks Initialize ugha fd;k x;k
gSaA ;g Pointer List ds First Element dks Point djus ds fy, Use esa fy;k tkrk gSaA
(2) 'kq:vkr esa List Empty gksxh vr% Head Pointer dks NULL ls Initialize fd;k tk;sxkA
(3) ;g iznf'kZr djrk gSa fd List Empty gSa blds fy, C esa fuEu Function dks cuk;k tk;sxkA

Function: -
void emptylist (struct node*head)
{
head = NULL;
}

(2) Display List: - List dks 'kq:vkr ls Last rd Traverse fd;k tkrk gSaA blds fy, ,d
Head Pointer dh vko';drk gksrh gSaA lkFk gh ,d vkSj Temporary Pointer dh
vko';drk gksrh gSa tks List ds Last Node rd tkrk gSaA List dk Last Node og Node gSa
ftlds Link Part esa NULL gksrk gSa vr% nks struct Node Pointer dh vko';drk gksxh
ftlesa igyk List ds First Node dks Point djsxk rFkk nwljk ,d ds ckn ,d Next Node
ij tk;sxk vkSj Information Part dks Print djsxkA
Function: -
void printlist (struct node*head)
{
struct node*t;
t = head;
if (t == NULL)
{
printf ("\n list is empty");
return;
}
else
while (t → link != NULL)
{
printf ("%d", t → info);
t = t → link;
}
}

Insertion in Link List: - fdlh Hkh Element dks List esa Insert djus ds fy, lcls igys Free
Node dh vko';drk gksrh gSaA tc Free Node cuk fy;k tkrk gSa rks Node ds Information Field
esa Data Value dks add dj nsrs gSa vkSj bl u;s Node dks mldh txg ij Add dj fn;k tkrk gSaA
fdlh Hkh List esa Insertion 3 izdkj ls fd;k tk ldrk gSaA
(1) List ds Beginning esa
(2) List ds End esa
(3) fdlh fn;s x;s Element ds ckn

Insertion at the Beginning: - fdlh Hkh Element dks List ds First esa Add djus ds fy, lcls
igys List Empty gSa ;k ugha bls check fd;k tk;sA ;fn List Empty gSa rks u;k Node gh List dk
First Node gksxk blds fy, fuEu Steps dks Perform djsaxsA
(1) New Node cuk;sxsaA
Coding Talks

(2) New Node ds Information Part esa Data Value Enter djsaxsA
(3) New Node ds Link Part NULL Value Insert djsaxs vkSj vc
(4) List dk Pointer Head ml u;s Node dks Point djsxkA
;fn List Empty ugha gS rks u;s Node dks List ds lcls vkxs Add dj fn;k tk;sxkA blds fy,
fuEu Steps dks Follow djsaxsA
(1) New Node cuk;sxsaA
(2) New Node ds Information Part esa Data Value Enter djsaxsA
(3) New Node ds Link Part esa Head (List Pointer tks List ds First Item dks Point dj
jgk gS) dk Address MkysaxsA
(4) vc Head New Node dks Point djsaxk

Function: -
void insert (struct node ** head, int item)
{
struct node*new;
new = malloc (sizeof (struct node));
new → info = item;
if (*head = = NULL) //if the list is empty
new → link = NULL;
else
new → link = *head;
* head = new;
}

Insert at The End of the list: - ;fn fdlh Element dks List ds End esa Insert djuk gks rks lcls
igys List Empty gSa ;k ugha Condition dks Test fd;k tk;sxk ;fn List Empty gSa rks u;k Node
List dk First Node Hkh gksxk vkSj List dk Last Node Hkh gksxkA vr% fuEu Steps dks Perform
djsaxsA
(1) New Node cuk;saxsA
(2) New Node dh Information Part esa Data Value Insert djsaxsA
(3) New Node ds Link Part esa NULL MkysaxsA
(4) vc Head Pointer New Node dks Point djsaxkA

;fn List Empty ugha gSa rks List dks Traverse dj Last Element rd igqWapsaxs vkSj ckn esa Last esa
u;s Node dks Insert dj fn;k tk;sxkA blds fy, fuEu Steps dks Follow djasxsA
(1) New Node cuk;sxsaA
(2) New Node dh Information Part esa Data Value Insert djsaxsA
(3) New Node ds Link Part esa NULL MkysaxsA
(4) vc Head Pointer ds vykok ,d u;s Pointer dh vko';drk gksxh tks List ds Last Node
rd tk;sxkA
(5) List ds Last Node ds Link Part esa u;s Node dk Address Mkysaxs

Function:-
void insertatlast (struct node**head, int item)
{
struct node * new, *temp;
new = malloc (sizeof (struct node));
new → info = item;
new → link = NULL;
Coding Talks

if (*head = = NULL) //if list is empty


*head = new;
else
{
temp = *head;
while (temp → link != NULL)
temp = temp → link;
temp → link = new;
}
}

Insert After The Given Element Of The List: - ;fn u;s Node dks fdlh fn;s x;s Element ds
ckn Insert djuk gks rks lcls igys ml Location dks Findout fd;k tk;sxk tgka Node dks Add
djuk gSa vkSj blds ckn List esa u;s Node dks Add dj fn;k tk;sxkA blds fy, fuEu Steps dks
Perform djrs gSaA
(1) New Node cuk;saxsA
(2) New Node dh Information Part esa Data Value Insert djsaxsA
(3) ,d Temporary Pointer ysaxs tks List ds ml Element dks Point djsxk ftlds ckn u;k
Node Add djuk gSaA ;fn
(4) ;fn Temporary Pointer NULL rd igq¡p tkrk gSa vr% List eas Item ugha gSa ftlds ckn
u;s Node dks Add djuk gSa
(5) ;fn Temporary Pointer ml Item dks <w¡< ysrk gSa rks fuEu Steps dks Follow djsaxsA
(i) u;s Node ds Link Part esa Temporary Pointer ds Link Part dks Mky nsaxsA
(ii) Temporary Pointer ds Link Part esa New Node dks Mky nsaxs

Function: -
void insertatmiddle (struct node **head, int loc, int item)
{
struct node * new, * temp;
int i;
temp = *head;
for (i = 0; i < loc; i++)
{
temp = temp → link;
if (temp = = NULL)
{
printf ("item not found");
return;
}
}
new = malloc (sizeof (struct node));
new → info = item;
new → link = temp → link;
temp → link = new;
}
Coding Talks

Delete The Element From The List


fdlh Hkh Element dks List ls Delete djus ds fy, lcls igys Pointers dks Properly Set fd;k
tkrk gSaA ckn esa ml Memory dks Free dj fn;k tkrk gSaA ftls Node }kjk Use esa fy;k tk jgk
FkkA List dk Deletion 3 txg ls fd;k tk ldrk gSaA
(1) List ds Beginning ls
(2) List ds End ls
(3) fn;s x;s Element ds ckn okys Node dks Delete djuk

(1) Delete the Element from the beginning of the List: - tc List ds First Element dks
Delete djuk gks rks blds fy, fuEu Steps dks Perform fd;k tkrk gSaA
(i) Head dh Value dks fdlh Temporary Variable esa Assign djukA
(ii) vc head ds Next Part dks Head esa Assign dj fn;k tk;sxkA
(iii) tks Memory ptr }kjk Point dh tk jgh gS mls Deallocate dj fn;k tk;sxkA

Function: -
void deletefromfirst (struct node **head)
{
struct node*ptr;
if (*head == NULL)
return;
else
{
ptr = *head;
head = (*head) → link;
free (ptr);
}
}

Delete From End Of The List: - fdlh Hkh Element dks List ds End ls Delete djus ds fy,
lcls igys List dks Second Last Element rd Traverse fd;k tk;sxkA blds ckn Last Element
dks Delete djus ds fy, fuEu Steps dks Follow fd;k tk;sxkA
(1) ;fn head NULL gSa rks List esa dksbZ Data Item ugha gSa vr% fdlh Item dks Delete ugha
fd;k tk ldrkA
(2) ,d Temporary Pointer ysaxs ftlesa head dh value dks assign djsaxsA
(3) ;fn head dk link part NULL gSa rks List esa ,d Item gSa ftls Delete djuk gSaA
(4) bls Delete djus ds fy, head esa NULL Assign dj nsaxs vkSj Pointer ptr dks Free djs
nsaxsA
(5) ;fn List esa ,d ls T;knk Item gS vr% igys NULL rd tk;sxs vkSj ,d Pointer Last
Node ds igys okys Node dks Point djsxkA
(6) IInd Last Node ds Link Part esa NULL Value Assign dj nsaxsA
(7) og Pointer tks Last Node dks Point dj jgk Fkk mls Free dj nsaxsA

Function: -
void deletefromlast (struct node **head)
{
struct node *ptr, *loc;
if (*head = = NULL)
return;
else if ((*head) → link = = NULL)
{
Coding Talks

ptr = *head;
*head = NULL;
free (ptr);
}
else
{
loc = *head;
ptr = (*head) → link;
while (ptr → link != NULL)
{
loc = ptr;
ptr = ptr → link;
}
loc → link = NULL;
free(ptr);
}
}

Delete After A Given Element Of The List: - tc fdlh fn;s x;s Element ds ckn okys
Element dks Delete djuk gks rks lcls igys Location dks Find out fd;k tk;sxk ftlds ckn okys
Element dks Delete djuk gSa blds fy, fuEu Steps dks Follow djrs gSaA
(1) blds fy, 2 Struct Node Pointers ysxsaA
(2) Head Pointer List ds first element dks Point djrk gSa ,d vkSj Pointer ogha Point
djsxk tgka Head Point dj jgk gSA
(3) ;fn Head NULL gS vr% List Empty gSA
(4) ;fn List Empty ugha gS rks ,d vU; Pointer ysxsa tks specific location ftls Delete djuk
gS ds ihNs jgsxkA
(5) vc ihNs okys Pointer ds Link Part esa vkxs okys Pionter dk Link Part Mky nsaxs vkSj
List Pointer dks NULL rd c<+krs jgsaxsA

Function: -
delete (struct node*head, int loc)
{
struct node *ptr, *temp;
ptr = head;
if (head = = NULL)
{
printf ("list is empty");
return;
}
else
temp = ptr
while (ptr != NULL)
{
if (ptr → info = = loc)
{
temp → link = ptr → link;
free (ptr);
return;
Coding Talks

}
temp = ptr;
ptr = ptr → link;

}
}
}

Doubly Link List


Doubly Link List dks Two way List Hkh dgk tkrk gS ftlesa gj Node dks 3 Parts esa Divide
fd;k tkrk gSA
(1) List dk igyk Part Previous Pointer Field dgykrk gS tks List ds fiNys Element dk
address j[krk gSA
(2) List dk nwljk Part Information dks Contain djrk gSA
(3) List dk IIIrd Part Next Pointer dgykrk gS tks List ds vxys element dks Point djrk
gSA
nks Pointer Variable dks Use esa fy;k tkrk gS tks List ds First Element rFkk List ds Last
Element ds Address dks Contain djrs gSaA List ds First Element ds Previous Part esa NULL
jgrk gS tks ;g crkrk gS fd bl Node ds ihNs dksbZ vU; Node ugha gSA List ds Last Element ds
Next Part esa NULL jgrk gS tks ;g crkrk gS fd blds vkxs dksbZ Node ugha gSA doubly link list
dks nksuks Directions esa Travers fd;k tk ldrk gSA

Representation of Doubly Link List


ekuk dh user Link List esa Integer Values dks Store djuk pkgrk gSA blds fy, Doubly Link
List Structure dk memory esa gksuk vko';d gSA vr% Structure define djsaxsA

Node Structure: -
struct node
{
int info;
struct node * prev, *next;
};

Insert A Node At The Begning


Doubly Link List ds 'kq: esa fdlh Node dks tksM+us ds fy, ;g check djrs gSa fd List [kkyh rks
ugha gSA ;g ns[kus ds fy, head Pointer dks check djrs gSaA ;fn Head NULL gS rks u;s Node
dks vc Head Point djus yxsxkA ugha rks Head ds Previous Pointer esa u;s Node dk Address
Mky fn;k tkrk gSA vr% fdlh Node dks tksM+us ds fy, fuEu Algorithm fy[ksaxsA

Step 1:- u;s Node dks Memory Allocate djsaxsA


struct node * new = malloc (sizeof (struct node))
Step 2: - ,d Temporary Variable esa Head dk Address MkysaxsA
struct node * temp = head
Step 3: - New Node ds Information Part esa Data Item Insert djsaxsA
new → info = item
Step 4: - New Node ds Previous Part dks NULL djsaxsA
new → prev = NULL
Step 5: - New Node ds Next Part esa head dk address MkysaxsA
new → next = head
Coding Talks

Step 6: - ;g check djsaxs fd head NULL gS ;k ugha ;fn head NULL gS rks Step 7 dks
Execute djsaxs vU;Fkk Execute djsaxsA
Step 7: - head esa u;s Node dk Mky nsaxs vr% vc head u;s Node dks Poin djsxkA
Step 8: - head ds prev part dks New node dk Address MkysaxsA
head → prev = new
head = new (vc head u;s node dks Point djus yxsxk)
Step 9: - Exit

Function: -
Insertatfirst (sturct node ** head, int item)
{
struct node*new = malloc (sizeof (struct node));
new → info = item;
new → prev = NULL;
new → next = head;
if (head = = NULL)
head = new;
else
{
head → prev = new;
head = new;
}
}

Insert The Item At The End Of The Doubly Link List


Link List ds Last esa fdlh u;s Node dks tksM+us ds fy, igys ;g check djsaxs fd List [kkyh rks
ugha gS ;fn List [kyh gS rks igyk Node gh First Node gksxk rFk ogh Last Node gksxkA ;fn List
esa vkSj Hkh vkbVel gS rks u;s Node dh Information Part esa Data Item rFkk u;s Node ds Next
Part NULL Mkysaxs rFkk mls List ds Last esa Add dj nsaxsA
At a last
Step 1: - u;k Node cu;saxs
stuct node * new = malloc (sizeof (struct node));
Step 2: - ,d Temporary * ysaxs ftlesa head dk address Mky nsaxs
struct node * temp = head
Step 3: - u;s Node ds Information Part esa Data Item Insert djsaxsA
new → info = item
Step 4: - check that head = NULL
;fn head NULL gS rks step 5 dks Run djsaxs ;k vU;Fkk Step6 dks Run djsaxsA
Step 5: - Insert at first function dks call djsaxs
vkSj return gks tk;sxsA
Step 6: - (1) u;s Node ds Information Part esa Data Item Insert djsaxsA
new → info = item
(2) u;s Node ds Next Part esa NULL Insert djsaxs
new → next = NULL
(3) tc rd T dk Next part NULL ugha gks tkrk gSA T dks vkxs c<+k,saxsA
while (temp → next != NULL)
temp = temp → next
(4) temp ds Next Part esa u;s Node dk Address MkysaxsA
temp → next = new
Coding Talks

(5) u;s Node ds Prev Part esa temp dk address MkysaxsA


new → prev = temp
Step 7: - Exit

Function: -
addatlast (struct node **head, int item)
{
struct node * temp = *head;
struct node * new = malloc (sizeof (struct node));
if (head = = NULL)
{
insertatfirst (& head, item);
return;
}
new → info = item;
new → next = NULL;
while (temp → next != NULL)
temp = temp → next;
temp → next = new;
new → prev = temp;
}

Traversing the Doubly link List


(1) In order Traversal: - blds fy, fuEu Steps dks Follow fd;k tkrk gSA
Step 1: - tc rd head NULL ugha gks tkrk rc rd List Pointer dks vkxs c<+k;saxsA
while (head != NULL)
Step 2: - List ds gj Information Part dks print djsaxs o head dks vkxs c<+krs tk;saxsA
print head → Info
head = head → next
(2) Reverse Order Traversal: - blds fy, fuEu Steps dks Follow fd;k tkrk gSA
Step 1: - tc rd tail NULL ugha gks tkrk rc rd List Pointer dks vkxs c<+k;saxsA
while (tail != NULL)
Step 2: - List ds gj Information Part dks print djsaxs o tail dks vkxs c<+krs tk;saxsA
print tail → Info
tail = tail → next

Deletion From Doubly Link List: -


(1) Delete from Begning of List: - ;fn doubly link list [kkyh gS rks return gks tk;saxs
vU;Fkk ftl Node dks head pointer point dj jgk gS mls Delete djsaxsA blds fy, fuEu
Steps dks Follow fd;k tk;sxkA ;gka Head Pointer Struct Node Pointer gS tks List ds
first node dks Point dj jgk gSA

Step 1:- ,d Temporary Pointer ysaxs ftlesa Head dk address Mkysaxs


struct node *temp = head
Step 2: - Check djsaxs fd head NULL gS ;k ugha ;fn head NULL gS rks return gks
tk;sxsaA
if (head = = NULL)
return
Step 3: - ;fn head NULL ugha gS rks head esa head ds next part dks assign dj nsaxsA
Coding Talks

head = head → next


free (temp)
Step 4: - head ftl Node dks point dj jgk gS mlds prev part esa NULL Assign djsaxsA
head → prev = NULL
Step 5:- Exit

Function: -
deletefromfirst (struct node *head)
{
struct node *temp = head;
if (head = = NULL)
return;
head = head → next;
free (temp);
head → prev = NULL;
}

Delete From Last: -


List ds Last Node dks Delete djus ds fy, lcls igys 3 Conditions dks Check djsaxsA
(1) ;fn List [kkyh gS rks return gks tk;sxsa
(2) ;fn List esa dsoy ,d gh Node gS rks mls delete dj nsaxs vkSj head esa NULL Insert dj
nsaxsA
(3) ;fn List esa ,d ls T;knk Items gS rks igys Last Node rd igqapsxs ckn esa Last Node dks
Delete dj nsxsaA blds fy, fuEu Algorithm gSaA

Step 1: - ,d Temporary Pointer ysxsa ftlesa hand dk address Mkysaxs


struct node * temp = head
Step 2: - ;g check djsaxsa fd head NULL gS ;k ugh ;fn head NULL gS rks return gks
tk;saxsA
Step 3: - ;g check djsaxsa fd head dk Next NULL gS ;k ughA ;fn head dk Next NULL
gS rks 4 ls 6 rd ds steps dks repeat djsaxsA
Step 4: - head esa NULL insert djsaxsaA
head=NULL
Step 5: - Temporary Pointer dks Free dj nsaxs
free(t)
Step 6: - return
Step 7: - tc rd temp ds Next part esa NULL ugha vk tkrk rc rd temp dks vkxs
c<+k,xsaA
while(temp->next==NULL)
temp=temp->next
Step 8: - temp ds prev ds next part esa NULL insert djsaxsA
temp->prev->next=NULL
Step 9: - Temp dks Free dj nsxsaA
free(temp)
Step 10: - exit

Function: -
deletefromlast (struct node *head)
{
struct node * temp = head;
Coding Talks

if (head = = NULL)
return;
if (head → next = = NULL)
{
head = NULL;
free (temp);
return;
}
while (temp → next != NULL)
temp = temp → next;
temp → prev → next = NULL;
free (temp);
}

Circular Link List: - Simple Link List esa Last Node dk Link Part ges'kk NULL gksrk gS
ysfdu Circular Link List esa List ds Last Node ds Link Part esa ges'kk head dk address jgrk
gSA vr% Circular Link List dk vk[kjh Node ges'kk List ds First Node dks Point djrk gSA

Insert the New Node In the Circular Link List


Insert at First:-
tc Circular Link List ds izkjEHk esa fdlh u;s Node dks tksM+uk gks rks blds fy;s lcls igys ;g
Check djsaxs fd List [kkyh rks ugha gSA ;fn head NULL gS rks u;s Node dks Insert dj fn;k
tk;sxk vkSj Head Pointer mls Point djsxk ;fn Head NULL ugha gS rks head ij igqap dj u;s
Node dks add dj fn;k tk;sxkA mlds fy;s fuEu Algorithm gSaA
Step1: - New Node cuk;saxs
struct node*new = malloc (sizeof (struct node))
Step2: - ,d Temporary Pointer cuk,xsaA ftlesa head dk address MkysaxsA
struct node * temp = *head
Step 3: - New Node ds Information Part esa Item Insert djsaxs
new → info = item
Step 4: - Check djsaxs fd head NULL gS ;k ugha ;fn head NULL gS rks 5th step dks
execute djsaxs ugha rks 6th Step execute gksxkA
Step 5:- Head esa u;k Node insert djsaxs
head = new node
new node → next = head
Step 6: - new node ds next part esa head dk address Mkysaxs
new node → next = head
temp ds next part esa tc rd head dk address ugha vk tkrk rc rd temp dks
vkxs c<+k,xsaA
while (temp → next != head)
temp = temp → next
head esa u;s node dk address Mkysaxs
head = new node
temp ds next part esa head dk address Mkysaxs
temp → next = head
Step 7: - Exit
Coding Talks

Function: -
struct node
{
int data;
struct node *next;
};
void append(int x,struct node **p)
{
struct node *temp=*p,*n=malloc(sizeof(struct node));
n->data=x;
printf("\nappending:%d",x);
if(temp==NULL)/*list empty*/
{
*p=n;
n->next=n;
return;
}

while(temp->next !=*p)
temp=temp->next;
temp->next=n;
n->next=*p;
}

Insert at Last: -
blds fy, lcls igys ;g check djsaxs dh List [kkyh gS ;k ugha ;fn List esa ,d Hkh Item ugha gS rks
appedn function dks call djsaxs ugha rks while loop dh lgk;rk ls ml Node rd igqapsxs tgka
temp ds next esa Head gksxk vFkkZr~ Last Node rd igqpsaxs vc bl Node ds Info Part esa data
item insert djsaxs vkSj Node ds Next Part esa head dk address Mky nsaxsA blds fy, fuEu
Algorithm gSaA
Step 1: - u;s Node dks memory allocate djsaxs
struct node*new = malloc (sizeof (struct node))
Step 2: - ,d Temporary Pointer cuk,xsaA ftlesa head dk address MkysaxsA
struct node * temp = *head
Step 3: - New Node ds Information Part esa Item Insert djsaxs
new → info = item
Step 4: - Check djsaxs fd head NULL gS ;k ugha ;fn head NULL gS rks 5th step dks
execute djsaxs ugha rks 6th Step execute gksxkA
Step 5:- appedn function dks call djsaxs
vkSj return gks tk,saxs
Step 6: - new node ds next part esa head dk address Mkysaxs
new node → next = head
temp ds next part esa tc rd head dk address ugha vk tkrk rc rd temp dks
vkxs c<+k,xsaA
while (temp → next != head)
temp = temp → next
temp ds next part esa head dk address Mkysaxs
temp → next = new
Coding Talks

Step 7: - Exit

Circular List dh Traversing


Step 1: - ,d Temporary Pointer cuk,xsaA ftlesa head dk address MkysaxsA
struct node * temp = head
Step 2: - if head = NULL
return
Step 3: - Repeat Step 4 to 5 while temp → next != head
Step 4: - print temp → info
Step 5:- temp = temp → next
(end of while loop)
Step 6: - print temp → info
Step 7: - Exit

Delete an Item from the Circular Link List


Delete from first: - Circular Link List ds izkjEHk ls fdlh Hkh Node dks delete djus ds fy, 3
conditions dks check djsaxs
(1) List [kkyh gSA
(2) List esa dsoy ,d Node gSa rks ml Node dks feVkdj head esa NULL Mky nsaxs
(3) List esa ,d ls T;knk Node gS rks Loop dh lgk;rk ls first Node ij igqapsxsa vkSj ml
Node dks feVkdj head dks vxys node ij ys tk;saxsA blds fy, fuEu Algorithm gSa%&

Step 1: - ,d Temporary Pointer ysxsa ftlesa head dk address Mkysaxs


struct node * temp = head
Step 2: - ;g check djsaxsa fd head NULL gS ;k ugh ;fn head NULL gS rks return gks
tk;saxsA
Step 3: - ;g check djsaxsa fd head dk Next head gS ;k ughA ;fn head dk Next head gS
rks 4 ls 6 rd ds steps dks repeat djsaxsA
Step 4: - Temp dks free dj nsaxsA
head=NULL
Step 5: - head esa NULL MkysaxsA
head = NULL
Step 6: - return
Step 7: - tc rd temp ds Next part esa head dk adres ugha vk tkrk rc rd temp dks
vkxs c<+k,xsaA
while(temp->next != head)
Step 8: - temp=temp->next
Step 9: - head = head → next (head esa head dk next Insert djsaxsA)
Step 10: - temp ds next esa head MkysaxsA
temp → next = head
Step 11: - free(temp)
Step 12: - Exit

Delete from Last: -


Circular List ds vUr esa ls fdlh Node dks gVkus ds fy, 3 conditions dks check djsaxs
(1) ;fn List [kkyh gS
(2) ;fn List esa dsoy ,d Node gS rks head esa NULL Mky nsaxs
Coding Talks

(3) ;fn List esa ,d ls T;knk Node gS rks Loop dh lgk;rk ls List ds vfUre Node ij igqapsxs
ml Node dks feVkdj ml Node ds fiNys Node esa head dk address MkysaxsA blds fy,
fuEu Algorithm gSaA

Step 1: - ,d Temporary Pointer ysxsa ftlesa head dk address Mkysaxs


struct node * temp = head
Step 2: - ;g check djsaxsa fd head NULL gS ;k ugh ;fn head NULL gS rks return gks
tk;saxsA
Step 3: - ;g check djsaxsa fd head ds Next esa head gS ;k ughA ;fn head dk Next head
gS rks 4 ls 6 rd ds steps dks repeat djsaxsA
Step 4: - head esa NULL MkysasxsA
head=NULL
Step 5: - temp dks free dj nsaxsA
free (temp)
Step 6: - return
Step 7: - tc rd temp ds Next part esa head dk adress ugha vk tkrk rc rd temp dks
vkxs c<+k,xsaA
while(temp->next != head)
temp=temp->next
Step 8: - temp ds next dks free dj nsaxsA
free (temp → next)
Step 9: - temp ds next esa head MkysaxsA
temp → next = head
Step 10: - Exit

Application of Link List


Linked List ,d Primitive data Structure gS ftls fofHkUu izdkj dh Application esa Use esa
fy;k tkrk gSA muesa ls dqN Applications fuEu gSA
(1) fdlh vU; Data Structure dks Implement djus ds fy, tSls Stack queue, tree rFkk
graph
(2) Name dh Directory dks maintain djus ds fy,
(3) Logn Integers ij Arithmetic Operation Perform djus ds fy,
(4) Polynomials dks Manipulate djus ds fy,
(5) Sparts Matrix dks Represent djus ds fy,
Coding Talks

UNIT-III
TREES
Arrary,Linked list,stacks and queues vkfn dks Linear data structure ds Onkjk iznf'kZr fd;k
tkrk gSA bu structure ds Onkjk hierarchical data dks iznf'kZr ugh fd;k tk ldrkA
Hierarchical data esa ancestor-descendant,superior-subordinate,whole part o data
elements ds chp similar relationship gksrh gSA
Jaspal

Iqbal Jaspreet gurpreet Navtej

Noni Manan Aman

mijksDr figure esa jaspal ds descendents dks diagram es hierarchical order esa iznf'kZr fd;k
x;k gSA ftlesa jaspal top of the hierarchy dks iznf'kZr dj jgk gSA Jaspal ds children
iqbal,jaspreet,gurpreet and navtej gSA Iqbal ds ,d child Noni gS,gurpreet ds nks o
jaspreet and navtej ds ,d Hkh ugh gSA bl izdkj ;g dgk tk ldrk gS fd Iqbal ds siblings
Noni, Jaspal ds descendents iqbal,jaspreet,gurpreet and navtej gS rFkk Noni ds ancestors
Manan and Aman gSA
The tree is an ideal data structure for representing the hierarchical data. As there are
many
types of trees in the forest,likewise there are many types of trees in data structure-
tree,binary tree,expression tree,tournament tree,binary search tree,threaded tree,AVL
tree and B-tree.

Tree ,d ideal data structure gS ftlds }kjk hierarchical data dks iznf'kZr fd;k tkrk gSA tSlk
fd ge tkurs gSa fd forest esa cgqr ls tree gksrs gSaA mlh izdkj data structure esa Hkh db izdkj ds
tree gksrs gSa tSls :-
Tree, binary tree, expression tree, tournament tree, binary search tree, threaded tree,
AVL tree and B-tree.
Tree Terminology
Level of the Eement: - Tree esa lcls T;knk iz;ksx esa vkus okyh term level gSA Tree ds first
element dks root dgk tkrk gSA Tree ds Root dks ges'kk Level 1 esa j[kk tkrk gSA mijksDr
Diagram esa Jaspal Level1 esa iqbal,jaspreet,gurpreet and navtej Level 2 esa rFkk Noni,
Aman and Manan Level 3 esa gSaA

Degree of Element: - fdlh Hkh Element dh Degree Number of Children ij Depend djrh
gSaA Leaf Node dh Degree ges'kk 0 gksrh gSa D;ksafd Leaf Node dk Left vkSj Right Part ugha gksrk
gSaA
Degree of The Tree: - fdlh Hkh tree dh Degree Tree esa mifLFkr Maximum Elements ij
Depend djrh gSaA mijksDr diagram esa tree dh Degree 4 gSaA D;ksafd mlesa ,d Nodes ls vf/kd
ls vf/kd 4 Elements tqM+s gSaA
Properties Of Tree : - Tree ds fuEu xq.k gSaA
(1) dksbZ Hkh Node Tree dh Root gks ldrh gSaA Tree esa tqM+h gj Node dk ,d xq.k gksrk gSa fd
izR;sd Node dks fdlh nwljh Node ls tksM+us ds fy, dsoy ,d gh Path gksrk gSaA ,d Tree
ftlesa fdlh Root dks Identify ugha fd;k x;k gSa og Free Tree dgykrk gSaA
(2) ftl Tree esa n No. of Nodes gSa mlesa n-1 Edges gksaxsA
Coding Talks

(3) os Nodes ftudk dksbZ Child Node ugha gksrk leaves ;k terminal node dgykrk gSaA
(4) os Node ftuds ikl de ls de ,d Child gksrk gSa Nonterminal Nodes dgykrs gSaA
(5) Nonterminals Node dks Internal Node rFkk Terminal Node dks External Node dgrs
gSaA
(6) ,d Level ij mifLFkr lHkh Nodes ds fy, Depth vkSj Height dk eku ogh gksrk gSa tks
mlds Level dk eku gksrk gSaA
(7) ,d Node dh Depth dks Node dk Level Hkh dgk tkrk gSaA Tree ds ,d laxzg dks Forest
dgrs gSaA uhps fn;s x, tree ds root vkSj edges tks fd mls tree ls tksM+ jgsa gSa dks gVk
fn;k tk, rks gekjs ikl forest cpsxk ftlesa vU; 3 Trees gksaxs
A
B C D
E F G
I J
H

K L M

A B E

C D F G H I

J K L M
Forest

Binary Tree
,slk Tree ftldh izR;sd Node ds ikl vf/kdre nks Child Nodes gks ldrh gSa] Binary Tree
dgykrk gSaA bls ge bl izdkj Hkh dg ldrs gSa fd Binary Tree esa fdlh Node ds il ds nks vf/kd
Child Nodes ugha gks ldrh gSaA
A

B C

D E C

G
Binary Tree
Binary Tree lkekU; Tree ds ,d fo'ks"k Class esa vkrs gSaA

Properties of Tree
Binary Tree ds fuEufyf[kr Properties gSa &
(1) ,d Binary Tree ftlesa n vkUrfjd Nodes Internatl Nodes gSa ] mlesa vf/kdre~ n+1
External Nodes gks ldrh gSaA ;gka ij Root Node dks Hkh Internal Node ds :i esa fxuk
x;k gSaA
(2) ,d Binary Tree ftlesa n Internal Nodes gSa] ds External Path dh yEckbZ] Internal
Path dh yEckbZ dh nks xuqh gksrh gSaA
Coding Talks

(3) ,d Binary Tree ftlesa n Internal Nodes gSa dh mapkbZ height yxHkx log2n gksrh gSaA
izR;sd Binary Tree ,d Tree gksrk gS ijUrq izR;sd Tree ,d Binary Tree ugha gksrkA
,d iw.kZ Binary Tree (Full vFkok Complete Binary Tree) dh leLr Internal Nodes dh
Degree gksrh gSa vkSj leLr Leaves ,d gh Level ij gksrh gSaA
Binary Trees dks 3 Parts esa ckaVk x;k gSaA
(1) Strictly Bianry Tree
(2) Complete Binary Tree
(3) Full Binary Tree
(1) Strictly Binary Tree: - og Tree ftlesa de ls de 0 ;k 2 Nodes dk gksuk vko';d gSa
vFkkZr~ ,d Tree esa ,d Node ls ;k rks Left vkSj Right nksuks Values dks Insert fd;k
tk;sxk ;k ,d Hkh ughaA

B C

D E
(2) Complete Binary Tree: - og Tree ftldk Level Same gks All most Complete
Binary Tree dgykrk gSaA tSls %&
A

B C

D E F

(3) Full Binary Tree: - og Tree tks Strictly Binary o Complete binary nksuks gks Full
Binary Tree dgykrk gSaA ;g Triangle Property dks iznf'kZr djrk gSaA
A

B C

D E F G

Tree Representation as Array


Array ds }kjk Binary Tree dks iznf'kZr djus ds fy, Tree ds Nodes dh Numbering djuh gksrh
gSaA fdlh Hkh Tree ds Nodes dh Numbering djus ds fy, ge Binary Tree dks Full Binary
Tree ekudj gj Node dks ,d Number ns nsrs gSa vkSj ftl Number ij og Node gksrk gSa mls
Array esa Store dj nsrs gSaA
bldk Example fuEu gSaA
1 A

2 B 3 C

4 D 5 E 6 F
11
7 8 9 10 G 12
Coding Talks

1 2 3 4 5 6 7 8 9 10 11 12
A B C D E F G
Array Representation of a Binary Tree

Link List Representation of Binary Tree: - fdlh Hkh Tree dks Linked List }kjk lgh ls
iznf'kZr fd;k tk ldrk gSaA ge tkurs gSa fd ,d Binary Tree esa vf/kdre nks Child Node gh gksrs
gSaA vr% bl izdkj ds Representation esa node Value ds nksuks vksj ,d&,d Pointer Maintain
djrs gSaA ftlls mldh Child Nodes dks iznf'kZr fd;k tkrk gSaA Tree dk og Hkkx ftlesa fdlh
lwpuk dks Store ugha fd;k tkrk mUgsa NULL ds }kjk lqjf{kr j[kk tkrk gSaA

B C

D E F

Linked List Representation of a Binary Tree

Basic Operations on Binary Tree


Binary Search Tree ,d fo'ks"k izdkj dk Binary Tree gS tks ;k rks [kkyh gS ;k Left Sub Tree
dh lkjh Information Root ls NksVh gS vkSj Right Sub Tree dh lkjh Information Root ls cM+h
gSa Left vkSj Right nksuksa gh Sub Trees Binary Search Tree gSaA
Binary Search Tree esa fuEu Operations dks Perform djuk gksrk gSaA
(1) Insertion
(2) Delition
(3) Traversal
Link List }kjk tc Tree Representation fd;k tkrk gSa rks Node ds rhu Hkkx gksrs gSa
(1) Left (2) Info (3) Right
blds fy, fuEu Strcutre dks cuk;k tk,xk
struct node
{
struct node * left;
int info;
struct node * right;
};
Coding Talks

Binary Search Tree esa Node dks tksM+uk% & blds fy, lcls igys ,d Temprarory Variable
T dks Root ij vkSj ,d Variable Prev dks NULL ij Hkst nsrs gSaA Prev Variable T ls igys
okyh Node dks iznf'kZr djsxk vc ,d u;s Node dks Memory Allocate djsaxs mlds Information
Part esa Data rFkk left vkSj Right esa NULL Value Insert djsaxs blds ckn Loop ds }kjk Prev
dks T ij Hkstk tk,xk vkSj ;g Check fd;k tk,xk fd D;k Data dh Value T dh Value ls NksVh
gSaA
;fn Data dh Value T ls NksVh gS rks mls T ds Left esa Insert dj fn;k tk,xk ;fn Data dh
Value T ls cM+h gS rks mls T ds Right esa Insert fd;k tk,xkA vc ;g Check djsaxs fd Previous
Variable tks fd T ds ihNyh Node dks iznf'kZr dj jgk gSa NULL rks ugha gSaA ;fn NULL gS vr%
Tree [kkyh gSa rks u;s Node dks Root cuk nsaxsA

Step 1: - ,d Temporary Pointer ysxsa ftlesa root dk address Mkysaxs


struct node * t = root
Step 2: - struct node * prev = NULL
Step 3: - struct node * n = malloc (sizeof (struct node))
Step 4: - n → info = data
n → left = NULL
n → right = NULL
Step 5: - while (t != NULL and data != t → info)
set prev = t
check whether (data < t → info)
if yes then
t = t → left
else
t = t → right
Step 6: - check whether (data = t → info)
if yes then return
Step 7: - check whether (prev = NULL)
if yes then
root = n
return
Step 8: - check whether (data < prev → info)
if yes then
prev → left = n
else
prev → right = n
Step 9: - Exit

fuEu Numbers dks ;fn Binary Tree esa Insert djuk gks rks mlds fy, fuEu Steps follow djsaxs
12, 15, 18 10, 11, 14, 22
(1) lcls igys 12 dks add djuk gS pqafd vHkh rd dksbZ Node Add ugha gqbZ gS] blhfy, ;g
lk/kkj.k :i ls Add gks tk,xhA
12
(2) blds ckn 15 dks Add djkuk gSA pwafd 15, 12 ls cM+k gS blhfy, 15, 12 ds nkbZa vksj Add
gksxkA

12

15
Coding Talks

(3) 18 dks Add djrs gq, geus ik;k fd 18, 12 ls cM+k gS ijUrq pwafd 12 ds nkbZa vksj 15 igys ls
gh gS blhfy, ge bldh 15 ls Hkh rqyuk djsaxsA rqyuk djus ij geus ik;k fd 18, 15 ls Hkh
cM+k gSA blhfy, 18, 12 ds Hkh nkbZa vksj Add gksxkA
12

15

18

(4) 10 dks Add djrs gq, geus ik;k fd 10, 12 ls NksVk gS] blhfy, 10, 12 ds ckbZa vksj Add
gksxkA

12

10 15

18

(5) 11 dks Add djrs gq, geus ik;k fd 11, 12 ls rks NksVk gS] blhfy, 12 ds ckbZa vksj Add
gksxk ijUrq 12 ds ckbZa vksj igys ls lqjf{kr lwpuk 10 ls cM+k gS] blhfy, 11, 12 ds ckbZa vksj
ijUrq 10 ds nkbZa vksj Add gksxkA
12

10 15

11 18

(6) 14 dks Add djrs gq, geus ik;k fd 14, 12 ls cM+k gS bfly;s ;g 12 ds nkbZa vksj Add
gksxkA ijUrq ;gka ij igys ls gh 18 lqjf{kr gSA blfy;s geus 14 dh rqyuk 15 ls dh vkSj
ik;k fd 14, 15 ls NksVk gS blhfy;s 14, 12 ds nkbZa vksj ijUrq 15 ds ckbZa vksj Add gksxkA
12

10 15

11 14 18

(7) vc gesa vfUre lwpuk vFkkZr~ 22 dks Add djuk gSA 22 dks Add djrs gq, geus ns[kk fd 22,
12 ls cM_k gS] blfy;s 22, 12 ds nkbZa vksj Add gksxkA ijUrq 12 ds nkbZa vksj igys ls 15
ljqf{kr gS] blfy;s geus bldh rqyuk 15 ls dh vkSj ik;k 22, 15 ls Hkh cM+k gSA blfy;s 22,
15 ds Hkh nkbZa vksj tk,xk] ijUrq 15 ds nkbZa vksj 18 lqjf{kr gS] vr% 22 dh rqyuk 18 ls Hkh
Coding Talks

(8) dh vkSj ik;k fd 2, 18 ls Hkh cM+k gS] blfy;s 22, 18 ds nkbZa vksj Add gksxkA vr% ge dg
ldrs gSa fd 22, 15 ds nkbZa vksj lqjf{kr 18 ds nkbZa vksj Add gksxkA

Delete a Node from Binary Tree.

(1) 15 dks Delete djrs gq, geus ik;k fd 15 ds left vkSj right child nksuksa Hkjs gq, gSa] blfy;s
geus viuh Algorithm ds vuqlkj bldk Inorder (14, 15, 18) fudkyk vkSj ik;k fd 18
bldk Inorder sucessor gSa blfy;s 15 dks delete djds 18 dks Promote dj fn;kA vr%
vc Tree fuEukuqlkj jg tkrk gS &
12

10 18

11 14 22

(2) 10 dks Delete djrs gq, geus ik;k fd 10 dk dsoy right child gh gS] blfy;s geus blds
child dks promote djds 10 dks delete dj fn;kA vr% vc tree fuEukuqlkj jg tkrk gS &
12

11 18

14 22

Traversal of Binary Tree


Binary Tree dks Traverse djuk lcls Important Operation gSaA Binary Tree ij cgqr lkjs
Operations Perform fd;s tkrs gSaA Traversing djuk tree ds gj Node ij gksrk gSaA
Traversing ,d Process gSaA ftlesa Tree ds gj Nodes ls xqtjuk gksrk gSaA fdlh Hkh Tree dks
Traverse djus ds fy, lcls igys mlds root dks check fd;k tkrk gSaA blds ckn left Sub Tree
dks Traverse djrs gSa rFkk vUr esa Right sub tree dks traverse djrs gSaA Normally Binary
Tree ds Traversal 3 izdkj ds gksrs gSaA
(1) In order Traversal: - (Left, Root, Right)
(i) lcls igys Left Sub Tree dks Traverse djksA
(ii) Root dks Traverse djksA
(iii) Last esa Right Sub Tree dks Traverse djksA

(2) Pre Order Traversal: - (Root, Left, Right)


(i) Root dks Traverse djksA
(ii) Left Sub Tree dks Traverse djksA
(iii) Last esa Right Sub Tree dks Traverse djksA

(3) Post Order Traversal: - (Left, Right, Root)


(i) Left Sub Tree dks Traverse djksA
(ii) Right Sub Tree dks Traverse djksA
Coding Talks

(iii) Root dks Traverse djksA

B C

D E F

G H I

mijksDr Tree esa ;fn rhuksa izdkj dj Traversal djuk gks rks blds fy, Tree dks Root ls Access
fd;k tk,xkA

In Order Traversal of Tree: - (Left, Root, Right)


lcls igys Root Node A ij tk,saxsA vc ns[ksaxs fd root dk Left Full gS vr% A ds Left esa B gSaA
vc B ds Left dks Check djsaxs B dk Left Hkh Full gSaA blds Left esa D gSaA ij D dk Left [kkyh gSa
vr% ;gka ls Left Root vkSj Right dks Traverse djsaxsA D dk Left [kkyh gSa vr% D vkSj G (Root,
Right) dks Traverse djsaxsA
(1) bl Tree dk In order Traversal D vkSj G gksxkA D
(2) D [qkn B dk Left gSa vr% vc Root dks fy[ksaxs DGB
(3) B dk Right E gSa rFkk E dk Left H gSa vr% EH okys Tree ds vUrZxr G
Traversal HE gksx vr% Tree
D G B H E
(4) Left sub Tree complete gks pqdk gSa vc root dks fy[ksaxs
D G B H E A
(5) vc Right Sub Tree dks Access djsaxsA F dk Left NULL gS rFkk Right
esa I gSaA L, Root, R ds vk/kkj ij Traversal FI gksxhA
D G B H E A F I
(6) F dk root C esa vkSj C dk Right NULL gSaA vr% Traversal
D G B H E A F I C

Pre Order Traversal: - (Root, Left, Right)


lcls igys Root Node A ij tk,saxsA vc ns[ksaxs fd root dk Left Full gS vr% A ds Left esa B gSaA
vc B ds Left dks Check djsaxs B dk Left Hkh Full gSaA blds Left esa D gSaA ij D dk Left [kkyh gSa
vr% ;gka ls Root, Left vkSj Right dks Traverse djsaxsA
(1) lcls igys root dks nsaxs Tree dk Root A gSa vr% Tree Traversal esa First Element A gSA
A
(2) A ds Left Sub Tree dks Check djsaxs A ds Left esa B vr% Tree Traversal
A B
(3) B dk Left D gS vkSj G dk Root D gS vr% Tree Traversal
A B D
Coding Talks

(4) D dk left NULL gS vkSj Right esa G gS vr% root Left Right ds vk/kkj ij Tree
Traversal
A B D G
(5) B ds right esa E gS vkSj E H dk Root gS vr% Root left right ds vk/kkj ij Tree
Traversal
A B D G E H
(6) vc Right Sub Tree dks Traverse djsaxs A dk Right C gS vr% Tree Traversal
A B D G E H C
(7) C ds Left esa F gS rFkk F I dk Root gS vr% Tree Traversal
A B D G E H C F I

Post Order Traversal: - (Left, Right vkSj Root)


lcls igys Root Node A ij tk,saxsA vc ns[ksaxs fd root dk Left Full gS vr% A ds Left esa B gSaA
vc B ds Left dks Check djsaxs B dk Left Hkh Full gSaA blds Left esa D gSaA ij D dk Left [kkyh gSa
vr% ;gka ls Left, Right, Root dks Traverse djsaxsA
(1) A dk Left B gS B dk Left D gS vkSj D dk Left NULL gS ij D dk Right G gS vr% Left
Right Root ds vk/kkj ij Traversal
G D
(2) B dk Left Sub Tree Complete gS vkSj B ds right sub tree ij t,saxsA B ds right ij E
gS vkSj E ds Left esa H vr% Traversal
G D H E B
(3) A dk Left Sub Tree traverse gks pqdk gS vc A ds Right Sub Tree dks traverse djsaxsA
(4) A ds Right esa C gS C ds Left esa F gS rFkk F dk Right I gS vr% igys IF dks ysaxs vr%
Tree Traversal
G D H E B F I
(5) C dk Left Sub Tree complete traverse gks pqdk gS vr% vc Right Sub Tree NULL gSA
vr% Tree Traversal
G D H E B F I C
(6) lcls vUr esa Root Node dks fy[kk tk,xkA
G D H E B F I C

Threaded Binary Tree


tc fdlh binary tree dks link list ds :i esa iznf'kZr fd;s tkrs gSa rks Normally vk/ks ls T;knk
Pointer Field NULL gksrs gSaA og Space ftls NULL Entry }kjk Occupy fd;k tkrk gSaA blesa
Valuable Information dks Hkh Store dj ldrs gSaA blds fy, ,d Special Pointer dks Store
fd;k tkrk gSa tks vius Higher Nodes dks Point djrk gSaA bu Special Pointer dks Threades
dgrs gSaA rFkk ,slk Tree ftlesa bl izdkj ds Pointers dks j[kk tkrk gSa Threaded Binary Tree
dgykrs gSaA
Threads Normal Pointer ls vyx gksrs gSaA Threaded Binary Tree esa Threades dks dotted
line ds }kjk iznf'kZr fd;k tkrk gSa blds fy, ,d Extra Field dks Memory esa Store djrs gSa ftls
Tag ;k Flag dgk tkrk gSaA Threaded Binary Tree dks iznf'kZr djus ds cgqr ls rjhds gSaA
(1) Tree ds Right NULL Pointer ds gj Node dks Successor ls Replace djok;k tk,xkA
;g In Order Traversal ds vUrxZr vkrk gSaA blfy, bl Thread dks Right Thread dgk
tkrk gSa vkSj bl izdkj dk Tree Right Threaded Binary Tree dgykrs gSaA
(2) Tree ds Left NULL Pointer ds gj Node dks Predecessor ls Replace djok;k tk,xkA
;g In Order Traversal ds vUrxZr vkrk gSaA blfy, bl Thread dks Left Thread dgk
tkrk gSa vkSj bl izdkj dk Tree Left Threaded Binary Tree dgykrs gSaA
(3) tc Left vkSj Right nksuksa Pointers dks Predecessor vkSj Successor ls Point djok;k
tkrk gSa rks bl izdkj ds Tree dks Full Threaded Binary Tree dgykrs gSaA
Coding Talks

Threaded Binary Tree nks izdkj ds gksrs gSa

(1) One-way Threaded Binary Tree


(2) Two-way Threaded Binary Tree

One-way Threading esa dsoy ml Node ls tksM+ nsrs gSa tks traversal esa mlds ckn vkrh gSaA
Two-way Threading esa Node dks mu nksuksa Node ls tksM+ nsr gSa tks Traversal esa mlds nksuks
vksj gSaA

B C

X F G X D X E F

X H
Right Threaded Binary Tree (One-Way Threading)

B C

X F G D E X

H
Left Threaded Binary Tree (One-Way Threading)
Coding Talks

B C

X F G D E X

Full Threaded Binary Tree (Two-Way Threading)


Coding Talks

B Tree
;g ,d fo'ks"k izdkj dk tree gS ftlds root esa ge ,d ls vf/kd Node j[kdj mlesa ls ,d ls vf/kd
Branches fudky ldrs gSaA bl izdkj ds tree dks M-Way tree Hkh dgk tkrk gSa ;gka ,d
Balanced M-way tree dks B Tree dgrs gSaA bl Tree ds Node ,d ls vf/kd ;k cgqr lkjs
Records vkSj childrens ds Pointers dks j[k ldrs gSaA B Tree dks Balanced Sort Tee Hkh dgk
tkrk gSaA ;g Binary Tree ugha gksrk gSaA ,d B Tree ds fuEu xq.k gSa %&
(1) izR;sd Node vf/kd ls vf/kd N Childrens j[k ldrh gSa o de ls de N/2 ;k fdlh vU;
la[;kvksa ds Children tks 2 vkSj n ds chp dgha ij gSA
(2) gj Node esa children ls 1 de dh vk ldrh gSaA
(3) Node esa Keys dks ifjHkkf"kr Øe esa O;ofLFkr fd;k tkrk gSaA
(4) Left Sub Tree dh lHkh Keys, Key dh Predecessor gksrh gSaA
(5) Right Sub Tree dh lHkh Keys, Key dh Successor gksrh gSaA
(6) tc ,d iwjs Hkjs gq, Node esa dksbZ Key tksM+h tkrh gSa rks Key nks Nodes esa VwV tkrh gSa vkSj
og Key tks chp esa gksxh mls Parent Node esa j[kk tk,xk rFkk NksVh Value okyh Key
Parent ds Left esa rFkk cM+h Value okyh Key Parent ds Right esa jgsxhA
(7) lHkh Leaves ,d gh Level ij gksrh gSa vr% V ds Level ds mij dksbZ Hkh [kkyh Sub Tree
ugha gksxkA

B Tree esa tksM+uk %&


B-Tree esa fdlh Hkh Key dks tksM+us ls igys ;g ns[kk tkrk gSa fd og Key dgka tksM+h tkuh gSA ;fn
Key igys ls fufeZr Node esa tk ldrh gS] rks Key dks tksM+uk vklku gS] og Node dks nks Hkkxksa
(Nodes) esa ckaV nsrh gSaA og Key ftldk eku ek/;eku (chp dk) gksrk gS] og Node esa jgrk gS vkSj
blds ck,a child esa bl Key ds nk,a fLFkr leLr Keys vk tkrh gSaA B-Tree esa Insertion (tksM+uk)
fuEufyf[kr mnkgj.k ls foLrkj ls le>k;k tk jgk gS &
ekuk gesa fuEufyf[kr Keys dk 4 Order dk Tree cukuk gS &
44 33 30 48 50 20 22 60 55 77
lcls igyh key 44 gS ftls gesa tree esa tksM+uk gSA pwafd ;g igyh key gS blfy, bl Key ds }kjk
gh Root Node dk fuekZ.k gksxk &
44
blh izdkj ge 33 vkSj 30 dks Hkh muds LFkku ds vuq:i Node esa tksM+ nsaxs &
44 33 30
pw¡fd geus ;g 4 order dk tree cuk;k gS blhfy, bldh ,d Node esa vf/kdre~ 3 Keys gh j[k
ldrs gSaA vr% vc tks Hkh key blesa tksM+h tk;sxh og bl (root) Node dks nks Hkkxksa esa foHkDr dj
nsxhA ;gka gesa 48 tksM+uk gS&
44 33 44 48

33

30 44 48
vc ge 50 dks blds LFkku ds vuq:i tksM+ nsaxs &
33

30 44 48 50
vc ge 20 vkSj 22 dks blds LFkku ds vuq:i tksM+ nsaxs &
33

20 22 30 44 48 50 60
Coding Talks

vc gesa 60 dks tksM+uk gSA tSlk fd ge ns[k ldrs gSa 60, 50 ds ckn tqM+sxk vkSj 50 okyh Node
iw.kZr;k Hkj pqdh gSaA blhfy;s 60 ds tqM+us ij ;g Node nks Hkkxksa esa foHkDr gks tk,xh vkSj ;g Node
mu nksuksa dk Parent Node dk eku root node esa merge gks tk,xk &
33

20 22 30 48

40 50 60

33 48

20 22 30 50 60

40

vc gesa 55 dks blds LFkku ds vuq:i tksM+ nsaxs &


33 48

20 22 30 50 55 60 77

40
vc gesa 77 dks tksM+uk gSaA tSlk fd ge ns[k ldrs gSa fd 77, 60 ds ckn tqM+sxk] vr% ;gka ij Hkh
Node foHkDr gksxh vkSj pwafd vHkh Hkh Root Node esa LFkku fjDr gS blfy, bl Node dk eku Root
esa pyk tk,xk & 33

20 22 30 55

40
50 60 77

33 48 55

20 22 30 60 77

40 50

vc ;g Tree Belanced gS] vkSj bl izdkj cuk;k x;k gS fd bldh leLr Leaves ,d gh Level ij
gSaA

B – tree esa ls feVkuk


tSlk fd ge tksM+us esa igys key rFkk mldk LFkku ns[krs gSa mlh izdkj feVkus esa Hkh igys Key rFkk
mlds LFkku dks <wa<rs gSaA
Coding Talks

;fn feVkbZ tkus oykh key ,d terminal (leaf) gS] tks feVkuk ljy gS] vFkkZr+ dsoy ml key dks
Node esa ls feVk nsuk gksxkA
;fn feVkbZ tkus okyh key ,d terminal (leaf) ugha gS rk] bls blds successor ds eku ls cny
fn;k tkrk gSaA bl izdkj ;fn successor, terminal Node esa gSa] rks ogka ls og feVk fn;k tk,xkA
R

E G J W

B C D H I K P X Y Z

F V

bl izdkj ;fn sucessor, non-terminal node esa gS] rks mldk sucessor, replace gksxk vkSj ;gh
lkjh 'krsZ bl sucessor ds lkFk Hkh yxsaxhA vr% ;g ns[kk tk, rks izR;sd izdkj ds deletion esa
terminal node esa ls ,d key vo'; feVsxhA
;fn ge 'H' dks Delete djrs gSa rks cgqr vklku gS vFkkZr+ dsoy 'H' dks Node esa ls Delete dj nsaxs
&
R

E G J W

B C D I K P X Y Z

F V

;fn ge 'J' dks feVkrs gSa rks gesa blds sucessor, K dks blds LFkku ij dkWih djuk gksxk &
R

E G K W

B C D I P X Y Z

F V
Coding Talks

;fn ge 'F' dks feVkrs gSa] rks ;gka underflow dh fLFkfr mRiUu gks tk,xhA ns[kk tk, rks ;gka F ds
[kRe gksrs gh G dk ,d child [kRe gks tk;sxkA vr% G dks remove djds E ds child node esa Hkst
fn;k tk,xk] rks blesa fuEu fLFkfr mRiUu gks tk,xh &
R

E K W

B C D I P X Y Z

F V

E K W

B C D G I P X Y Z

F V

bl izdkj fn, x, Øe dk B-Tree cuk;k tk ldrk gS vkSj fdlh B-Tree esa ls fdlh okafNr key dks
feVk;k Hkh tk ldrk gSaA

AVL Tree (Height Balanced Tree)


,d Binary Tree, ftldh height, h gS] iw.kZ :i ls lUrqfyr (balanced) rHkh gksrk gS ;fn mldh
leLr leaves ;k rks level ;k fQj h-1 level ij gSa vkSj ;fn h-1 level ls de level ij fLFkr leLr
Nodes ds nks children gSaA
ge ;g Hkh dg ldrs gSa fd ;fn fdlh Binary Tree dh izR;sd Node dh left ls lcls yEcs Path dh
Length, right esa lcls yEcs Path dh length ds yxHkx cjkcj gksrh gS] rks og Binary Tree ,d
Balanced Tree dgykrk gSaA
;fn fdlh Binary Tree dh izR;sd Node ds fy, Left Sub – Tree dh height vkSj Right Sub-
Tree dh height eas ;fn dsoy ,d dk vUrj gh gS] rks ,slk Binary Tree Height Balanced Tree
dgykrk gSaA
,d yxHkx Height Balance Tree dks AVL Tree dgk tkrk gSaA

Height Balance Tree dk fuekZ.k


Coding Talks

Height Balance Tree ds fy, izR;sd Node dk ;g xq.k gksrk gSa fd blds Left Sub-Tree dh
Height Right Sub-Tree dh Height ls ;k rks 1 T;knk ;k cjkcj ;k fQj 1 de gksrh gSaA bls
Balancing Factor (bf) Hkh dg ldrs gSaA

;fn nksuksa Sub – Trees cjkcj gS rks] bf=0


;fn Right Sub-Tree dh height cM+h gS] rks bf = -1 (H (left)<H(Right))
;fn Left Sub-Tree dh Height cM+h gS rks vf = +1 (H(left)>H(Right))
vFkkZr~ bf = left height – right height
tc Balancing Factor – 2 gksrk gS rks ge tree dks okekorZ (Anti-Clockwise) ?kqek nsrs gSa
vkSj tc Balancing Factor+2 gksrk gSa rks ge tree dks nf{k.kkorZ (Clockwise) ?kqek nsrs gSaA

mnkgj.k & 3 5 11 8 4 1 12 7 2 6 10
dks gesa height balanced tree cukuk gSaA

ge bl fØ;k dks root vFkkZr~ List dh 0


igyh lwpuk ls izkjEHk djrs gSa & 3

-1
vc Tree esa tksM+us ds fy, 5 gSA ;g 3
igys tree ds right esa tk,xk &
0
5

ifj.kkeLo:i izkIr tree vHkh Hkh -2


3
balanced gSA D;ksafd bf = -1 gSA vc
gkejs ikl tree esa tksM+us ds fy, 11 gSA -1
;g 5 ds Right esa tk,xk&
5

0
11
0
5
pw¡fd bf dk eku –2 gks x;k gS] blfy,
ge Tree dks Anti-clockwise ?kqek nsaxs 0
& 0
3 11

-1
5

vc gekjs ikl tksM+us ds fy, 8 gS] ;g 0 +1


11 ds left esa tk,xk & 3 11

0
8
Coding Talks

ifj.keLo:i izkIr tree vHkh Hkh 0


5
balanced gSA vc gesa 4 dks tksM+uk gS
;g 3 ds right esa tk,xk &
-1 +1
3 11

0 0
4 8

0
ifj.kkeLo;i izkIr tree vHkh Hkh 5
balanced gSA vc gesa 1 tksM+uk gSA ;g
3 ds left esa tk,xk & 0 +1
3 11

0 0 0
1 4 8

0
5
ifj.kkeLo:i izkIr tree vHkh Hkh
balanced gSA vc gesa 12 dks tksM+uk gSA 0 0
;g 11 ds right esa tk,xk & 3 11

0 0 0 0
1 4 8 12

-1
ifj.kkeLo:i izkIr tree vHkh Hkh 5
balanced gSA vc gesa 7 dks tksM+uk gSA
;g 8 ds left esa tk,xk & 0 +1
3 11

0 0 +1 0
1 4 8 12

0
7
Coding Talks

0
5

+1 +1
ifj.kkeLo:i izkIr tree vHkh Hkh 3 11
balanced gSA vc gesa 2 dks tksM+uk gSA
;g 1 ds right esa tk,xk &
-1 0 +1 0
1 4 8 12

0 0
2 7

-1
5

1 +2
3 11
ifj.kkeLo:i izkIr tree vHkh Hkh
balanced gSA vc gesa 6 dks tksM+uk gSA
;g 7 ds Left esa tk,xk & 0
-1 +2 0
1 4 8 12

0 +1
2 7

0
6

0
5

+1 +1
3 11
pw¡fd bf dk eku 2 gks x;k gS blfy;s ge
tree dks clockwise ?kqek nsaxs &
-1 0 0 0
1 4 7 12

0 0 0
2 6 8
Coding Talks

0
5

+1 2
3 11
ifj.kkeLo:i izkIr tree vc balanced
gSA vc gesa 10 dks tksM+uk gSA ;g 8 ds
right esa tk,xk & -1 0 -1 0
1 4 7 12

0 0 -1
2 6 8

0
10

0
5

pw¡fd bf dk eku 2 gks x;k gS blhfy, +1 0


gesa tree dks clock wise ?kqekuk gksxkA 3 8
;fn ge 7 dks Promote djrs gSa] rks 7
ds right okyk sub tree dkQh yEck gks
-1 0 +1 0
tk,xk vkSj gesa vusd ckj balancing
1 4 7 11
djuh iM+sxhA blhfy, ge 8 dks
promote djsaxs &
0 0 0 0
2 6 10 12

bl izdkj izkIr tree gh vHkh"V Height Balanced Tree gSA

B+ Tree
B+ Tree Data Structure B Tree dk Extension gSA bls Index File Organization dh
Technique dks Implement djus ds fy, Use esa fy;k tkrk gSA B+ Tree ds 2 Parts gksrs gSaA
(1) Index Set
(2) Sequence Set

B+ Tree ds vUrZxr Leaf Node dk Right Pointer Next Leaf Node dks Point djrk gSaA

69
Index Set
69 43 110 136
Coding Talks

Insert into B+ Tree: - B Tree dh rjg gh B+ Tree eas Hkh ubZ Value dks Insert fd;k tkrk gSa
tc Leaf Node dks 2 Nodes esa Split dj nsrs gSaA
Delition from B+ Tree: - B+ Tree ds vUrZxr fdlh Hkh Key dks Delete djuk B Tree ls vklku
gSA tc fdlh Hkh Key Value dks Leaf ls Delete fd;k tkrk gSa rks Index Set esa ls fdlh Key dks
Delete djus dh vko';drk ugha gksrh gSaA

B* Tree :– bl Data Structure dks Knuth }kjk cuk;k x;k Fkk bldks cukus dk eq[; mn~ns';
Insertion vkSj Deletion esa gksus okys Overhead dks Reduce djuk gSA ,d vU; Primary mn~ns';
Search Performance dks Improve djuk Hkh Fkk B* Tree ,d B Tree gS ftlesa gj Node Half
Full u gksdj 2/3 Full gksrk gSaA B* Tree ds }kjk Node Splitting dks Reduce fd;k tkrk gSaA tc
Node Full gks tkrs gSa rks mUgsa Split djus ls igys Re-Distribution Scheme dks Use esa ysrs gSaA
Insertion rFkk Deletion Operation B Tree dh rjg gh gksrs gSaA
Coding Talks

UNIT – IV
Sorting: - fn;s x;s elements dks Ascending (vkjksgh) ;k descending (vojksgh) Order esa tekuk
sortring dgykrk gSaA
Searching: - fn;s x;s Item esa ls fdlh ,d Item dks Findout djuk Searching dgkykrk gSaA
Sorting vkSj Searching lkekU;r% file ds records ij Perform dh tkrh gSaA blyh, dqN
Standard Terminology dks Use esa ysrs gSaA

Sorting
Sotring ,d Important Operation gS vkSj Normally blds fy, cgqr lkjh Applications dks
Use eas fy;k tkrk gSaA Sorting izk;% ,d List Of Elements ij Perform dh tkrh gSaA ,slh List
dh Sorting dks Internal Sorting dgrs gSa ftlesa List ds NksVs%NksVs Hkkxksa dks Sort fd;k tkrk gSaA
List iw.kZ :i ls Computer dh Primary Memory esa jgrh gSaA
blds foijhr tc Sorting dks Secondary Memory esa Perform fd;k tkrk gSa rks bl izdkj dh
Sorting External Sorting dgrs gSaA

Classification of Sorting Methods: - Sorting dk lcls Easy rjhdk lcls NksVh Key ds lkFk
,d Element dks pquuk gksrk gSa pquk x;k Element Array ls vyx gks tkrk gSaA vkSj cps gq,
element esa ls Again NksVs Element dks pquk tkrk gSaA bl rjhds dks Choosing Element With
Sorting dgk tkrk gSaA bl rjg ge dbZ Sorting Family tSls Sorting With Merging, Sorting
with insertion o Sorting with exchange dks esa esa ysrs gSaA bu lHkh rjhdksa dks Comparative
Sort dgk tkrk gSaA Sorting ,d Alternate Approach gSaA ftlesa Elements dks Arrays esa
Collect fd;k tkrk gSaA

Advantages of Sorting: -
(1) Data Sensitiveness: - dqN Sorting dh rjhdks esa Sequence dk /;ku j[kk tkrk gSaA
bl rjhds ds }kjk data dh Sensitiveness dks iznf'kZr fd;k tkrk gSaA ogha nwljh vksj tks
Data Sensitive ugha gksrk gSaA og Working Type dk dqN le; Waste dj nsrk gSaA
(2) Stability: - tc dksbZ Hkh izkjfEHkd List ftlesa Element gksrs gSaA tks fd Sequence esa
ugha gksrs gSa ds }kjk dksbZ Hkh Sorting dks Use esa fy;k tk ldrk gSaA
(3) Storage Requirement: - dqN Sorting rjhds, Original Array ds }kjk Re –
Arrangement of Element ij fuHkZj djrs gSaA

Insertion Sort: - bl Sorting esa Set of Value ls ugha cfYd miyC/k Sort file esa Elements dks
Fill djrs gSaA tSls Array ftlesa N Element A[1], A[2].... AN Memory esa gS rks blds fy, fuEu
izdkj ds Steps dks Follow fd;k tk;sxkA
Pass1: A[1] igys ls Sorted gS
Pass2 :A[2] dks A[1] ls igys ;k ckn esa Insert fd;k tk;sxk bl izdkj Array dks Sort djsaxsA
Pass3 :A[3] dk A[1] o A[2] ls Comparision fd;k tk;sxk ;fn A[3], A[1] o A[2] ls NksVk gS
rks A[3] dks lcls igys Insert dj fn;k tk;sxkA ;fn A[3], A[1] ls cM+k vkSj A[2] ls NksVk
gks rks A[3] dks A[1] vkSj A[2] ds chp esa Insert dj fn;k tk,xkA ;fn A[3], A[1] vkSj
A[2] nksuksa ls cM+k gS rks mls nksuks ds ckn Insert dj fn;k tk;sxkA vr%
Pass4 :bl izdkj ;g Øe fujUrj pyrk jgsxA
Pass5 :bl izdkj Array ds Elements dks Proper Place ij Insert dj fn;k tk;sxkA

Example: - Insertion Sort dks le>us ds fy, fuEu Method dks Use esa ysrs gSaA
fuEu Array fn;k x;k gSaA
77 33 44 11 88 22 66 55
bl Array dks sort djus ds fy, fuEu Steps dks Follow djsaxsA
Coding Talks

1. lcls igys Array esa 77 dks Store fd;k tk;sA


77 33 44 11 88 22 66 55
2. vc 33 dks Array esa Insert djsaxs vkSj Check djsaxs fd 33, 77 ls NksVk gS ;k cM+k vkSj
comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA

77 33 44 11 88 22 66 55

33 77 44 11 88 22 66 55

3. vc 44 dks Array esa Insert djsaxs vkSj Check djsaxs fd 44, 33 vkSj 77 ls NksVk gS ;k cM+k vkSj
comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA

33 77 44 11 88 22 66 55
33 44 77 11 88 22 66 55
4. vc 11 dks Array esa Insert djsaxs vkSj Check djsaxs fd 11- 33, 77 vkSj 44 ls NksVk gS ;k cM+k
vkSj comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA

33 44 77 11 88 22 66 55

11 33 44 77 88 22 66 55
5. vc 88 dks Array esa Insert djsaxs vkSj Check djsaxs fd 88- 33, 77, 44 vkSj 11 ls NksVk gS ;k
cM+k vkSj comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA 88 Array esa
Inserted lHkh elements ls cM+k gSA vr% bls Array ds Last esa Insert fd;k tk;sxkA
11 33 44 77 88 22 66 55
6. vc 22 dks Array esa Insert djsaxs vkSj Check djsaxs fd 22- 33, 77, 44, 11 vkSj 88 ls NksVk gS
;k cM+k vkSj comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA 22 Array
esa Inserted Value 11 ls cM+k vkSj 33 ls NksVk gS vr% bls 11 vkSj 33 ds chp esa Insert dj fn;k
tk;sxkA
11 33 44 77 88 22 66 55

11 22 33 44 77 88 66 55

7. vc 66 dks Array esa Insert djsaxs vkSj Check djsaxs fd 66- 11, 22, 33, 44, 77 vkSj 88 ls NksVk
gS ;k cM+k vkSj comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA 66
Array esa Inserted Value 44 ls cM+k vkSj 77 ls NksVk gS vr% bls 44 vkSj 77 ds chp esa Insert
dj fn;k tk;sxkA
11 22 33 44 77 88 66 55

11 22 33 44 66 77 88 55

8. vc 55 dks Array esa Insert djsaxs vkSj Check djsaxs fd 55- 11, 22, 33, 44, 66, 77 vkSj 88 ls
NksVk gS ;k cM+k vkSj comparision ds ckn mls mlds lgh LFkku ij Insert dj fn;k tk;sxkA 55
Array esa Inserted Value 44 ls cM+k vkSj 66 ls NksVk gS vr% bls 44 vkSj 66 ds chp esa Insert
dj fn;k tk;sxkA

11 22 33 44 66 77 88 55

11 22 33 44 55 66 77 88
Coding Talks

Algorithm of Insertion Sort


Algorithm - 1. Set K = 1
2. For k = 1 to (n-1)
3. Set temp=a (k)
4. Set j = (k-1)
While temp<1 (j) and (j>=0) perform the
following steps; set a(j+1) =a (j)
[End of loop structure]
Assign the value of temp to a (j+1)
[End of for loop structure]
5. Exit

Selection Sort: - ;s Technique Minimum Maximum Element ij fuHkZj djrh gSaA blds fy,
lcls igys Minimum Value dks Select fd;k tk;sxkA vkSj mls Array dh First Position ij j[k
fn;k tk;sxkA mlds ckn 2nd Minimu Element dks find djsaxs vkSj mlls 2nd Position ls
Exchange dj fn;k tk,xk
Pass1 :Find the location LOC of the smallest in the list of elements
A[1], A[2],....,A[N], and then interchange A[LOC] and A[1]. Then:
Pass2 :Find the location LOC of the smallest in the sublist of N – 1 elements
A[2], A[3],..., A[N], and then interchange A[LOC] and A[2]. Then:
A[1], A[2] is sorted, since A[1] < A[2].
Pass3 :Find the location LOC of the smallest in the sublist of N – 2 elements
A[3], A[4],..., A[N], and then interchange A[LOC] and A[3]. Then:
A[1], A[2],...., A[3] is sorted, since A[2] < A[3].
... ..........................................................................................................................................
... ..........................................................................................................................................
Pass N – 1 Find the location LOC of the smaller of the elements A[N-1], A[N], and
then interchange A[LOC] and A[N-1]. Then:
Thus A is sorted after N – 1 passes.

Example: - Selection Sort dks le>us ds fy, fuEu Method dks Use esa ysrs gSaA
fuEu Array fn;k x;k gSaA
77 33 44 11 88 22 66 55
bl Array dks sort djus ds fy, fuEu Steps dks Follow djsaxsA
1. lcls igys Array ds Minimum Element dks Findout djsaxsA
2. Array dk Minimum Element 11 gSaA bls Array ds 0 Index ls Replace dj fn;k
tk,xkA
77 33 44 11 88 22 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 33 44 77 88 22 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
Coding Talks

3. vc a[1] ls a[7] rd esa ls Minimum Element dks Find Out djsaxsA Minimum
Element 22 gSa mls a[1] ls Replace dj fn;k tk,xkA
11 33 44 77 88 22 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 22 44 77 88 33 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]
4. vc a[2] ls a[7] rd ds Minimum Element dks Find Out djsaxs Minimum Element
33 gSaA bls a[3] ls Replace dj fn;k tk;sxkA
11 22 44 77 88 33 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 22 33 77 88 44 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

5. vc a[3] ls a[7] rd ds Minimum Element dks Find Out djsaxs Minimum Element
44 gSaA bls a[3] ls Replace dj fn;k tk;sxkA
11 22 33 77 88 44 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 22 33 44 88 77 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

6. vc a[4] ls a[7] rd ds Minimum Element dks Find Out djsaxs Minimum Element
55 gSaA bls a[4] ls Replace dj fn;k tk;sxkA
11 22 33 44 88 77 66 55
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 22 33 44 55 77 66 88
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

7. vc a[5] ls a[7] rd ds Minimum Element dks Find Out djsaxs Minimum Element
66 gSaA bls a[5] ls Replace dj fn;k tk;sxkA
11 22 33 44 55 77 66 88
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

11 22 33 44 55 66 77 88
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

8. vc a[6] ls a[7] rd ds Minimum Element dks Find Out djsaxs Minimum Element
77 gSaA ;g viuh LFkku ij gh gSaA vr% bls Sort djus dh vko';drk ugha gSaA
Coding Talks

11 22 33 44 55 66 77 88
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7]

Algorithm of Selection Sort: - ;g ,YxksfjFke vklkuh ls izkjEHk dh tk ldrh gSaA


tSls & 1. Repeat steps 2 and 3 for k=1, 2 ..................... n-1:
2. Call min(a,k,n,loc).
3. [Interchange a[k])and a(loc)]
Set temp : a (k) a (k) : = a (loc) and a (loc) : = temp.
[End of step 1 loop]
4. Exit.

Heap Sort
Heap ,d Binart Tree gS] ftldh Parent Node ges'kk Child Node ls cM+h gksxhA Heap Sorting
esa ,d nh gqbZ List dks Heap esa cnyrs gq, bl izdkj ls Arrange djrs gSa fd og List Lor% gh
Sort gks tk,A
Heap Sort dh rduhd esa ge Element dks ,d&,d djds tree esa Insert dj nsrs gSa vkSj ;fn
Insert fd;k x;k Element, Parent Node ls cM+k gS rks ml Node dks Parent Node ds lkFk
swap dj nsrs gSaA bl fof/k dks vPNh rjg le>us ds fy, ge ,d mnkgj.kdh lgk;rk ysaxsA uhps ,d
Unsorted List ds Elements dks n'kkZ;k x;k gS &
44 30 50 22 60 55 77
Heap Sorting ds fy, tree cukrs gq, Node dks preorder ds :i esa Insert djrs gSa vFkkZr~ igyh
Node dks root ij] nwljh Node dks Left esa] rhljh Node dks Right esa rFkk blh Øe esa vkxs rd
Nodes dks Tree esa Insert djrs gSaA
Step 1 - 44 dks ge Tree esa Insert djsaxs] pw¡fd Tree vHkh rd gh ugha cuk gS blhfy;s 44 gh
parent Node cu tk,xhA

44
vr% vc List –44A

Step 2 - vc gesas 30 dks Heap Tree esa Insert djuk gS pw¡fd ge ;g igys gh crk pqds gSa
Heap Tree esa Insertion djrs gq, igys Root, fQj Left esa Node dks Insert djrs
gSaA blhfy;s ge 30, 44 ds Left esa MkysaxsA
44

30
Coding Talks

vr% vc List – 44, 30A

Step 3 - vc gesa 50 dks Heap Tree esa Insert djuk gS] Insertion ds fu;e ds vuqlkj root
vFkkZr~ 44 ds right esa Insert djsaxsA

44

30 50

;gk¡ ij geus ns[kk fd mijksDr Tree ds Child Node dk eku Parent Node ds eku
ls cM+k gS blhfy, ge bu nksuksa dh Swapping dj nsaxsA

44 50

30 50 30 44

vr% vc List – 50, 30, 44 A

Step 4 - vc gesa izkIr Tree esa 22 dks Insert djuk gSA pwafd Root (50) ds nksuksa vksj (left
,oa right esa) Data Save gS blhfy, ge Root ds Left child dks ,d sub-tree dk
root ekudj blds Left esa 22 dks Insert djsaxsA

50

30 44

22

Step 5 - ;gka ij gesa 60 dks Insert djuk gS] pwafd ge ;gka Root ds Left Tree esa Insertion
dj jgs gSa blhfy, 60 dks 30 ds Right esa Save djsaxsA

50

30 44

22 60
Coding Talks

;gka ij geus ns[kk child node dk eku parent Node ds eku ls cM+k gks x;k gS
blhfy, ge bu nksuksa dh Swapping dj nsaxsA

50 50

30 44 30 44

22 60 22 30
nksckjk geus ;gka ns[kk fd child node (60) dk eku Parent Node (50) ls cM+k gS
blhfy;s ge bu nksuksa dh Swapping dj nsaxsA

50 60

60 44 50 44

22 30 22 30
vr% vc List – 60, 50, 44, 22, 30 A
Step 6 - vc gesa 55 dks Insert djuk gS] pwafd ge Root ds Left sub-tree ds nksuksa vksj
Insertion dk pqds gSa blhfy, vc ge Root ds Right sub-tree ds Left esa Node
dks Insert djsaxsA 60

50 44

22 30 55

;gka ij geus ns[kk child node dk eku Parent Node ds eku ls cM+k gks x;k gS
blhfy, ge bu nksuksa dh Swapping dj nsaxsA

77 77

50 44 50 55

22 30 55 22 30 44
Coding Talks

vr% vc List – 60, 50, 55, 22, 30, 44A


Step 7 - vc gesa 77 dks Insert djuk gS] pwafd ge root ds right sub-tree ds Left esa
Insertion dj pqds gSa blhfy, vc ge root ds Right sub-tree ds right esa Node
dks Insert djsaxsA
60

50 55

22 30 44 77

;gka ij geus ns[kk child node dk eku Parent Node ds eku ls cM+k gks x;k gS
blhfy, ge bu nksuksa dh Swapping dj nsaxsA
60 60

50 55 50 77

22 30 44 77 22 30 44 55
nksckjk geus ;gka ns[kk fd Child Node (77) dk eku Parent Node (60) ls cM+k gS
blhfy;s ge bu nksuksa dh Swapping dj nsaxsA
60 77

50 77 50 60

22 30 44 55 22 30 44 55
vr% vc List – 77, 50, 60, 22, 30, 44, 55A
vc] pwafd List dks Increasing/Accending Order esa sort djuk gS] vr% gesa List ds igys
Element (77) vkSj vfUre Element (55) dh Swapping djuh gksxhA
vr% vc List – 55, 50, 60, 22, 30, 44, 77A
vc 77 Sort gks pqdk gS] List ds vU; element dks Sort djus ds fy, List ds bl Sorted Element
dks NksM+dj] vU; Unsorted Elements ij mijksDr izfØ;k iqu% nksgjkbZ tkrh gSaA
Step 1- set in R, root, par, son, temp (R, root, par, son o temp dks initialize
djsaA)
Coding Talks

Step 2- Reapeat for k = n, k<=1 (k dk eku n lqjf{kr djsa vkSj step 3 ls ste 15 rd
nksgjk,a tc rd k dk eku 1 ds cjkcj ;k cM+k gksA)
Step 3- call wapt with &A[1], &A[k]
(A[1] o A[k] dks Swap djus ds fy, function dks dkWy djsaA)
Step 4- set par = 1 (part dk eku 1 lqjf{kr djsaA)
Step 5- set son = par x 2 (Son Variable esa par dk nqxquk eku lqjf{kr djsaA)
Step 6- set roo = A[1] (root variable esa A[1] vFkkZr~ array dk igyk
element lqjf{kr djsaA]
Step 7- check whether (son + 1) < l (tkaps fd D;k (son + 1) dk eku K ls NksVk gSA)
if yes then (;fn gka rks
check whether A[son+1] > A[son] (tkaps fd D;k A[son+1] dk eku A]son] ls
cM+k gSA)
if yes then
son++ (;fn gka rks son esa 1 Increment djsaA)
Step 8- Repeat while [son < = (k-1) and A[son] > root]
(step 9 ls step 15 rd nksgjk,a tc rd son dk eku k-1 ls NksVk ;k cjkcj gS vkSj
A[son] dk eku root ls cM+ gSA
Step 9- set A[par] = A[son] (A[par] esa A[son] dk eku lqjf{kr djsaA)
Step 10- set par = son (par esa son dk eku lqjf{kr djsaA)
Step 11- set son = par x 2 (son esa par ds nqxqus eku dks lqjf{kr djsaA)
Step 12- Check whether (son+1)<k (tkaps fd D;k (son+1) k ls NksVk gSA)
if yes then (;fn gka rks
Perform Step 13 Step 13 dk vuqlj.k djsa
else vU;Fkk
Perform step 14 Step 14 dk vuqlj.k djsaA)
Step 13- check whether A[son+1] > A[son]
(tkaps fd D;k A[son+1] dk eku A[son] ls cM+k gSA)
if yes then
son ++ (;fn gka rks son esa ,d dk increment djsaA)
Step 14- check whether son > n (tkaps fd D;k son dk eku n ls cM+k gSA)
if yes then
set son = n (;fn gka rks son esa n lqjf{kr djsaA)
[End of If condition]
Step 15- set A[par] = root (A[par] esa root dk eku lqjf{kr djsaA)
[End of loop of step 8]
[end of loop of step 2]
Step 16- Exit

Bubbles Sort
Algorithm – ekuk fd n Elements dk ,d Linear Array gS] temp Element dh Interchanging
ds fy;s vLFkk;h Variable gS &
1. Input n elements of an array a.
2. Initialize i=0
3. Repeat through step 6 while (i<n)
4. Set i=0
5. Repeat through step 6 while (i<n-i-1)
6. If (a[i]>a(j+1)
(i) temp = a (j).
(ii) a [j] = a (j+1).
Coding Talks

(iii) a (j+1) = temp.


7. Display the sorting elements of array a.
8. Exit.
For Example:- The following array is given:-
bl izdkj ds Array esa adjacent number dks compare fd;k tkrk gSA
;fn a[i]>a[i+1]=exchange
if a[i]<a[i+1]=no change
11 15 2 13 6
a[0] a[1] a[2] a[3] a[4]

blesa vyx vyx passes dks perform fd;k tkrk gSA tc rd array ds last element rd uk igqWap
tk,A
PASS 1:
1.fn, x, Array a[0] dks a[1] ls compare djok,saxsaA
if a[0]>a[1]=exchange and if a[0]<a[1]=no change
11,15 ls NksVk gSAvr% no change

11 15 2 13 6
a[0]<a[1]=no change

2. fn, x, Array a[1] dks a[2] ls compare djok,saxsaA


if a[1]>a[2]=exchange and if a[1]<a[2]=no change
15,2 ls cM]k gSAvr% exchange

11 15 2 13 6
a[1]>a[2]=exchange

11 2 15 13 6
3. fn, x, Array a[2] dks a[3] ls compare djok,saxsaA
if a[2]>a[3]=exchange and if a[2]<a[3]=no change
15,13 ls cM]k gSAvr% exchange
11 2 15 13 6
a[2]>a[3]=exchange

11 2 13 15 6
4. fn, x, Array a[3] dks a[4] ls compare djok,saxsaA
if a[3]>a[3]=exchange and if a[3]<a[4]=no change
15,6 ls cM]k gSAvr% exchange
11 2 13 15 6
a[3]>a[4]=exchange

11 2 13 6 15
PASS 2:
Now the array is:
11 2 13 6 15
a[0] a[1] a[2] a[3] a[4]
1.fn, x, Array a[0] dks a[1] ls compare djok,saxsaA
if a[0]>a[1]=exchange and if a[0]<a[1]=no change
11,2 ls cM]k gSAvr% exchange
Coding Talks

11 2 13 6 15
a[0]>a[1]=exchange

2 11 13 6 15
2. fn, x, Array a[1] dks a[2] ls compare djok,saxsaA
if a[1]>a[2]=exchange and if a[1]<a[2]=no change
11,13 ls NksVk gSAvr% no change

2 11 13 6 15
a[1]<a[2]= no change

3. fn, x, Array a[2] dks a[3] ls compare djok,saxsaA


if a[2]>a[3]=exchange and if a[2]<a[3]=no change
13,6 ls cM]k gSAvr% exchange
2 11 13 6 15
a[2]>a[3]=exchange

2 11 6 13 15
4. fn, x, Array a[3] dks a[4] ls compare djok,saxsaA
if a[3]>a[4]=exchange and if a[3]<a[4]=no change
13,15 ls NksVk gSAvr% no change
2 11 6 13 15
a[3]<a[4]=no change
PASS 3:-
Now the array is:
2 11 6 13 15
a[0] a[1] a[2] a[3] a[4]
1.fn, x, Array a[0] dks a[1] ls compare djok,saxsaA
if a[0]>a[1]=exchange and if a[0]<a[1]=no change
2,11 ls NksVk gSAvr% no change

2 11 6 13 15
a[0]<a[1]=no change

2. fn, x, Array a[1] dks a[2] ls compare djok,saxsaA


if a[1]>a[2]=exchange and if a[1]<a[2]=no change
11,6 ls cM]k gSAvr% exchange

2 11 6 13 15
a[1]>a[2]= exchange

2 6 11 13 15
3. fn, x, Array a[2] dks a[3] ls compare djok,saxsaA
if a[2]>a[3]=exchange and if a[2]<a[3]=no change
11,13 ls NksVk gSAvr% no change
2 6 11 13 15
a[2]<a[3]=no change

4. fn, x, Array a[3] dks a[4] ls compare djok,saxsaA


if a[3]>a[4]=exchange and if a[3]<a[4]=no change
Coding Talks

13,15 ls NksVk gSAvr% no change


2 6 11 13 15
a[3]<a[4]=no change

Now the Array is sorted.

QUICK SORT
(1) fn, x, Array dh left boundary ij left pointer rFkk right boundary ij right
pointer assign djsaxsaA
(2) Array ds 1st element dks pivot ekusaxsa vkSj bls bldh lgh position ij igqWpk,saxsaA vr%
Pivot ds left esa Pivot ls NksVs Element vkSj Pivot ds right esa Pivot ls cMssa element
(3) blds fy, lcls igys Pivot vkSj left pointer nksuks ,d gh element dks iksabV djsaxs vc
left dk comparision right pointer ls djsaxs
If ( pivot or left is less than right then no change)
if (pivot or left is greater than right then exchange the element with pivot)
(4) ;fn Pivot Right esa igqWap x;k gS rks left dks increase djsaxsa
if pivot or right is less then left then exchange
if pivot or right is greater then left then no change

for example :-
25 10 30 15 20 28
(1) fn, x;sa array esa 25 dks left vkSj pivot nksuksa point djsaxsa rFkk right pointer
array ds last element dks point djsaxk vc pivot dks right lsa compare djsaxs ;fn pivot
right ls NksVk gS rks no change ughs rks element dks exchange dj fn;k tk;sxk
25 10 30 15 20 28
Pivot Pivot or left < right = no change right
left

(2) right pointer dks vkxs c<k;saxs vkSj mls pivot ls compare djsaxs
If ( pivot or left is less than right then no change)
if (pivot or left is greater than right then exchange the element with pivot)
25 10 30 15 20 28
Pivot right
left

20 10 30 15 25 28
left Pivot
right

(3) D;ksafd pivot right side esa vk pqdk gS vr% pivot ds left esa pivot lsa NksVh values vkuh
pkfg, vr% vc left pointer dks vkxs c<k;sasxs A
20 10 30 15 25 28
Pivot or right > left = Nochange Pivot
left right
(4) D;ksafd pivot right side esa vk pqdk gS vr% pivot ds left esa pivot lsa NksVh values vkuh
pkfg, vr% vc left pointer dks vkxs c<k;sasxs A
Coding Talks

20 10 30 15 25 28
left Pivot
Pivot or right < left = Exchange right

20 10 25 15 30 28
(5) D;ksafd pivot left side esa vk pqdk gS vr% pivot ds right esa pivot lsa cMh values vkuh pkfg,
vr% vc right pointer dks vkxs c<k;sasxs A
20 10 25 15 30 28

Pivot right
left
Pivot or left >right = Exchange

20 10 15 25 30 28
(6) vr% vc pivot viuh lgh position ij vk pqdk gS A pivot ds left esa lkjh pivot ls NksVh
values gS vkSj pivot ds right esa pivot ls cMh values gS Avr% vc array dks rhu parts esa divide
dj nsaxs A first array esa pivot ls NksVh values dks j[ksaxs second array esa pivot dks j[ksaxs rFkk
third array es pivot ls cMh values dks j[kk tk;sxk A vc first vkSj third array dh vyx vyx
sorting djsaxs vkSj mls ckn esa pivot ds lkFk add dj fn;k tk;sxk A
(7) pivot ls NksVs elements ds array dks sort djsaxs A Pivot ls NksVs elements dk array fuEu
gS %&
20 10 15

Pivot right
left
(8) mijksDr array ess 20 dks pivot ekuk gS vc Pivot vkSj Rght Pointer dks Compare djsaxsA
;fn Right Pointer Pivot ls NksVk gS rks Right okys Element dks Pivot ls Exchange dj
fn;k tk;sxkA

20 10 15

Pivot right
left
Pivot, right Pointer ls cM+k gS vr% bls Exchange djsaxsA
15 10 20
↑ ↑
Left Pivot
Right
(9) bl izdkj Pivot Right esa vk pqdk gSaA vkSj Right ds Left okyh lHkh values right ls NksVh
gksuh pkfg, vr% Left dks vkxs c<+k,xsaA
15 10 20
↑ ↑
Left Pivot
Right
Coding Talks

Left Pointer okyk Element Pivot ls NksVk gSaA vr% No Exchange vc bls Pivot okys
Array esa Add dj nsaxsA D;ksafd tc Left dks vkxs c<+k,xsa rks Left Pivot vkSj right rhuksa ,d gh
element dks Point djsaxsA vr% Array Sorted gSaA
15 10 20 25

(10) vc Pivot ds right side okys Array dks sort djsaxs


30 28
(11) 30 dks Pivot ekusaxs vkSj Left Pointer Hkh 30 dks Point djsxkA Right Pointer 28 dks
Point djsxkA

30 28
↑ ↑
Pivot Right
Left
vc Pivot dks right Pointer ls Compare djsaxsA ;fn Pivot right ls cM+k gS rks mls exchange
dj fn;k tk,xkA
28 30
↑ ↑
Left Pivot
Right
D;ksafd Pivot Right esa vk pqdk gSa vr% Pivot ds Left esa Pivot ls NksVh Values gksuh pkfg, vr%
Left dks vkxs c<+k,xsaA
28 30

Left
Right
Pivot
vr% Array Sorted gSa vc bls Pivot ds lkFk Pivot ds right esa add dj nsaxsA
15 10 20 25 28 30
(12) bl izdkj quick sort ds }kjk Array dks sort fd;k tkrk gSa
Algorithm For Quick Sort
Step 1 : [Initially]
Left = l
Right = R
Pivot = a[(l+r)/2]
Step 2 : Repeat through step 7 while (left <= high)
Step 3 : Repeat step 4 while (Left<=Right)
Step 4 : Left = Left + 1
Step 5 : Repeat step 6 while (a {[Right] < pivot})
Step 6 : Right = Right – l
Step 7 : If (left < = Right)
(i) Temp = a (left)
(ii) a(left) = (Right)
(iii) a (Right) = temp
(iv) left = left +1
(v) Right = Right + 1
Step 8 : If (l, right) quick_sort (a, l , right)
Step 9 : If (left<R) quick_sort (a, left, r)
Step 10: Exit
Searching
Coding Talks

Searching dk vFkZ gS <wa<ukA Searching }kjk fdlh Hkh List esa lqjf{kr fdlh element dks <wa<k tk
ldrk gSaA Searching fdlh Hkh List vFkkZr+ dqN Elements dk leqg gSa tks Link ds }kjk tqM+s jgrs
gSa esa ls fdlh Element dh Location dks <wa<us ds fy, dh tkrh gSaA Searching dk Successful ;k
Unsuccessful gksuk iw.kZr% Element ds feyus ;k u feyus ij fuHkZj djrk gSa vFkkZr~ ;fn element
list esa fey tkrk gSa rks Searching successful dgykrh gSa vkSj ;fn element list esa ugha feyrk gSa
rks Searching Unsuccessful dgykrh gSa Searching dh algorithm list esa stored elements ds
Data Structure ds type ij fuHkZj djrh gSaA vr% ;fn ,d array esa searching djrs gSa rks mldh
algorithm vyx gksxh vkSj ;fn ,d List esa Searching djrs gSa rks mldh algorithm vyx gksxhA
Fast Searching ds fy, nks Searching methods dks Use esa fy;k tkrk gSaA
(1) Linear ;k Sequential Search (2) Binary Search

(1) Linear ;k Sequential Search: - ;g lcls Easiest Searching Method gSa Sequential
Search dh rqyuk single key ds mij dh tkrh gSaA Linear Search esa ge Øe ls ,d&,d djds
Array ds gj Element rd igq¡prs gSa vkSj ns[krs gSa fd og element fn;k x;k element gSa ;k ugha
;fn element fey tkrk gSa rks og Successful Search dgyk,xh vkSj ;fn Element ugha feyrk gSa
rks og Unsuccessful Search dgyk,xhA ;g ,d ,slh rduhd gSa tks fn, x, Item dks Locate
djus ds fy, Squentially Array dks Traverse djrk gSaA
Effeciency of Sequential Search: - blesa fn;k x;k le; vFkok comparision ds No.
Searching esa Record cukrs gSa tks Technique dh Capacity ij vk/kkfjr gksrk gSa ;fn fn;k x;k
element first position ij gSa rks dsoy ,d Search Table dk Comparision cusxkA ;fn fn;k x;k
Element Last esa gS rks N Comparisions gksaxs vkSj ;fn element array ds chp esa dgha ij gSa rks
(N+1)/2 comparision gksaxsA blds fy, fuEu algorithm gSa
Algorithm - ;g Algorithm a esa Item ds Loc, Location dks izkIr djrk gSa] ;k Sets loc = 0 ;fn
Search vlQy gSaA
1. [insert item at the end of data] set data [n+1] = item
2. [initialize counter] set loc = 1.
3. [search for item]
Repeat white data [loc] = item;
set loc = loc+1.
4. [successfull] if loc = n+1, then set loc = 0
5. Exit.
Binary Search: - ;g Data Structure Sorted Array vkSj Link List nksuksa dk Combination gSa
bl Search ds dqN Important Points fuEu gSaA
(1) Bianry Search Recursive gSaA ;g fu/kkZfjr djrk gSa fd Search Key Array ds uhps ;k
mij okys vk/ks Hkkx ij fLFkr gSa ;k ugha
(2) ;gka ,d Termination Condition gSa
(i) ;fn Low > high rc partition ;g Search djrk gSa fd dksbZ element blesa
ugha gSaA
(ii) ;fn ;gka ij Current Partition ds e/; esa Element ds lkFk ,d Match gSa rks
Easily Element ls okil tk ldrs gSaA
(3) Array dks rc rd Search djuk tc rd fd lgh Point }kjk Insert djok;k x;k u;k
Item izkIr u gks tk,A
(4) lHkh Items dks ,d LFkku ij Move djuk
(5) u;s Item dks [kkyh LFkku esa Insert djukA
(6) Binary Search dks Static ?kksf"kr fd;k tkrk gSa ;g ,d Local Function gSaA tks lHkh
Programs ds }kjk Access fd;k tk ldrk gSaA
(7) fn;s x, Element dks Array esa Search djus ds fy, fuEu Steps dks Follow djsaxsA
(i) Beg Array ds first element dks Point djsxkA
(ii) End Array ds Last Element dks Point djsxkA
Coding Talks

(iii) vc Begning vkSj end okys Index ls Middle Value dks Find out fd;k
tk,xkA
(iv) vc Check djsaxs fd ftl Item dks Search djuk gSa og Middle Value ls c<+k
gSa ;k NksVkA
(v) ;fn fn;k x;k Item Middle Value ls NksVk gSa rks mls Middle ls igys okys
Array esa Search djsaxsA
(vi) ;fn fn;k x;k Item Middle Value ls cM+k gSa rks mls Middle ls ckn okys
Array esa Search djsaxsA
bl izdkj gj ckj Begning vkSj End dks Set fd;k tk,xk vkSj Specific element dks Find out
djsaxsA
Example: - ,d Sorted Array fn;k x;k gSa ftlesa ls 15 Element dks Search djuk gSa
Array = 3, 10, 15, 20, 35, 40, 60
Solution: -
3 10 15 20 35 40 60
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
↑ ↑
Beg End
mid = (beg + end)/2
(0 + 6)/2 = 3
(1) vr% mid a[3] Element dks point djsxkA vc ;g Check djsaxs fd a[3] ij tks element gSa
og 15 ls NksVk gS ;k cM+k
(i) ;fn a[3] ij tks Element gS og 15 ls NksVk gS rks 15 ds right okys array esa
search djsaxsA
(ii) ;fn a[3] ij tks element gS og 15 ls cM+k gS rks 15 ds Left okys Array esa
Search djasxsA
(2) a[3] ij 20 gS tks 15 ls cM+k gS vr% 15 dks mlds left esa search fd;k tk,xk blds fy,
end esa mid – 1 dks Assign djsaxsA vkSj iqu% Begning vkSj End ds Middle Value dks
find djsaxsA
3 10 15 20 35 40 60
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
↑ ↑ ↑
Beg End mid
mid = (beg + end)/2
(0 + 2)/2 = 1
(3) a[1] ij 10 gS tks 15 ls NksVk gS vr% 15 dks mlds Right esa search fd;k tk,xk blds fy,
Beg esa mid + 1 dks Assign djsaxsA vkSj iqu% Begning vkSj End ds Middle Value dks
find Out djsaxsA
3 10 15 20 35 40 60
a[0] a[1] a[2] a[3] a[4] a[5] a[6]
↑ ↑
Beg End
mid = (beg + end)/2
(1 + 2)/2 = 2
(4) Begning vkSj end nksuks a[2] dks point djsaxs vr% vc check djsaxs fd 15 a[2] ij rks ugha
gSa array ns[kus ls irk pyrk gSa fd a[2] position ij 15 gSA bl izdkj Binary Search
Perform fd;k tk,xkA
Algorithm for Binary Search
Begin
set beg = 0
Coding Talks

set end = n – 1
set mid = (beg+end)/2
while ( (beg < end) and (a[mid] # item ) ) do
if (item < a [mid]) then
set end = mid-1
else
set beg = mid + 1
endif
set mid = (beg + end) / 2
endwhile
if ( beg > end) then
set loc = -1
else
set loc = mid
endif
End.
Coding Talks

UNIT - V
Hash Table:- Memory esa dqN Data dks Store djus ds fy, cgqr ls Program dh vko';drk
gksrh gSaA Data Structure dks dbZ izdkj ds Operations Perform djus ds fy, cuk;k x;k gSA tSls
Array, Linked List, Binary Search Tree vkfn ds vUrZxr fdlh Hkh Element dks vklkuh ls
Add ;k Delete fd;k tkrk gSaA ,d Hash Table mu Target dks Include dj ldrs gSa ftuds ikl
Key vkSj Value gS rFkk tks Objects dks Hkh Include djrs gSaA blesa Hash Table dk ,d Array
cukk; tkrk gSa tks fd Hashing Function gksrk gSaA Hash Function ,d Argument ds :i esa
Key ysrk gSa vkSj Table ds Array dh Range esa dqN Index dh Calculation djrk gSaA
tc ge Hash Table esa ,d element dks Include djuk pkgrs gSa rks lcls igys bldh Hash
Value dh Calculation dh tkrh gSaA ge Array esa Location dks rc rd Findout djrs gSa tc rd
bldk index hash value ds cjkcj ugha gks tkrk vkSj ogka Item Insert dj fn;k tkrk gSaA ;fn
Location Table esa igys ls gh use esa vk jgh gSa rks ikl okyh Free Location dks Check djrs gSaA
bl izdkj dh hashing simple hashing dgykrh gSaA
tc Array esa ,d Element dks iznf'kZr djuk pkgrs gSaA rc lcls igys Hash value dh x.kuk dh
tkrh gS vkSj rc Location ij Table dh Checking Start gksrh gSa ftudh Index bl hash value
ds cjkcj gksrh gSA
;fn Hash Table ls element dks Delete djuk pkgrs gSa rks lcls igys ml Element dh Location
dks Find out fd;k tkrk gSaA tc Element fey tkrk gSa rks ml ij Mark yxk fn;k tkrk gSa fd
Location Free gksA
;fn dksbZ Element "C" dks Findout djus dh dks'kh'k djrs gSa rks lcls igys bldh Hash Value
dh Calculation dh tkrh gSaA fQj bl Location esa tkdj ns[krs gSa ;g Location 2 gSA tc C fey
tkrk gSa rks bldh Hash Value dh calculation djrs gSaA ;fn fdlh Element dks Hash Table ls
gVk;k x;k gks rks mlds LFkku ij ,d Mark yxk nsrs gSaA tSls fuEu fp= esa B dks gVk;k x;k gS rks
mls fuEu izdkj iznf'kZr fd;k tkrk gSaA
a * c
Hash Table ij Operations dks Find rFkk View fd;k tk ldrk gSaA ;g nks Factor dk
Function gSA
Hash Table dh complexity (okLro esa bl Cell esa fdrus Percent Element dks Include fd;k
x;k gSaA) Example: - Hash Function ges'kk fdlh Hkh Element ds fy, Return gksxk rks ;g ns[kk
tkrk gSa fd ,d Link Lised dh rjg Hash Table ds dk;Z ls feyk gS tks Operation dks <+w<us esa
T;knk Important ugha gSaA Key dks Use esa ysus dk General Item ;g gSa fd fdlh Hkh Record ds
Address dks Store dj fy;k tk;sA ysfdu blh Modify fd;k tkuk pkfg;s rkfd T;knk Memory
Waste u gks bl Modification ds fy;s ,d Function H dks K Set of Keys ij Define fd;k
tkrk gSaA ;g Function fuEu izdkj gSaA
H:K→L
bl Function dks Hash Function dgk tkrk gSa gks ldrk gSa fd nks Keys K1 rFkk K2 nksuks Same
Hash Address dks Point djrh gSaA bl izdkj dh Condition dks Collision dgykrh gSa vkSj bl
Collision dks nwj djus ds fy, dqN Methods dks Use fy;k tkrk gSaA Hashing dks 2 Parts esa
Divide djrs gSaA
(1) Hash Function
(2) Collision Resolution

(1) Hash Function: - Hash Function dks Select djus ds fy, 2 principles dks Use esa fy;k
tkrk gSaA ,d Function H cgqr Easy rFkk Quick Computation dks Perform djrk gSaA
Hash Function Hash Addresses dks Uniformly distribute djrk gSaA ftlls de ls de
Collision Occur gksrs gSaA
(2) Collision Resolution: - ekuk fd ge ,d u;s Record R dks Key K ds lkFk File F esa
Add djuk pkgrs gSa ysfdu Memory Location tgka ij u;s Record dks Add djuk gS og
Coding Talks

Full gSa rks bl izdkj dh condition Collision dgykrh gSaA Collision dks nwj djus ds fy, dqN
Techniques dks Use esa fy;k tkrk gSaA
Normally Collision dks Avoid djuk Impossible gS tSls ,d Class esa 24 Studnets gSa vkSj
,d Table esa 365 Records dk Space gSaA ,d Random Hash Function ds }kjk Student ds
Birthday dks Choose fd;k tkrk gSaA

Hash Function dh Efficiency dks Collision ds Solution ds Procedure ds vk/kkj ij Major


fd;k tkrk gSaA blds fy, Key Comparision dh vko';drk gksrh gSaA ftlls Specific Record dh
Location Find fd;k tkrk gSaA Efficiency Load Factor λ ij fuHkZj djrh gSaA Collision
Resolution ds fy, 2 Procedure dks use esa fy;k tkrk gSaA

(1) Open Addressing: - ekuk ,d u;s Record R dks Key K ds lkFk Memory Table T esa
Add djuk gS ysfdu Memory Address ;k Memory Location With Hash Address igys ls
Hkjh gqbZ gSaA bl izdkj ds Collision dks nwj djus ds fy, R dks First Available Location esa
Assign dj fn;k tk;sxkA blds fy, lcls igys Record R dks Search fd;k tk;sxk blds fy,
Linear Search dks Use esa ysrs gSaA bl izdkj ds collision resolution dks linear probing dgk
tkrk gSaA
Example: - ekuk ,d Table T esa 11 Memory Locations gSaA T[1].... T[11] rFkk File F esa 8
Records gSaA A, B, C, D, E, X, Y rFkk Z ftlds Hash Address fuEu gSaA

Record : A, B, C, D, E, X, Y, Z
H(k) : 4, 8, 2, 11, 4, 11, 5, 1

ekuk 8 Records dks Table T esa mijksDr Order ds vk/kkj ij Enter dj fn;k x;k gSaA vc File F
dks meory esa fuEu izdkj ns[kk tk ldrk gSaA

Table T: X, C, Z, A, E, Y, __, B, _, _, D
Address: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Linear Probing dk Main Disadvantage ;g gSa fd Records dk Clusterization gks tkrk gSaA
tc Load Factor > 50% gks Clustering fdlh Hkh Record ds Average Search Time dks
Increase dj nsrh gSaA

(2) Chaining: - Chaining Memory esa 2 Tables dks Maitain djrh gSaA ,d Table T
Memory esa F Records dks Contain djrh gSaA T ,d Additional Field dks Maintain djrk gSa
ftlesa Links dks j[kk tkrk gSa lHkh records tks Table T esa gSa Same Hash Address H ds }kjk
,d nwljs ls Linked jgrs gSaA For Example: - ,d Data Table T esa 8 Records gS ftudk Hash
Address fuEu Records gSaA

Record : A, B, C, D, E, X, Y, Z
H(k) : 4, 8, 2, 11, 4, 11, 5, 1

Chaining dks Use esa ysdj Records dks Memory esa fuEu izdkj ls Represent fd;k tk ldrk
gSaA

1 8 1 A 0
2 3 2 B 0
3 0 3
4 5 4 C 0
5 7 5 D 0
6 0 6 E 1
7 0 7
8 2 8 X 4
9 0 9
10 0 10
11 6 11
Coding Talks

GRAPHS
Graph Hkh ,d Non-Linear Data Structure gSA bl v/;k; esa ge Graph ds lkFk fd, tk ldus
okys lHkh Operations ds ckjs esa tkudkjh izkIr djsaxsA
,d G Graph Vertices (Nodes) ds Set V vkSj Edges ds Set E, ls fufeZr gksrk gSA bls ge bl
izdkj Hkh dg ldrs gSa fd Vertices (Nodes) dk Set V vkSj edges dk Set E feydj ,d Graph
cukrs gSaA ge Graph = (V,E) fy[krs gSaA V, Nodes dk ,d lhfer vkSj Hkjk gqvk Set gS vkSj E,
Nodes ds tksM+ksa dk Set gS] ;s tksM+s gh Edges dgykrs gSaA

Graph dks ifjHkkf"kr djuk


V(G) dks Graph dh Nodes i<+k tkrk gS vkSj E(G) dks Graph dh Edge i<+k tkrki gSA
,d Edge E=(V,W) nks Nodes, V vkSj W dk ,d tksM+k gSA tgka V vkSj W Incident gSA
bu nksauks Sets dks le>us ds fy, fupa fp= esa n'kkZ, Graph dk v/;;u djsaA
bl fp= esa ,d lk/kkj.k ;k fn'kkghu Graph dks n'kkZ;k x;k gS] ftlds Nodes dks Øe'k% A, B, C,
D vkSj E uke fn;k x;k gS blfy, &
A B

C D
V(G) = (A, B, C, D, E)
E(G) = {(A,B), (B,D), (D,C), (C,A), (A,D) (B,E), (D,E)}
vki ns[k ldrs gSa fd ,d Edge A vkSj B dks tksM+rh gS vkSj geus bls (A,B) fy[kk gS] bls (B,A) Hkh
fy[k tk ldrk FkkA ;gh ckr ckdh lHkh Edges ij Hkh ykxw gksrh gSA blhfy, ge dg ldrs gSa fd
pwafd Graph esa Nodes dks Øe ugha fn;k x;k gS] ;g ,d fn'kkghu xzkQ ds fy;s lgh gSA
,d fn'kkghu Graph esa Nodes dks tksM+k ,d fcuk Øe dh Edge dks izLrqr djrk gSA blhfy,
(V,W) vkSj (W,V) nksuksa ,d gh Edge dks izLrqr djrs gSaA
,d fn'kk okys Graph esa izR;sd Edge, Nodes dk ,d Øfed tksM+k gksrk gS] vFkkZr~ izR;sd edge ,d
fn'kk okys tksM+s ls izLrqr gksrh gSA
;fn E = (V,W) rks Edge, V ls izkjEHk gksxh vkSj W ij lekIr gks jgh gSA ;gka V dks Tail vFkok
Initial Vertex rFkk W dks Head vFkok Final Node Vertex dgk tk,xkA vr% (V,W) vkSj (W,V)
nks fHkUu Edges dks iznf'kZr djsaxhA
,d fn'kk okyk vFkok fufnZ"V Graph fuEukafdr fp= esa n'kkZ;k x;k gS &

A B

C D
Coding Talks

mijksDr fp= esa n'kkZ, x, Graph ds fy, &


V (G) = (A,B,C,D,E,)
E (G) = {(A,B), (A,C), (A,D), (C,D), (B,E), (B,D), (D,E)}

vk/kkjHkwr ifjHkkf"kd 'kCnkoyh


(Graph Terminology)
fn'kkghu vkSj fufnZ"V Graph ge igys gh crk pqds gSa] dqN Graphs fuEufyf[kr gSa &
,d Graph G, iw.kZ Graph dgk tk,xk] ;fn Graph dh izR;sd Node Graph dh izR;sd nwljh
Node ls tqM+h gksA ,d iw.kZ Graph ftlesa n Vertices gSa mlesa] n(n-1)/2 Edges gksaxhA ,d iw.kZ
Graph dks fuEukafdr fp= esa n'kkZ;k x;k gS &
A

D E

,d Vªh] ftlesa cycle cuh gks] mls Graph dgrs gSaA bls bl izdkj Hkh dg ldrs gSa fd ;fn ,d tree
dh fdlh Node dks nks izdkj ls accesds fd;k tk ldrk gS] ;g tree ugha cfYd Graph gksrk gSA
,slk gh ,d Graph fuEukafdr fp= esa n'kkZ;k x;k gS &

B E

C D F G
,d tqM+k gqvk Graph ftlesa cycle u gks] mls Tree Graph vFkok Free Tree vFkok lk/kkj.k Tree
dgk tkrk gSA ,slk gh ,d Graph fuEukafdr fp= esa n'kkZ;k x;k gS &
A B

C D
Coding Talks

,d Graph ftldh Edges ij Data fy[kk gksrk gS] Labeled Graph dgykrk gSA ,slk gh ,d
Graph fuEukafdr fp= esa n'kkZ;k x;k gS &
A1
A B
A2
A5 A4

C D
A3
,d Graph ftldh Edges ij /kukRed vad fy[ks gksrs gSa] mUgsa Weighted Graphs dgrs gSaA ,slk
gh ,d Graph fuEukafdr fp= esa n'kkZ;k x;k gS &
2
B C

2 4 2
2 5

4
A D E 6 H

3 3
2 1

F G
5
og Graph ftlds gj Node dh Degree leku gksrh gS] Regular Graph dgykrs gSaA ,slk gh ,d
Graph fuEukafdr fp= esa n'kkZ;k x;k gS &

B C

D E
os Edges ftudk Intial vkSj End Point leku gksrk gS] mUgsa Parallel Edges dgrs gSa rFkk og
Edge ftldk Intial vkSj End Point ,d gh gksrk gS] mls Self Loop dgrs gSaA
,slk Graph, ftlesa u rks Self Loop gks ;k fQj Parallel Edge gks vFkok nksuksa gh gksa,
Multigraph dgykrk gSA
A B

C D
Coding Talks

,slk Graph, ftlesa u rks Self Loop gks vkSj uk gh Parallel Edge gks, Single Graph dgykrk gSA
bls bl izdkj Hkh dgk tk ldrk gS fd dksbZ Graph, tks fd Multigraph ugha gS, Single Graph
dgykrk gSA
,slh Vertex, tks fdlh vU; Vertex ds Contact esa ugha gksrh, og Isolated Vertex dgykrh gSA
,slk Graph ftlesa, Isolated Vertex gksrh gS, mls NULL Graph dgrs gSaA ,slk gh ,d Graph
fuEukafdr fp= esa n'kkZ;k x;k gS &

Adjacent Vertices (Neighbours)


fuEukafdr fp= esa vertex V1, vertex V2 ds adjacent dgyk,xh, ;fn ,d edge (V1, V2) vFkok
(V2, V1) gS &

e1
V1 V2

e3 e2

V3 e4 V4

e5

V5

;gka V1 vkSj V2 adjacent gSa, V1 vkSj V3 adjacent gSa, V2 vkSj V3 adjacent gSa, V3 vkSj V4
adjacent gSa rFkk V4 vkSj V5 adjacent gSa D;ksafd ;s lHkh vertices fdlh u fdlh edge ds }kjk
vkil esa tqM+s gq, gSaA izR;sd Vertex dks mlls lEc) edges ds vk/kkj ij fMxzh (Degree) iznku dh
tkrh gSA ;gk¡ ij V1 Vertex, nks Vertices V2 vkSj V3 ls tqM+k gqvk gS, vr% V1 dh fMxzh 2 gksxhA
blh izdkj Vertex V2 dh fMxzh = 2
Vertex V3 dh fMxzh = 3
Vertex V4 dh fMxzh = 2
Vertex V5 dh fMxzh = 1
og Vertex ftldh fMxzh dsoy 1 gksrh gS, mls Pandent Vertex dgk tkrk gSA
og Vertex ftldh fMxzh 'kwU; (0) gS, mls Isolated Vertex dgk tkrk gSA fiNs fn, x, fp= esa blh
izdkj dh Vertex dks n'kkZ;k x;k gSA

Path: - closed path og Path gS tgka Path ds End Points Same gks
og Path Simple Path dgykrs gSa ;fn lkjs Vertices ,d Sequence esa gks vkSj lHkh vyx&vyx
gksA

Connected Graph: - ,d Graph G Connected dgykrk gSa ;fn 2 Vertices ds chp esa ,d
Path gks

Multigraph: - ,d Graph ftldh Multiple Edges gksrh gSaA mUgsa Multigraph dgk tkrk gSaA
Out Degree & In Degree of vertex: - ,d Vertex ls ftruh Edges ckgj dh rjQ fudyrh gSaA
mUgsa Vertex dh Outdegree dgk tkrk gSaA ,d Vertex ij ftruh Edges vkdj [kRe gksrh gSaA mUgsa
Vertex dh In Degree dgk tkrk gSaA
Coding Talks

In Degree Out Degree

Source & Sink: - ,d Vertex Source dgykrk gSa tc mldh Outdegree Greater than 0 gks
rFkk In degree = 0 gksA
,d Vertex Sink dgykrk gSaA tc ml Vertex dh In Degree Greater than 0 vkSj Out degree
= 0 gksA
Parallel Edges: - vyx&vyx Edges E rFkk E- Parallel Edges dgyk,saxs ;fn os same source
rFkk Terminal Vertices ls Connected gksA
Direct Acyclic Graph: - Directed Acyclic Graph ,d Directed Graph gS ftlesa Cycles ugha
curh gSaA

Incedency
,d Edge ftu Vertices dks tksM+rh gS os Vertices ml Edge dh Incedent gksrh gSA
Edge e1 dh incident – Vertices V1 vkSj V2
Edge e2 dh incident – Vertices V2 vkSj V3
Edge e3 dh incident – Vertices V1 vkSj V3
Edge e4 dh incident – Vertices V3 vkSj V4
Edge e5 dh incident – Vertices V4 vkSj V5
Representation of Graph in Memory
Graph dks Memory esa Representation djus ds fuEufyf[kr rjhds gSa &
(i) Adjacency Matrix
(ii) Incedency Matrix
(iii) Adjacency List Representation
(iv) Multilist Representation
Adjacency Matrix
bl izdkj ds Representation esa vertex dk nwljh vertex ls lEcU/k (Relation) dks ,d Matrix
ds }kjk izLrqr djrs gSaA fuEukafdr fp= esa n'kkZ, x, Graph dk Adjacency Matrix
Representation fp= ds uhps n'kkZ;k x;k gS &
e1
V1 V2

e5 e2

V4 V3
e3
e4

V1 V2 V3 V4
V1 0 1 0 1
V2 1 0 1 0
V3 0 1 0 1
V4 1 0 1 1
Coding Talks

;gka geus mu vertices ds uhps 1 fy[kk gS, tks lkeus fy[kh vertex dh adjacent gSaA 'ks"k ds uhps 0
fy[kk gSA tSls V1, V2 vkSj V4 dh adjacent gS blfy, geus V2 vkSj V4 ds uhps 1 fy[kk gS vkSj
V1 vkSj V3 ds fy;s 0 fy[kk gSA
Indedency Matrix
bl izdkj ds izLrqrhdj.k esa Vertex dk fofHkUu Edges ls Relation dks ,d Matrix ds }kjk izLrqr
djrs gSaA fuEu fp= esa n'kkZ, x, Graph dk Incedency Matrix Representation dks uhps n'kkZ;k
x;k gS &
e1 e2 e3 e4
V1 1 0 0 1
V2 1 1 0 0
V3 0 1 1 0
V4 0 0 1 1

;gka geus mu Vertices dks vkxs 1 fy[kk gS, tks mij nh xbZ Edge ds Incedent gS & tSls V1, e1
vkSj e5 dh Incident gS, blfy, e1 vkSj 35 ds uhps 1 fy[kk x;k gS vkSj e2, e3 vkSj e4 ds uhps 0
fy[kk gSA

Adjacency List Representation


fuEu fp= esa n'kkZ, x, Graph ds fy, ge Adjacency List izLrqrhdj.k djsaxsA blds fy, loZizFke
,d Table cuk,axs, ftlesa izR;sd Node dh Adjacent Node, mlds lkeus fy[kh gks &

A B

C D

A B C D
B C E
C
D C
E C

vc bl Table dks fuEu :i esa List esa ifjofrZr djsaxs &


A B C D

B C E

D C

E C
Coding Talks

;gka ij ftl Node dh adjacent Node crkbZ xbZ gS] mlesa nks Pointer iz;qDr fd, gSa, igyk
Pointer rks vxyh Node dks n'kkZ jgk gS vkSj nwljk Pointer Adjacent Node dksA

Multilist Representation
bl izdkj ds Representation esa ge List ds }kjk gh Graph dks izLrqr djrs gSa, ijUrq Adjacent
Node esa mudh lwpuk ugha cfYd muds Pointer dks j[kk tkrk gSA

Graph Traversal
Graph Traversal dk vFkZ gS & Graph dh izr;sd Node dks Visit djukA ,d Graph dh
Vertices dks Visit djus ds vuds rjhds gks ldrs gSaA tks nks rjhds ge vkidks ;gka crkus tk jgs gSa]
oks cgqr gh izpqj ek=k esa iz;ksx fd, tkrs gSa vkSj ;s rjhds Traversal ds vR;Ur ljy rjhds fl) gq,
gSaA ;s rjhds fuEufyf[kr gSa&
(1) Breadth First Search (BFS)
(2) Depth First Search (DFS)

Breadth First Search (BFS)


tSlk fd uke ls izrhr gksrk gS fd bl rjhds esa Nodes dks pkSM+kbZ ls Visit djuk gSA BFS esa igys
ge Start Vertex dh leLr Vertices dks Visit djrs gSa vkSj fQj budh lHkh Unvisited Vertices
dks Visit djrs gSaA
Coding Talks

blh izdkj vkxs rd leLr Vertices dks Visit fd;k tkrk gSA BFS ds fy, fuEufyf[kr Algorithm
dks /;ku esa j[krs gSa &
status 1 = ready
status 2 = waiting
status 3 = process

Step 1- Graph dh lHkh Nodes dks visit ds fy, rS;kj j[ksaA (Status1)
Step 2- Start Vertex A dks Queue esa Mkydj bldk Status Waiting djsaA
(Status 2)
Step 3- Step 4 ls Step 5 rd nksgjk;s tc rd Queue [kkyh u gks tk,A

Step 4- Queue dh igyh Node n dks Queue ls Remove djds n dks Process dj nsaA
(Status 3)
Step 5- Queue ds vUr esa n ds lHkh Adjacent Vertices tks fd ready – state esa gS] dks
Mky nsaA (Status 1)
Step 6- Step 3 dk ywi [kRe gqvkA
Step 7- Exit

bl iwjs Process dks le>us ds fy, uhps fn, x, mnkgj.k dk v/;;u djsa &
A

B C

D E

bl fp= ds fy, igys Adjacent Table cuk ysrs gSa &

A B C
B A C D
C A B E
D B E F
E C D F
F D E

vc ge fdlh Hkh Vertex dks Start Vertex cukdj dk;Z 'kq: dj ldrs gSaA
bl mnkgj.k esa geus B dks Start Vertex ekuk gSA vc gesa bls Queue esa Mkyuk gSaA

Queue B
Origin 0

bl Queue ds nks Hkkx gS & igyk Hkkx rks lk/kkj queue gS ftlesa ge Vertex dh Adjacent
Vertices Mkysaxs] tcfd nwljs Hkkx esa ;g Entry dh xbZ gS fd os fdldh Adjacent Vertices gSaA
tks&tks Vertices, visit gks tk, mUgsa Queue ds uhps fy[k ysaA vc gesa B dh leLr Adjacent
Vertices dks Queue esa Mkyuk gS &
Queue A C D
Coding Talks

Origin B B B
B A C D
vc bu Vertices dh Unvisited Adjacent Vertices dks Visit djuk gSA tSlk fd ge ns[k ldrs gSa
fd A dh leLr Adjacent Vertices Visit gks pqdh gSa] blfy, vc ge C dh cph Adjacent
Vertices dks Queue esa MkysaxsA

Queue D E
Origin B C
B A C D E

vc gesa D dh cph Adjacent Vertices dks Hkh Queue esa Mkyuk gS &

Queue E F
Origin B D
B A C D E F

vc tSlk fd ge ns[k ldrs gSa fd Graph dh leLr Nodes Visit gks pqdh gSaA vr% Graph dk
traversal lEiUu gks pqdk gS vkSj Queue ds uhps fy[kh List gh fn, x, Graph dk BFS gSaA

,d Graph BFS fHkUu gks ldrk gS pw¡fd ;g Start Vertex ij fuHkZj djrk gSA

Depth First Search (DFS)


blds uke ls gh izrhr gks jgk gS fd ;g rjhdk Graph dks xgjkbZ ls Visit djrk gSA DFS esa ge
lcls igys Start Vertex dks Stack ds }kjk Visit djrs gSa] fQj bldh leLr Adjacent Vertices
dks Stack esa Mkydj] Stack ds Top ij fLFkr fØ;k rc rd nksgjkrs gSa tc rd fd Stack [kkyh u
gks tk,A DFS djus ds fy, vxzfyf[kr Algorithm dks /;ku esa j[krs gSa &

status 1 = ready
status 2 = waiting
status 3 = process
Step 1- Graph dh lHkh Nodes dks visit ds fy, rS;kj j[ksaA (Status 1)
Step 2- start vertex dks stack esa Mkydj bldk status Waiting dj nsaA (Status 2)
Step 3- Step 3 ls 5 rd rc rd nksgjk,a tc rd fd Stack [kkyh u gks tk,A
Step 4- Stack ds Top esa ls Node dks fudkydj mls Process djsaA (Status 3)
Step 5- n dh Adjacent Vertices dks Stack esa Mkydj mudk Status 1 ls 2 djsaA
Step 6- Step 3 dk Loop [kRe gqvkA
Step 7- Exit

bl iwjs process dks le>us ds fy, uhps fn, x, mnkgj.k dk v/;;u djsa &

B C

D E

F
Coding Talks

bl fp= ds fy, igys Adjacent Table cuk ysrs gSa &


A B C
B A C D
C A B E
D B E F
E D C F
F D E

vc Start Vertex dks Stack esa Mky nsa &


:

A
vc lcls igys Visited Node dks stack ds uhps fy[k ysaA Visited Node dh leLr Adjacent
Nodes dks Stack esa Mky nsa &
:

C
A
A

vc iqu% Top of the Stack dks stack ls ckgj fudkydj bldh Adjacent dks Stack esa Mky nsa vkSj
blh izdkj rd vkxs djrs jfg, tc rd stack [kkyh uk gks tk,A

: : : : :
F
E D D
B B B B
AC ACE ACEF ACEFD ACEFDB

vc tSlk fd ge ns[k ldrs gSa fd stack iwjk [kkyh gks pqdk gS vr% DFS lEiUu gks pqdk gSA [kkyh
Stack ds uhps list gh Graph dk DFS gSA
,d Graph DFS Hkh fHkUu gks ldrk gS pw¡fd ;g Hkh Start Vertex ij fuHkZj djrk gSA

Shortest Path Problem


geus Graph dks Traverse djrs le; ns[kk fd ge Graph dks Graph dh Edges ds }kjk travel
djrs gh fdlh & fdlh case esa ,slk Hkh gks ldrk gS fd bu Edges ij dqN Weight fn;k x;k gSA
;g Weight dh nwfj;ksa ds mij vlj Mky ldrk gSA
2 3
A B C
Coding Talks
1 2 1
E D
1

fuEu fp= esa n'kkZ, x, Graph dk v/;;u djsaA ge Graph dh izR;sd Node ls Graph dh nwljh
izR;sd Node ij tk ldrs gSaA ekuk gesa A Node ls C Node ij tkuk gS rks gekjs ikl nks jkLrs gS &
A→ →B→ →C ;k A→→E→ →D→ →CA ns[kus esa rks nwljk jkLrk yEck yxrk gS] ijUrq ;fn ge edge ij
fy[ks Weight ls x.kuk djsa rks nwljs jkLrs dks pqusaxsA bl izdkj ge lnSo ,d NksVs&NksVs Path dks gh
pqusaxsA
;gk¡ ij gesa Graph dks nkgjk ysuk pkfg,A Graph esa Path, Vertices dk ,d Øe gS] ftl izdkj os
Edge esa gSaA Path dh yEckbZ mldh Edge ij fy[ks Weight ds tksM+ ds cjkcj gksrh gSA Path dh
igyh Vertex, Path dh Source Vertex rFkk vfUre Vertex, Path dh Destination Vertex
dgykrh gSaA
2
A B

1 2
1 3
5
F G E C

2 2 1 1 2
H I D
1

vc ge mijksDr fp= esa n'kkZ, Graph ds fy, Shortest Path fudkysaxsA


;gka ij ge ns[k ldrs gSa A ls D rd tkus ds vusd jkLrs gSa &
igys Path dh yEckbZ A → B → C → D ⇒ 2 + 2 + 2 = 6
nwljs Path dh yEckbZ A → G → I → D ⇒ 3 + 1 + 1 = 5
rhljs Path dh yEckbZ A → B → E → D ⇒ 2 + 1 + 1 = 4
ge ns[k ldrs gSa fd A → B → E → D Path dh dher (cost) lcls de gS] blfy, ge blh jkLrs
dks viuk,axsA
fdlh Hkh Graph esa Shortest Path dks [kkstus ds fy, iz;ksx fd, tkus okyk rjhdk Dijkastra dk
Method dgykrk gSA bl rjhds dks iz;ksx djus ds fy, fuEufyf[kr Algorithms dks /;ku esa j[krs
gSa &
status 1 = ready state
status 2 = coloured state

Step 1- Graph dh lHkh Nodes dks ready state esa initialize djk nsaA (Status 1)
Step 2- Starting Vertex dk Status cny nsaA (Status 2)
Step 3- Loop 4 dks rc rd nksgjk,a tc rd fd lHkh Nodes Coloured Status esa ugh
ifjofrZr dj nsaA
Step 4- n dh Adjacent Node, ftldk lcls de Weight gks] dk Status cnydj
Coloured Status dj nsaA
Step 5- Step 2 dk Loop [kRe gqvkA
Step 6- Exit

bls vc mnkgj.k dh lgk;rk ls le>saxsA


A B C D E F G H I
{ 2 ∞ ∞ ∞ 1 3 3 ∞
ge ;gka ns[k ldrs gSa fd lcls de Weight okyh Univisited Edge AB gS] blfy, ge bldh
Unvisited Incedent Vertices dks iqjkuh 'krksZa ds vuqlkj gh Table esa tksM+ nsaxsA
Coding Talks

A B C D E F G H I
{ 2 4 ∞ 3 1 3 3 ∞
ge ;gk¡ ns[k ldrs gSa fd lcls de Weight okyh Unvisited Edges AE, AG vkSj AH gSaA bu rhuksa
esa ls fdlh Hkh Edge dks igys pquk tk ldrk gSA ;gka ge AE ys jgs gSa vkSj bldh Unvisited
Incedent Vertices dks iqjkuh 'krksZa ds vuqlkj Table esa tksM+ ysaxsA
A B C D E F G H I
{ 2 4 4 3 1 3 3 ∞
vc ge AG dh lkjh unvisited incident vertices dks iqjkuh 'krksZa ds vuqlkj fyLV esa tksM+ nsaxsA
A B C D E F G H I
{ 2 4 4 3 1 3 3 4
vr% ge ns[k ldrs gS ;fn A dks I ij igqapkuk gS] rks mldh dher (Cost) dsoy 4 gSA A ls vU;
Nodes ij igqapus ds fy, Path fuEufyf[kr gSa &
A→B ⇒2
A→B→G ⇒4
A→F ⇒1
A→G ⇒3
A→F→H ⇒3
A→G→Ι ⇒4
A→G→E→D ⇒4
A→B →E ⇒3

A B (A, 2)

(A, 1) (A, B, 3)
F (A, 3) G E C (A, B, 4)

(A, F, 3) H I D
(A, G, 4) (A, G, E, 5)

;gka ij izR;sd Node ml ij A Node ls igqapus dk jkLrk (Path) o dher (Cost) crk jgh gSA

Minimal Spanning Tree


Spanning Tree og Tree gS] tks fd ,d Graph }kjk cuk;k tkrk gS vkSj ,d Minimal Spanning
Tree , og Spanning Tree ftldh Edges dh dher (Cost lcls de gSA
ftl Graph (G) ls Spanning Tree dk fuekZ.k fd;k tk jgk gS] mldk Connected gksuk vko';d
gSA ;fn Graph Connected ugha gS] rks mlls Spanning Tree dk fuekZ.k ugha fd;k tk ldrk
D;ksafd Unconnected Graph ,d Tree dgykrk gSA
;fn dksbZ Tree (T) tks fd fdlh Connected Graph dk Spanning Tree gS] rks &
(1) Graph (G) dh izR;sd Vertex Tree (T) dh Edge dks Belong djsxh] vkSj
(2) Tree (T) esa Graph (G) dh Edges gh Tree cuk,saA
ge fdlh Graph dks Minimal Spanning Tree esa fuEufyf[kr nks Methods ls ifjofrZr dj ldrs
gSa &
(1) Krushkal's Method
Coding Talks

(2) Prim's Method

Kurushkal's Method
J. Krushkal dk lu~ 1957 esa izfrikfnr ;g Method, Minimal Cost Spanning Tree cukus dk
cgqr gh tkuk ekuk rjhdk gSA ;g Method Graph dh Edges dh ,d List ij dk;Z djrk gSA
;gka ij Input ,d Connected Weight Graph G gS] ftlesa n Vertices gSaA
Step 1- Graph dh Edges dks c<+rs Øe esa O;ofLFkr dj ysaA
Step 2- Graph G dh Vertices ls 'kq: gksdj ckjh & ckjh ls Process djsa vkSj izR;sd
Edge dks tksM+s tks ,d Cycle ugha cukrh vFkok tc rd n-1 Edges ugha tqM+ tkrhA
Step 3- Exit
;g rjhdk rc rd rks ykHknk;d gs tc rd fd Graph Nksvk gS D;ksafd bl izdkj ls Tree cukus esa
'kq: esa Nodes Unconnected jgrh gSaA
bls vc mnkgj.k dh lgk;rk ls le>saxsA

Step 1- Graph dh Edges dks c<+rs Øe esa O;ofLFkr dj ysaA


AF 1
BE 1
ED 1
ID 1
GI 1
AB 2
BC 2
CD 2
HG 2
FH 2
AG 3
GE 5
vc igyh Edge ls 'kq: djrs gq, Tree dks rc rd cuk,as] tc rd fd n-1 vFkkZr~ (9-1=8) Edges
ugha gks tkrh rFkk mUgha Edges dks tksM+saxs tks fd Cycle ugha cukrh gSaA
A
1
(1) ge Tree dk fuekZ.k fdlh Hkh 1
Weight okyh Edge ls izkjEHk dj ldrs F B
gSaA ;gka ij geus AF Edge ls ;g
fuekZ.k izkjEHk fd;k gSA 1

A E
1
B
(2) ;gka ij Tree esa ,d vU; 1 Weight F
okyh Edge BE dks tksM+ fy;k gSA 1

A E
1
1
F
(3) ;gka ij Tree esa ,d vU; 1 Weight D
okyh Edge ED dks tksM+ fy;k gSA
B

A 1
1
E
F
1
Coding Talks

(4) ;gka ij Tree esa ,d vU; 1 Weight


okyh Edge D1 dks tksM+ fy;k gSA

(5) ;gka ij tree esa ,d vU; 1 Weight


okyh Edge IG dks tksM+ fy;k gSA

(6) ;gka ij Tree esa ,d 2 Weight okyh 2


A B
Edge AB dks tksM+ fy;k gS] D;ksafd 1
Weight okyh lHkh Nodes Visit gks 1 1
pqdh gSaA
G E
F
1 1
I D
1

(7) ;gka ij Tree esa ,d vU; 2 Weight 2


A B
okyh Edge BC dks tksM+ fy;k gSA
1 1 2

G E C
F
1 1
I D
1

(8) ;gk¡ pw¡fd CD ,d cycle cuk jgh gS blfy;s ge bls ugh tksMsaxsA

2
A B

1 1 2
(9) ;gka ij tree esa ,d vU; 2 Weight G E C
okyh Edge GH dks tksM+ fy;k gSA F
2 1 1
H I D
1
Coding Talks

tSlk fd ge ns[k ldrs gSa fd n-1 (8) Edges tqM+ pqdh gSA bl Tree dh dher (cost) =
1+2+2+1+1+1+1+2=11 gSaA

Prim's Method
Prim dk Method Hkh connected graph dks Spanning Tree esa ifjorZu djus ds fy, iz;ksx gksrk
gSA
Step 1- blesa ,d Vscy lkFk & gh & lkFk iz;ksx dh tkrh gS] ftlesa izR;sd Vertex dh
Adjacent Vertices muds Weight ds lkFk fy[kh tkrh gSaA
Step 2- Step 3 dks rc rd nksgjk,a tc rd fd n-1 Edges u tqM+ tk,aA
Step 3- lcls de Weight okyh Edge dks Tree esa tksM+s] ijUrq ;g /;ku j[kk tkuk pkfg, fd
dksbZ Hkh edge cycle uk cuk,A
Step 4- Step 2 dk Loop lekIr gqvkA
Step 5- Exit

You might also like