0% found this document useful (0 votes)
9 views17 pages

Ds Unit4

aktu data structure third semester unit 4notes

Uploaded by

Ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Ds Unit4

aktu data structure third semester unit 4notes

Uploaded by

Ak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

TREES

Tree w a
a hierachi Cal
noninean data sbucture & epresent

Cg ReCovds, famity
elatiomshu

hee,Takles of
between Data elemorts .
Cortertta.
BASIC

Keot :TERMINOLOaY:
Re ias firt in the
fierachical mongemunt
0f data items. h tiy. A s tko Rovt tem
Node: Each data tem un a bee us Call ed Node..
t shecifies -tfe
data 'nformation & Links, to
OtR dorta tems. Here A8,c, 0,€. are Nodes
Veee of A node : st the nwnben of
subtrees
Node A s3, node in a given hee.In tke fiy-
of a
dagree ot

D eqee of A Tree:- s Re mamum


JIven Tree. degree of Nodes
Hce te deyce in a
of ee Tiee 3.
Teminal Nodes: A node
Zodegee si
Calied
Terminal Node r
hodes Here 7 leat.
are asTeminal
E,T, G,H, K
Non
Teminal M
Nodes: Any node
deree (excat Root
us not bde)
Kero uy ofese
These care five Called Non
non Teminal Node'.
Termihal Nodes.
children nodes of Jiven farth a
SIbling: The
Si hlings. Here E& F Coc
are Called
Parent Node B A K,L,M oesiblgs C4 Parent
shuchue is Qevelled
Level: The entie hee

lovt Node us alwos C.


way that tRe
a
1 tEer
Cmmediate Children oe at level
&

Children e level2 So On. A

(A)- Rovt
Edge Level o

(D) Level 1

(H) Level 2
) ) Level 3

Tree

COye :- SE is Comecting Line of Two nodes.

fatf : Jt js a Seyuence of Consecutive edges f


the owce node to the Destinction iode.

Here Patt b/s A AT is (A,8) (e,F)&EJ


no. of
9LordDeptt:
botwtot a gvo Tee. aa Heve Rept (Heigkt) s
The Moomum numben of Nodes in
a Branch o1 Tree.
forest: is a
Set o4 disjort
s a ToreSt
LIHh tiee Tree
Trees. Hee tie
(Aey Removng Ra
Branch: A Path ending In a deaf us Calied Br

ihe ene wUe then the moximum hevel oy


vodena fiven tree.
g
BINARY TREE:
a finite Sct
A bmeoY Thee
o elements,Called
T s defined
nodes,
Such tRat
(9 Tus empty (Called thenull Tree or Cnpty Tree)
) T Cotùns a distingished nodes R,Called the
ot 0f T,& tfe Jmaing nodes o4 T fom cn
dered fair of dijoirt bmey
Trees Ti& Ta.
1} T does Contun a 00t R, en tetwohees TL

& T2 ae Called ubectively, tRe Left & ngkt subbees

-tRe Left Succesor of R, Similarly If T2 nm emphy


Called the JUght BuccessoY 0f R.
-thon fis Jloot y

ARovt Node)

Sleft SuccessY
0f A oY
Left SubTe
Right Subhee
E

Bmay Tree

% 6 left succeSsaY
s &C is Sigtsucces or of Noe A .
Anynode N na
binay Tee has eitker o,L or 2 sucCANOYA.
The nodes wHh no suCCAs or) Called Terminal Nodes?
ALGGEBRAC EXPRESSIONS :
E= a-b)/ (c*a)+e)
Above epeson E Con be cpesented by means of
-tke binaoty Tree as-(inaling ondy bimary opeatio

a b

C d
COMPLETE BINARY TREE:
A Tree us said tobe =
a Comhete
(f al its levels
TreebmoTy
except possiby tte last, have t
Maoimum numben 0f PoBsIble nodes. 1e. Level
C4 Bree Can Rave t mot 2n nodes. y n ottar
Wrds f tere au m nodes ct Lerel then
Tree
Contans atmost Qm nodes at Lerel .

(6
Jn Complete Bnaty Tee,childen &
Chy hde KK.be Pasy
farent f
Rateyminid as
The Lefl & ight chidd o4 node K au hodes
B*K|A +1], JUspectely.& tee Perent of
nole tk u te node [K½.
8 The Qerte (0,) of a Complete Ginooy Tiree (T)
LSHE n node given by

EXTENDEO GINAAY TREES :


to be an Eptended
A binay Tree
Žaid
(T) s
bincoy ree (or
O or 2
2-Tree) if Cach
node has Gipeitkey n
Childen.
The nodes stt 2oso childen
Jhe noddes

n
oite To childen
Called-Gaternalnodes
Called - temal nodes
fg. Circlesed
for jnternal
nodes teeal
Seuwu ()used for external
nodes.

(- Bina Toee.
e Conetted into . Lateded.2B
So

hancd

ARRAY REPRESENTATION OF BINARY TREE


An Any Can be wsed to store tRe nodey

4 a bmooN Tree. Thu Rebresentation Uses oniy hge


Lineoon Anay TREE s falloWA:
(a)The Robt of Tree ius stored on TREEL1].
(b) It a node N Occubies TREE (K],ten ts left Chia

y stored in TREE [2*k] & tts nght Child Ato

tn TREE [2*K+].
I{ TREE [I= NULL, Undicates tie Tree s emphy.

45
5)

30

15
(30 Q5

R5 3
*)Reyre 4 iS

Tree has oniy 9


loCations
Aray n een
16

nuie enthes or hodes.if nclud


succesorA o
tyuire 29 memory TerminaQnds
Aface.
Note: The Aray
epresentation of
f a tree
JYire an d d wi

(K wually
apfroimately 2d+l elementa.
Ineficient mehod
as stted above eason.
cficient for Complete
&inany Tree or Newey
to Complce.
The lrguol sefrasevdotisn ya node in c is ien elas
Struet ade;
Chou dodo;
nde *Retild
Hrut node child;
Sut
Gedet atzuct nade

LINKE0RE PRESEN TA TION OF BINARY TREE:


bincoy Tree oiR bo maintinad ln memoy
by Linked list which used Three farllel Aays

INFO, LEFT, & RIGHT.nd a fointer vanable RocoT

s.folRo:
(a) INfo fk? Cotai-ee dat atte hode N.
b) LEf T [k CotùnyA location of te Lett childof Nodan
C) RIGHT [K? ontoainy t loCatim o t iyhi childoNadeN,

KoUT wo e Contun te tocatiun ofe


H

fonten
Any subtbee s empty ten tee Conesfonding
Root R.

oì i Contuiin Ae nuee Value.

If ie Tree (Te itzel{ is empty, tRen RooT


= NULL. INfo LEFT RIGHT
2
3
3 15
RooT

+5 45
4 30 7
AVAlL 25
77
|3
88
90
12

13
12
14

(UNKE 0 REPRESEN TATION)


TRAVER SING 6/NARY TREES:
TiYe
There ae thee
Ct traveIŠing a bincoty Tree ortR Rot R.
Rre
orden: (ay froceys the Root R.
(NLR) (6) Taverye the Left bubtree of R in Preordey

(C)Traverse tte right subtee of R n


freorder

Inovde: (aw Toaverse the


Left subtree
(ENR) of R On hovden
(b) Procuyy the Rovt R
lc)
TaveYe-tte ight subtree ofR n norder.
Postovde; a
Travese HAe Bub trce
(LRN) Left f R n fost orde
(Lejt,nyt,
Nodu) Traveue te Right Aub bree of R in fost dy
(C) Pocess tte Rout R.

E H

k
L

PRe ovde- ABDE FChHJLK


Postovdon -DFE BGLIKHCA
Inodn - DBFEAGCLJHK
(
Reovden:
A

fost
ovden:
AO

to
6 HO

Inorde:

Do

Lo
HEADER NODES: -A speci al node s added to

begiming of treen'inkad oepresontation cf binory Tree


Called Headen Nodes.
The Left Poimter o4tRe Readen node wie bint
Root o Tree. e totu
LEPT[HEAD] RooT =
tx]HENDER NoDE

x|K

31H a binaJY Tree us empty.Then TYee e hll


CoYtin feaden node, buct te Left binten of the
headen node )
Contùn the null value. Ie

LEFT [HEAO=NULL -Idlicate empty he


f a node thas an empty Subtree, ten te Po'mterfiek
toy tfe ubtree bi Cortein te addres of the feadrnod
nstead o4te null alue. AcCovdingly No Pointen oil
eveY ontain on Lnvalid addres &Re Condition

LEfT[HE AO]= HEA0


oiee indicote cn empty sub Tree.
THRE ADED BINARY TREES: T 'nkod Representatim ot
bmuy Tree,ahbrorimatety Aatf of the enties (n th
Potey fie lds LEFT & RIGHT LJi Contain null eemnts.
used by uplacimy the
space may be More cfficiently
hull. enties by some ofey type information. o Yehlace If
ohich Point to
Certain null ebies by special Pointerb Poimters
nodes higho n
the tree. These shecial
Called & binay Tree wite Auch Pointers
reads'.

are Called Theaded BnyTee'.


The theods n a diacon of Thhecdod Tree ae laly
ndicated by dotted Lines.

In Comþuter memoy, an exta -bjt TAG field May be wsld

to distinqùh tbends foom ordinory Pomtens.


Thiads may b denoted by negtive tegens whn
rdiny PoirteS are donoted by Positive iteges.
hee ae mony ways to tiread a Bimcry Tree
1nne way Theading of Tree, atfread
aPpeaT
nhe mght field of a node & uie Pobnt to tee
oe
neot nde fe inoden avesal of Tree.
n

Al

K
(One twoy Threading)
n TWo LWay Thecding ot ree,atfead o aUso
(h-the LEFT fietd od node & oiQe point to The Pecedino
node n
the inorde aveal of Tree.
[A

D
Tiwo- way inordenThveading)
The Left Pon ter of tee firt
node & the ignt Pointey
Oftthe tast node il
Contuin tee nuee'yaliie
when Tree does not have a headen node, but ie
foint to tte keaden node when T does fave a tndenod

HEADER NODE

LAl

YD

(Tuo-ytireading wHG
headen node)
HUFFMAN'S AL(oORITHM:
In any Q-Tree tfe nnben ot eterncl nodes (NE)
uOne more t6an te nunben o iternal nodes"le.
NE = Nyt1)

Heve Ni= 6
NE = 6+L

The exernae Patf Lenate Le ot aree be the sun


C† all Perth dentRs pmed Oven each Patf. from the
Jlut R of Tee to an Ctesinal node.
LE = Q+2+3+4 +4t3+3 =21
The ntemal Pate Lengte
Nodes , |-e L cd 2-Tree wsing bnterhal

LI= 0+l+|+2+3+2=9
Here
LI 2n= 9+Q.6=9t 12 =1=Le
+

here n=6utAe nternol nodes.


no. of

fomula: LE = L +2n
whese n-inteynal nodes tue
s frany-Tee.
Let T Us a a- Tree LoiH n- exterhe nodes Å subþohl
tRe extevnal nodes iy asined a (hoh
gative)
Coch c

Patf, Lengtk P of the


Loeight. The extenal weighted
to Re
sum cf the weighted Patt
ee T us de fined be

lengts. te.
+ Wnn
P=WtWyly t
here wi= ueight Of n external hode
li= fate length of an etevnl node.

P=2.2+3.2+ 5.2+11-2 = 42
P =-2+3-3t 5-3 t 2.1
2] 3 = 23+3.3 +5-2.+ I1-1 =36.

LC) T3
Huffmon
Aloonthim: Hutfman
de fined in tem, of Algoithim us recUs
vely
the numbN Of
tRe solution for one Lseijht weights and
One node. On tthe
Jy smply hree wit he
otfn hond Hufman alqorthm
Constucting te tec fom the bottom up
tion fom the the
toh don.
Mutman olganithm gensoted a aiabl
Jength cade in A a cy
Hot hughen
efesen ded higharno.
d hids.tere he ymbal
cortucked de
is

ALnORTHMi SþOse W,& wg ae Tuoo minim um Loeights

anong tfe n qiven weights ws,wWa--- Wn.


ind a Tree T' wtich give, a salution for the n-1

weighta
Wt W2,W3, -

Then, Tn the Fee T'. suh lace tee external

W tW2 bythe subtree

Ihe neLJ 2tee T use desred solution


IMPLEMENTATION:
C Subpose A,8,c, 0,E,f,G, & H aL 8 data tems & csipr
Weighta as fallow3:
Oata item: A
weight: 22 5 l| 13 2 1| 25 5

la)

(6)

(c)
(d)

(2)

Ie) (-f)
3
(12)

l)
(-k)(FmaTree)
To imhlemnt the
Hutfman algoitGm,oe
A AMay wT to holdthe eights Yeyure an
of nodeS. 1:e Tree
martained by fow Poarllel
INFO, LEFT, RI Arags,
GHTAWJT.
INFo
E) The abone Tree Jubresnted n mmry as
WT LEFT RiGHT INFO
1
A 22 LEPT RIGHT
2 5 1
A 22
3 C
C
5 oo
19 )

2 D
6
7
2
. Q5 ooooo
6 ooo
8 H 5 o oooooo 7

H 5
lo

J2
5 2
22 6 3
oo
13
31
44 4
J6
56
106
|2
)3
AVAn =9 14

Matin a
5,
RooT=1
AVAIL=6
minfeab, ohee
& each node
te
locrtion cfthe olooi of aClnt Contaby
subtree.
wt
5TEPS: GO Remove tee node N5]&
estion)

Ng
-watien
=(5,2] boy
. the

(0 Firt Available ace AvALL9,& Set dota Koe as

WT= 2+5= ,EPrLa1= 5 , RlGH7 [93= 2


Thls NI u Letchild & N a Right child of tenehod
() AdjoIn tee
to tEe
Reap.
weight d Location of tehew nodu e L7.I
RehCating the abore
& Set the RoT, SInce
Sters
il1e Reab usemfty
+Ru y the
node added loCath n of Last
to tte Tree.

CODING: Coch edqe frm


child s labeled by
on
nternalnode to Jet
te bit o &
each edge-to tee t
ight Child is Labeled by tee bit 1. Thu
Hufman
Code cssigns to eoch extemal nodes
Segen ce te
O4 bits fom Ropt to the hode.
Hee H=lloo

C-0||

Hence Code Ae
A: o0
C:o| O: I
E: llolo f: 6lo h: lo H: l160

You might also like