Data Structure in Hindi PDF
Data Structure in Hindi PDF
Data Structure in Hindi PDF
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
(ii) Doubly Link List: - Doubly Link List esa gj Node dks 3 Parts esa Divide fd;k
x;k gSaA
Coding Talks
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
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
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
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
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,
(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/+
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
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
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
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
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
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
(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
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
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
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
(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;
}
}
}
Node Structure: -
struct node
{
int info;
struct node * prev, *next;
};
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;
}
}
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;
}
Function: -
deletefromfirst (struct node *head)
{
struct node *temp = head;
if (head = = NULL)
return;
head = head → next;
free (temp);
head → prev = NULL;
}
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
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
(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
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
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
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
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
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
(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
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
(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
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
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
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
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
;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
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
mnkgj.k & 3 5 11 8 4 1 12 7 2 6 10
dks gesa height balanced tree cukuk gSaA
-1
vc Tree esa tksM+us ds fy, 5 gSA ;g 3
igys tree ds right esa tk,xk &
0
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
0
8
Coding Talks
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
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
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
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]
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
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
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
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
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
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
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 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
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
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
(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
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
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 &
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
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
A B
C D
A B C D
B C E
C
D C
E C
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)
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
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
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
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
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
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
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
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
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
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