Data Structure

You might also like

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

QUEUE ADT OPERATIONS

Queu Queue u a linean data Btrud


thad pevmit1 însertion of new elemnert at One
end and deletion of elerment at
other end
| Reay

deletien < k- inserhon


a. N

Front

The basic opelatBon


opeatlon on a queue qre enqueye
which inserts an element at end of thc ut Call
Rear. ( which returns)
Dequeu , which deletes, the elencrt at
tat the dut Known as front
Model o Quee

dequea enqucue
Queu

Arroy Tmplementation ef
As with Backs, any.im
implemetatiorn u
queues like Stacks, both the linked dit

and avag
ag implementation give o() runnin times

gor eve opection.


For eah queu data tetune, we Keep an
the Avray and the positiond tront k back
ohich represenu the end o queuu
we also keep track of the nurrber of
actually in the queu u
clement that ane actually
Current Size
5
Front back

The opuations should be clear.


element X we
incvement Curerdin
To enqueu On

and back .
the Array[bacx] X

-det the Tetun


an element.
- To dequeu
Value
thu Avray[ front ]
Curent SiFe , inctem ent front
’ decrement
+) when Cn dut s ul, the nent enqueu
wouwd be n a rion - cnitenea position.
tha. whenevex front
The Jimple soletion u
Or back to the end of the anay
get
the begining. "is
oTapped around to
knouon circetar avsay împlermertation
En:: Inttol Atall

Frond baor

Afte enqueut (1) -


back front

Aftei enqueu (3) 3

back Front

Afte dequeu, whieh Tetutn (2) |34


back Frord

Afte dequeu. which returns (4) 3a41


Front baek

Actea degqeuc,which Teterns (1) I3 4

Front
back
Attes dequew, which retunns (3)
a4
back Frort
’ Thu tot makes the
queu epty
Some p eqrame
Sene dijecnt ays of repra oti
the {ront and back queu.
(Reje tent tor qormulas)
Opetions on Queue

i) TntialieC) :. intialire a queu by odding tha


Vaue TCar and ront to -|
enqueue () : - Insert an elernert ad end
a)
of queu

3)
dequee C) : deletes the rort elernend and return
ts are

4) emptyco: Tt
empty and Yetnn talse (o) i the
queu u not empty
6) T Teterns tmueC 1) ie the queu u
qul and eturn aie (o). R queu
not

Queue B implerme nted in the tollecing ay


Implementation uing Arra4 (Statie Queu)
Tmplementaton uing Linked (Dynamoc
(Dynamie Lit ueu)
Ihee ate two Common opeatiens One n queuu

They aNe addtion element to the queu and

deletion o an elemert rom the quecu


Tuo Vaiables front and re wsed to
Poimt to the enos ot thu queue.
"the ront polnt to the ror end of tu
where, deletien tke place and vean end o the
hu qu
Tritiaty. eohen the queu u tut. the
and eut
Yet u equal to .

Enqueue opexation :

lo o30 40
Fuit Quu

Front Reor
(

Empty Queue
ER
3
- Queua with 1 elument

FR

Dequeu opeiction:
Agie empty quee, quea with Ielement . Ateu duqueu
30 do
- Atter DeQieu to
F R

linked List implementation of Quee:


Queue implenented uhinq SL
Su (Aing%
Linked Li) node
Enqueus opection ipeyomed at thu
at te end

the linked at & Dequeu opection


d linked s t
Peyermud 3ont ot the
Lirked it împlemertation , tor emp
queue
FRONT : NULL & REAR = NUL
header
NULL
o

Reas
Frond

enqueu & dequeu in


(Rae terd or dirked t

Applications ot Queue
Serve
First come first
Aueue maânly reared
when Jobs ase
Subritted to a printer. the a
Thus Sent
job sent to a
ordex of arri val
ronged queu
Pintet te placed a

data tructue CA quecu: Thele are


ohich disk
-sekup in computex
machine , known as Servet
attached to One
aces to tle
othet machines e
given
Come - fiat served b auu
A whole branch of mathenates known as

qu
ueuing theey dea with Cormputing houw Jorg
Users enpect to coat ine
Round Robin techrique for Procesor schedulie
implemerded using queues
All type ot Customex Service (dike
ailedau
ticket Teservatton) center sotwte g
derigned
are

uing queuel
Other Application ot Queae
- Batch processing is opeuting gtem.
To îrnplernent prierity Queues
|horty Quedes Can be used to Jort the element,
cuing Heap sot
( Reier tent tor more Point )

UNIT- Y

Trees - Binary Trees Tepreentation - opeations on


-

Bìnary Trees - Traversal of a Binavy


Binary Tree
Tre -Binary
Search Trees , Graph - Representation of GTaphs
TraverSal Graph - Dijkstra's
Di Alqonthm
Breadth - First Search.
Depth First Vs
TREES :

Tiee s
mon- inear dota
stuctwe
that used to epreserit
blw n dividual data tems tievasichal selotionships
n
A Tree & a ideal Speacialüed torm for Cvganur9
data tructwre or Storrg
vepresenting hiesar chicat data.
A Tree Can
-
be theoriticaly detned
Set one mere data tems (or nodes )
Such tat

(i) There u a special nde called the oot


o the tree.
(3) Removing modes (or data îtems) aute

Partittoned ?nto number of mutualy encluive (i:e


Dsjoined ) subsets each of which is tbet a lree
ave Caued Sub ree a chld of anode

OriinaTy and Binary Trees Tevminology


Tree A Tree s ginit Set One ON mOYe

nodes. There u -Specialy detianated node o called


Yoot Temaining nodes Qve pavtioned into
A

disjcint seti each o these


TI, TR. .. Tn, where
wheve
Bet a Tree T, Ta. To are call ed &ubtrees

o the oot .
Bran ch s he irk
" Branch ! between
Parent and s child.

Leay i A node wtth no children


chiidven u calles

Subtree.: A Subtree u Subset o a tree


is tell a tree.
Degree : The no. of s ubtrees of a mode &
called the degree of the node
node with degree geLo calied deay /term~nes
node
The othex nodes Tejesed as non-terminal node
ave

The nodes branching rom the antiue


mode X are called children of X and
ta Parent.
children o the
the &ame
ame paxert
Sibline e

daid to be ibi lin gs


highet degre onong Dodes)
Degtee of tree - the nanimun of the
degTee o the nodei in the tree

Ancestors : AncestorS of a node Ue Qll the

medes along the path om root to that node


ancestors of au the nodes n the
Hence Toot
tree.
Level ':- level o
node
oot
u
dejined
level
by teting
One
A

Node : Leve L

Level L+I
. Heigh! oY depth :
Heighd on depth o
depth os a tree
deined-theto tree
be the
rnairnum
(1
level o any
nede fn
heigtd 1
climbinq: (6) () depih 3
The Process of traversinq the tree fm
to the Yool called cltmbing the tree

Descending : the tree

The proces of trave vsing,trom the TOot

caued the
descendinq the tyee
to the dea
Terminolqies of Tree.
TREE
Root
Level Parelode cimbing
Branch
Non tesminal nede
levet (B
H
deten
Levet 3
F, G, H are
D,E QTe

Sibilings
(Leag Note
oT Texinal Nocde)
F, G,H)
(B, C, D, E,
Subiree o
B ( D, E)
Subtree

jDegvee A :-(no.of subtrees )


De qree o c
BINARY TREBS -

Bìnary Tree has nodes each which


no more than two child nodes
has
Bìnay Tree a a Set of
qinit sel of nodes t
is eithex is empty Consutt o
f a oo! and

dajoint bìnary trees call ed dest subtree and


nght subtsee
Let chilcd i- The node present to the det o the
Parerd node
Right chúld :- The node present to the ight of thu
Pevent nocde.

Skewed Binary tree i- I J the


the mew nodes in
în the tre
are added only to ohe ide the binary tree
tmctt binavy tree : the binary tree has each
node consuting of either two nodes or modes
a t al.

tree i
Complete binary tree T al the nodes of a binary
tree Consuts o tw0 nodes each and the nodes at
the ast level does not Corsut any nodes, then that
tree Caled Complet inary tree
Can be obseved as max, no. of nodes on leve
binarg tree is
o°8 a no. Qi-1 cwhere i i
of nodes de pth
man.- no. o k u whue K
Binay tyee. Skewed Binavy Tree
A Root only one Side )
t chitd Rightchildtd
Righe child
D

sAnicty binary tee Cormplet Binary Tree

Properticl of Binary Treei :


) The nd ot nodes 'n en a yuw binary tree
Cd leaut and n: Qh+}-|
the tree.
h: heigta
has
A binary tree of n elemente n-1
edges.
h as atleast h and
3 A binary tree of height
at most ah- element
a Toot hode has a
(4 A trce consiting oj orly
height og o.
56). The trce.
no leay modes in a Paxycet binary
leav es has n Q - | nde
(O) The no tree
indeenal nodes n complet binarg n/2
REPRES ENTATION Of BINARY TREES

-These ae two în cohich


binany
a
ways
Can be Tepresented. "They
tree
Array Tepresentation of binory trees
Linked re pres entation of bin ary trees
Tepresentation of Binary Trees

when asrays aTe


a Lsed to
Tepresent the
binary trees, ther an array
declared. :. K= depth ot the tree
For en:-
The depth ot binary tree 3 3, then
maimum
3) -I =(4 elementt) wil be preen
the node. and hena, the aYray ize wi
be
Thú is because ,the element are stored Torm
1

leaving the
positiorn ,one Position VacantE

30

Arsa Reprasentation :
30
3 5
The seot elemert
elemert u aluways stored in position
1. The tet chid ot the node i stored
in the position &i nd ight ch-d o node
Stored Pasition
Hehce the tollouinq ovmula Can be sed to
iderti the parert, dejt chiid and igha chiidnode
oo a

Parent (i) = i/2, if i t , I i=1 then 'i' u


the YOot node and soot does not has parenu
Leytchild (i) = ai, t4 aisn where h the

manimum no. ot element n tree.


ai>n, then i has no lei chijd
Kighi child (i) =
then ? has ho child

The empty positioru in the tree, whee no

nede i Connected ave vepreserded in the array

(-i, indi coting absenu of a node


uing
Enam ple by uring tormula i- (Compare with array
TepTeerctatior
the node Value
we n
assign the Pavent )
-Parent : i/2 (3/s ) = I ( 5o iu
-let child &i (ax3) b. Bt the Position b

lejt chitd.
tonsists h -! ndcoating thue ü
6>K * Condition
Q(3) >
5 5 salilies
-Right chtd i | > a(3) +) z
Position 4 4 in the arry Q0.

binary tree
inked Representatlon of binary
Ih
-In inked Represerntaton o binay trees, înstca
inteo:d
aTras. pointe ar Used to Connect
ave
the Vat-
nodes o the tree.
Henca, each node of the binany ree consut o!
three parta ingo, legt. ght
-The Tnjo part stores the data, det pat tote
-the address of the dett child.
Right paet stoves the addhes of the ngh chid

30N

|lo

-The Polles sto ring NULL Volue indicales that,


there & ho hode attached to t .
TraveTS al i- Leyt child accesed Jet k og nede

child QcceHed chid of node


nght

You might also like