0 ratings0% found this document useful (0 votes) 36 views31 pagesDSA Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
|
°
BI
INFO 1m formatione SS LINK HEXF
part pointer field
node node 5 nodes
lAddresa of next nade in tise 2 ——__————————
isttisttistiisiista
infor mation part of <1 tink part of nodes
e = u he i
node K
LINKIK1= consoins the address of next pointer fie
of node kK or address Of next node of Kk____etart>) contains ‘osdress of Firat node_of list.
____INULL7 Indicates th e_end_OF- list.
Let us consider alist which the nodes of | “ist neeq .
occupy. clementsin-the-array aN FO_and Link, or
"INFO LINK —
\ \ =
2 2
_3 | 3
_ = .
: sf s 7
6 6 _
7 7 %
8 | 8 a
q L 4 —
10 lo
u u
TRAVERSING A LINKED LIST *
_____iret tist be atinked tist jn memory stored inlinear—
___|orray INFO ond LINK with start indicating address
__|To +raverse oO linked list means +O process each node
lO+least Once.
_____|Her® ie, use pdinter PTR =LINKIPTRI,LE,.
currently being processed link Of PTR points +0 thé next
—_|nod€ 40 be processed. so,the statement PTR =LINKLPTRI
—_|moves pointer +0 the next node in the list —_—TRAVERSE. (LIST, INFO,LINK,START,PTR)
<19|Se+ PTR=START
|Se+ PTR=START
dyl count = count +4‘List ig Ungorted 2%
— jLet ug consider ist io unsorted in. memory 4 this algoritns
Finds the jocation_ of irem in St when item firs + thi
: ~ lappear_in ist otherwise it et LOC SNULL, i —
SEARCH (INFO,LINK,PTR, START, ITEM, LOG)
___i9|Set PTR= START — — -
___dip/ Repeat Ss SteP dii2_untii PTR #NULL
‘te ITEM = LNEOLPTRI, th en——— wo
__ did
| toc =prR_and_exit
else a
* \List is sorted:>
lsuspase data intist is sorted ,here We search for ite
lin list by traversing the list_using a pointer varianie_
pTR_and compairing item with cOntents of LNFOIPTRI
lof ech node_Gne by One of list NOW we can stop
—__ lushen LIEM exceeds INFOCPTRI it the list is in descending
order “
___JScEARCHCINEO,LINK,ITEM,PIR, START)
__<|set PTR= START (
dil Repeat step Ci) while PTR#NULL zi
Gibltf ITEM < INFOLPTRI,then =
Set PTR=LINKIPTRI i
els & c
| Set 1OC=PTRand exit = TTT
= —tav:
— array and supt
\setis in_array wilLot
__luge-.one way to-do
Suppose our linked liot io implemented by parce,
pose_insertion- and deletion are to i
nked list 5 then the unused memory
90 be linked tigether +0 form
_ag_ its list vAriabie a
performed on our ti
ain Ked fiat using avai L
GARBAGE COLLECTION > - am
suppose node is deleted or an_entire listis deere
Ithen si ome _memory Space become res eabie
Isom © want this space to e@ available fOr suture
“this i¢ +0 irmmediotely reinsert _
the space into free rorage. list _
I1t+S cis. i i shod is time consuming
lsor the Ope: rating systern-of a computer sO We chadse
Gq aiter method »the operating sys tem_of_a_comp Utter
lway periodicat y collect oli the deleted space _onto_
all_untagged_space onto the free storage list. __—
lmsertion rhto Linked List.
Tnserion into a sor+ed linked jist ______—
insertion _end of linked list
———__|with_O\\_of tnes: 2 coses,cometimes new AQth. are +O be —
.
epee tee eae Available that is{
ee ree storage is empty ten this condition. ia coed
OVE FLOW» U0! =
hen free space = NUM
let list we a linked list in memory or Algorithm.
—___|assume that the. inked list ina memory inthe
EL
iid
dip
f INFOCLNEW1 = T+em.
Givi LINKLNEWT]= START
w
wi)
START = NEW
Exit— suppose_a nade_N_is 9 be inserted into list betwee,
lnode_ A and &. =
~
- Ke“ ;
: ‘
N
— 1
, N
SO here, at_thi node NF 7} ,
== 2
lmere are some Special CAS@s 4 Sa
= “pltn the 1ist_if the new node N_ias_the_tirst node in
lpne sist then_start will point to new nade N.—— —
ode N is the last node in tist then_N will _
__d1f the new _n.
__|eontain NULL pdr
LINK LLO CJ = NEW
awit LOC=NULL, then
|__ AINKENEW1 = START and START=NEW
2S &
—ea exe
| Ext NKLNEWI=LINKILOCT & LINKIIOCI]= NEW| Here traverse the list byt
Ineertion ino a. sorted Linked List
\suppose. 1tem_is +0 be inserted into_o. sorted 4inkea tiot
then item must be inserted between nodes hs
©0 that, INFOLA] <4 LIEM SLNFOLB1———-_—~ ti,
80 here, with the following prowdure 1nd thie 100 tion
; ofsnode in list when voiue ig Si. ;
using pointer voridble PTR :
nd comparing LITEM with INFO LPTRI. While traversing
{KEEP track OF 1OCOti On_of preceeding node by using———
{pointer variabie.
Save o pointer PTR voriGble Gre Updated —————————
____|__save =pre 2 PTA =LINKEPTR, :
Ae
|S0, traversing stop when item je—< INFOLPTRI,then—
___] ptr pointer points 10 node 650 SAVE the location —
ditlset SAVE = PTR and ETR=LINKIPTRI
WWirepea+ step cw) acvi whiie PTR NULL
wots ITEM < INEOIPTR] then
set LOC =SAVE Ond return
oot SAVE = PTR & PTR=LINKIPTRI
" | e+ & LOC = SAVE
evil! Re+uUro——«h>
—_did|CALL INSERTLO-C CINFOSLINK LISTS 1TEMSSTART)
iid
ANGERTSORICINFOSLINK, LIST, 1TEM,STARTI
CALL FLINDLOCCINFO,LINK ,LIST,ITEM,START)
Exit
DELETION _F ROM _ LINKED LIST’. rae a
___|rettist be alinked_tist in_memory with a node betweer
node A And B,_
STOEL
7
—__|50,M.ways arter deleting a node from dur tinke. Atist
V3,
—e worite Following steps +0 add deleted space to
D1Deleting the node salowing the given nade. _
a i: >
iii!
the beginning Of Oval list.
f———__________ LIN IKiKT = AVQi)
Different cases.
Deleting the node with given item of ineorm tion.
jPelete the jast node trom tinked ist
Delere the node sonowing a given node.
LOCP=NuULL
——— Algorithms DEL CLIS
QJ LE StOrt = NULL , then “Under¢ ious 9
Cb)!
| —_________se+ Stort = LINKCstar+)
i
Lf LOCP = NULL, thenfo NS ml .
- set LINKS! LLOCPI=LINK ocd
enerin cl= AVALL and AVAIL =LOC ___ — —
Exit
[EIND B(L1S Ty INFO,LINK,LOG ,LOCP)
a> |rf StArt¢=NUIL, +neEn
sb Tf INFOTS+Ort] = 14em , then
= Ei = +
(6) |S@+ SAVE =SFOrt Ond BREN Mites PTR =LINKTSicn dy]
cd) 2 t p) i Z .
__<@) |Te INF OreTRi= Trem, then =
c= =
elge
[set Save =pTR Gnd PIR=LINKIPTAY
A | set LOC=NULL
cg)
ExitDeleting the 1aot node frorn linked list +
——- | Assignment —___- a
I
— 43)
—_|Where the 19st nade contains the MULL pointer ,
ite y ig a special nodé it is similar +0 sirane
MEADER, LINKED | Lis =
It is ainked tis + which OlWwOYS- contains. aspecian node
cOled the header note ot the beginning Of list.
\There are. +wo_types_of header list — Se
‘@rounded Header Linked. List > aut
linked 1iGt-— — — a
—_dibCircutar Header
_____iwnen +he lost node | points back +0 the | header nage,
—___|Here, start will Qinays point +0 the heQder node
epeciat_coses:
(Od |1f LINKISTART 1= NULL it indicates thot _o. grounded ~
heOder 1iSt.
__ bd Le LINKIS TART] = START, i+ indic CO+8S thot _ocircul ar
header is empty e
Traversing o CHIL.
__ dalser pTR =rinkistarey
did REPEAa+ steps Gil) w Civ) while PTRF START
cdib| Ani. procesa-4osTNEOIpTRan. he
aN)
wd
Se+ PTR=LINKIPTRI oe
Exit___|1+ ig a tinear conectian
nodes where eact
| POUBLEY LINKED LisT(2=way Lists).
of data elements caued
nade N {6 divided inta_three parts,
Jan intorma ian figid INFO which cOntains the data og
TL
Information of node N
p 5 oe 7
lofiast Made and backward ield Gf First nade in
the list
1a9+ instead Gf One START.
> |put the AVALL 1i5+ will be maintained as_a_single —
(inked ot 2UNLT=1V TREE
TREE* es
A non-linear data structure is caued tree 2WhEN there 7
Lis hierarchical relationship between elements then we
Use tree +0_represent data. Mainly here te discus s abous
Binary Tree (@12. Tee iS _defined 09 Finite se+ of ¢ lerments
cauled nodes such that wu 4
T ig empty catied the NUL tree Or Empty tree _)
Tcontains_a distinguish _node_ecaued the rOot ang
the remain nodes of tree formed Ti _ON_ ordered pain
disjoint binary +ree Tr and Ta
Let coniains a root R then the two trees Tang Tt,
Are _cAlled the 1eft And right subtree of Rif Tig
Inon=-empty then _i+e root jo_called the left successor
of R similarly if To is non-empty then imMiOriy ie
Tais_nOn-empty then its root node called wight
successor OF REvery node Nin a.binary #ree T except roar none hos a
Unique parent called predecessOr OF N. ALINE dragon, froha
Q_nodé Nn of T to 9 successor is called edge and a
sequence of consecutive edges is caied a path. a
mia is called a1eaf Anda path ending ing
eat i coled a branch .£Qch node in binary tree T is
pe | Qssigned_o evel number the root R_Of a tree is ds signed
to 1evel nurnber O and every other node is assigned _
| the lever number ushich ig One mOre than the level
——__|number of paremt.mhe depth and height of a tree Tig
——_|;nGximum_na. of nodes in branch of +ree op is one mdre_
than largest number of 1,
{EXTENDED BINARY TREE *
—Exm! ART - -
fel 2 ie has either O or 2 chilaren. he nodes
+ re i
lagi citcie ‘he baad thin epi are emerneh nodes 1
are denOred bY square,aaaaeeael REPRESENTATION “oF “BINARY REE IN MEMORY) _
There are +wo ways oF representing- pinary tree inn emony
-—ia Sequential Representation ———
magstion
Linked Representation 22 ————————
Tbe mainsained in_me ema ory,
____| considera pinary+ree- 5 Tull
iby tnree paranel array _INEO, Leet, Right ANd ap Ointer
we ante ROOT-EACh NOde Of Tusill COMSSPONG-+0 oO
location Stich tho —
NFOTKI2 contains dato Of nade N—————
eee comainsiocasion of lett child of NOG N __
Inightik1 2 —_ tigi chi iad
eet o comaine iacotion of root os mékke—te if-any
—_leup-tree ia empty then corresponding pointer tailt__
1 Romain NULL VOlue Tf the tree Tis emery then 1004
____lusitt_contain-the NULL Value
|
aam|
—— ky te e ao
a
a ti eoTree.
K=t
= 1 2%+=2-
1 2xitl=3 3 5
Ke
a BSL L
15. 4 xa+ = ae
\B _5
eo ‘9 20 16 19
Be «7,
19. 9
20 4
1610
9 TY= ooeenee
[TRAVERSING BINARY TREE ? a
__\There are three ways of sraversing se DINGLY 4ree T wish
root Ro —
Preorder , inorder, Post order
__ Preorder. First process- the root R_of T-—————_
Ww tree of Rin pre Order
+ pracesa the lefts!
Ls process the right subtree Of R inpreorde,
Tmoyder 1+ Process the left Subtree— of Rininorder.
process the root NOdEe ROFT———— at
*pracess the wight subtree of Rin inorder, — a
rocese ane left subtree Of Ri in post order.
|e proces the right subtree— of Rin post order __
__*procesa the rootnod © R OF TO
- “OF STACK ———
TRA eee ALGORITHMS LSING_STACKS
‘suppose a binary tree Tis maintained in-memory by __
ome linked represemiation,
gina ioe ues a sore aaa Ene
ALougorithm uses a variable PTR ushich contains the
location of node N clirrentiy being processed. _
vend denotes 1eft subtree /clnild of mode N.
- REND —
—____| A stack 16 On array which will hold the node for —
future processing.
__AIgOrithm >
—___|Initiaily push NULL onto stack and set prR =ROOt —
ithen repeat fououing st@pe
-__(0)|Proceed down +he left most path rooted at PTR —
: processing each nodes On the path and pushing—
—_|€0ch_right child RCN) Onto stack. The traversing —
lends afrer a node nv with no tef+ child 1¢N) is ——
processed, i.€.,we stop when Lef+ (pTR)=NULLLJ
element_Of STACK +0 PIR Gp
__%») | PaP and assign the TOP¢
PIRF NULL ,#hen return +0 StePCOd Otherwise orig
a =
a c =
Jf A
D f__F ~
IN
Ge H
. g s
G) |STACK =A
iii
dv) +
cv) |e PTR = KK, STACK = G40
__«vid |
Oa PrOcesS H, STACK = dy © 5b
process k » STACK = hy
svi BI
ix) |
PTR= a5 STACK=
noes PIR =C
IQ) Process C» STACK = yf
&
lad Process FS TACK= A
uu
A, B,D, G,4,K,C, ESE > Preorder Traversalerr ——_
pook = 2: “ah
TOP = 4, STACKLLL = NULL one a au of escnnna enTE
- DRepeay strep dita +o while P
——cANGAppIY process to INFOLPTRI
Ova tRight child a
HE _RIQHtLPTRI# NULL then ———
owes ice o08 ty STaCALOPL= AIM LPTED
——wa|tLet+ Child
— it \eestpraig Nui anon ————
_Se+ PTR = reftlPTRI
eloe = -
[ger SIRS STACKLTOPI ,10e=TOB= 4
Predrderiyr, ROOt,PIR» 5)
Widlexit
—__|Tnorder Traversing +>
[et tse _uses_a pointer variable PTR and an_array stack.
____| with this _aigorithm_o node is _processéd only ishen
tt te POP from sTACk.
—_Tinitioty pus NULL Onto STACK and repeat FONOWing_
Steps until’ NULL ig POP from STACK
__doieroceed doin the left mOst path rooted at PTR
—____|pushing each node N Onto STACK and stop when —
lanode N with no 1eft child is PUShed Ontq STACK.
— Bo PoP and _process the nodes on STACK Tf NUL is pope—Aiv)| Proce ed _dounn tne eft
Ost pth rooted of eTR=\4
8 cS
Z L
D =
q H
L
k L__M
i
il >}
wa
wal
Haat samackinpy sore
(b)_set PTR = 1ef+IPTRI
leet PTR =STACKITOP] & ToP=TOP=1
Repeat step CV) +0 cli? whike ETRE NULL
| Appt 8
wilt, eigntipTRI +NULL, then
vi | =
co) Set PTR=RightiPTRI
lth) GO +0 step Av
tai)
Exitnee
. Pg, a _ — —— we
Order Traverei > ee
i erciing _
‘Nittany pus NULL wnt _STACK.and. 06+ PTR = ROO+ ang
repeay the folowing _steps until NULL IS POP from STACK
Pro, —
SEA down the 1eft most Pp aan SOCh node
~Of the poth, push it_an +0 STACK OOS if NM has g
Wht chia RINT on +0 STACK. —— -
POP And process _p ositive nodes -on STACK.LEO nega ive
Dade. is popped i.e. if PTR No then Set PIREN ong i
return to step —
te UU is popped then exif ——8 —
> POp_an process E> ose cunt - a:
POP _Gnd_pro! “1405 D, BE ca :
Post order +raversi
) ee met
uubtree
j 50
-
30 10.
25 35 55 15
O 3338 BO.
45Of o.given it item ana iss _%
ocatio
+ree_ bY using poidter variable _
~ Tais_aigorithm finds ne!
Porent.1t traverse +e
>
TR_and SAVE. jan orieem-and. ens shves loca oh
yn
~~ LOC gives lacatio! -
}OF paremt_of item.
LOC: O0,PAR) is
IFLND CINE O, Left Right yx00t Hiern ° _
Special cases? nd ico eis 7
: +e. Tre ery
Lie» it_UoliLin pty.
LOGS NULL & PARE ND indicate isem. 1s root of T
1. ini
(1LOc# Non & PAR = NULL sit Hl cate search Is NSUCCeSSFiy
GiD/LOc=NoulLe PAR =NI an pe added as onew
isem_is not found in T,that
node in T z - eturn
id /T# ROOt=NULI
Ine tera = INFOLROOH 1 ,thEN-LOG-= ROOHSPARSNONE ATE
__ dv) IRepeat atep cv and cvid_ushile PTR NULL
cv Le bem = INFOTP:
Set LOC=PTR & PTR=Save And Return
__ vid |e item < INEOLETRI, th €p
Save =PTR & PTR= 1eft(PTRI
elSe
SAVE = PTR zg PTR= RiQh*+ 4
wildse+ LOC = NULL, PAR = SAVE Z
cviiexitDeletion_in_B:
Let T_be a 8,9. 1-SUPI
liter ond
——_|When_n_is “gelered from" ae
—__—_|possibiliti es 7
N_has. no children +hen A ULL value
repiacing ane 1ocasiot Tey acu-dencies +66 Ne"
N has +o childret 2 ‘om _1_by_titst deleting
Sonor of Nthen Nis deleted
etnat trom_1_and then tepincl ng_nade NPYy-nOse oA —
lg se Ot gy cation Hab ere
|___ This. cugoritnm doit © inter PAR gives tne
it indicate.
__did [tf PAR # NULL» then
N_doesn?+ bave =
ocation of parent nadée_of_N—&—P =
lynat nis te root node
a DACINFOsLEAt RIgh* >LOC, PAR, ROO, Leer)
e
jolLe eetLLOC1= NULL andl Right LLOG1= NULL shen ———
get CHILD = NULL
else
eft L1Ocd + NULL
Set CHILD = Lef+C1 OC} WW —___~
else
CHILD = RightLLOC]
| LE Oc = LeftC PART then
set Left PARI = CHILD =
OSS
| ae Ee\5e “ i: ee
_ROOt =CHILD _
+$——
—cllid| Exit _
lit deletes the node N Ot 19cotiOn 1OC where Mate 129
children. Here, PAR gives the location of parent of nOs< N
if PAR=NULL it indicates thot Nis POO NOSE. Siippose —
SUC gives the location of inorder success, uae Nt ond
—__|PAR SUC gives the lacatiogn of pore n+ of inorder succes:
DEN.
lcOS2 GCINEO, Left, Right, OC, PAR, ROOT, 1term)
beast OTR = RigntLL1OCl ond save =1O%
—_tb)|Repeot while LeftreTRI = NOLL ———
Set SAVE = PTR
Gi) | (0) 1¢ PAR NULL, then
| gg pOC = peter paRT them
S e+ LeF+ (PARI = 5UC
else
+10 2
E\Ge
+ =
S2(b) SC+ Lef si SbCl = 1eF + T10c1
Righ+(oucd = Righ+tidci
ayy Return,88.9.1 ip in memory-ane ON
jslven and deieres the-tertt PAR, ROOT, Item)
Delete as tUned,rett ROMs Oe poor, tiem)
(I COUFIND CINFO Lefts RIDATSEE On and exit
9 Le Loc= Nui item ig.not SOUR nen —
Gils RightrLoc1# NULL a LefttLO
WAlExi+
;
|
!
!
|
66 55 a5 4g
L L
£6 55__48 55 69 77 95 2S.
2 L
O6 3.0 25 |
\ Z
IB 40 30 56J - _
1213 WIS te iz is
| to 4 1a
i Resa BS asthe aT
|
myIB)
ayy
iG Repeat step citi 10 vl
Cv) Set PAR = PTR/2 ——
— (>.
— via
— wild)
_|peteting the root of O
ivlassign the rod+ R +0_SO!
TR,LOC ROOF, Item) _
PAR» P
INS Heap CINFO,Le FtyRiGht»
Tre CLA = 1y-em_ & exif ———~
Set N= N+t1,eTR=N————
hiLe— prTR 21.—
09 |
a
Le 1rem= treeLPARI hen —
ete
~~ get TreetprRi = item & ResUr _
6e+ TreeLPTRI- = Tree LPAR
Set PTR= PAR
Heap.
cap with N elements & we toant to ——
This_can be done-as
me variable i-——_—_—_—_____
deR by the last node | of H_
Replace the de! leted nos 4
60 that io & comp Lete tree but not necessarily —
suppose H_is_ah
IdeLe+ed root R-Of _H.
heap
inc . « .
aGK +0 its appropriate place in H 60 that 4
____ Wi DRehemtLet_L_+
fi pty fa @ 0p
R= 955,L592— 95
step ti22 Ne
/~ a5 70
95 20. /
N lo™N
55 33 30 65 aT] 30 65
15 [20 15 (Slo ceao8
Step lt. 35
aN
E 234 5 6 18 410 i
jaojeshno 35)33 [20 [65 [15 20 |I5, 22 |
55 08.
| 4s Z™
xz 3330 6S__Ine INFO, Let Right LOC, PAR, ROOF, Lenn) —_______
sd se+ Item = tree tit
bl set LAST = Tree tn1,N=N= 4
—__uibiset+ prR=1, reett+=2,Right=3
Tidvikesan aeamne nite ige=a E
—_walit LAST? Treetlett1 & LAST 2 TreerRightt
5 Set TYEE TPTRI=LAST & return
WL TreetLeft] 2 Tree rRighti +*hen
Set TreerepTRi =treelrefti g PTR=LEFt
else
A TRi= igh: PTR=Ri
=o* R i =Q* OT 4
wildit et pett+=N4 LAST =TreetLeftl
Se+ PTR=LEFT
Set TreelpTRI = LAST
ciMReurn
Heapsort
Golrepeat fer srom _I=1 +0 N-L
uw J44)
ap ct i 9.
COU pereteheapCa,N item)
eet ALN+ 41 = item
aid | exit
___lcompiexity+ Ocnioga nd