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

4 Trees

Tree
Non lina data Atratae
Nedes aranged in beachiad manr

oet f. Trae A t r e can


hare atmet not
Sibling
Exteenal
Chidren
nodesavesA
Care
node har
paent
no children
Intena ne n hac one o more children
AnceStor - Descendant
A node pce an ancestor Of node it there
exik
trem root to and P appears
the path.
Qncestors of k
Edqe - ink rom parent -to child
Path Ay two ensesuire nodes in the
form an edge
Lerel Eoot is at lerel
Depth lergth ot tte poth from root to nod
es! depthC k) =3 depth CH) = 2
Height - lengt of the path from that nede to the
nod
és beight (K) eo he'gh(cJ - 2
he'ght (A) =3
Mar. hag ht among all hodes
in freL
Depth of a The max depth nong all node
Ske T r e e I erey node in a tee hoe
onechild Cexept aat )
Lett ckew Trae
Qrdeed Tree among Th chide
inca orde
Meaningtu
et each nod
Book
PatB
aet A ch9

Binay Trcs
nec he atmg tuwo children
Erer child or nghtCab
Each chid is either let
2. Ex: 0)oot
frrper
lett 8T
tree

Types binay tre


LStrict BT - i4 each node has axacty tuwo
children er chijdren
2. Full BT Fach nede has euacy
two chden and al cat nade
aee at cone kerel.

3. Compleke BT
all the lerels ot tpee aue flled ompietey
exeept lous est rel nodes which ae filed
trom þasibe .

L. Na. ot nedes in tull BT =2 I


2. Ne o nodes n
minimum
omple BT ht
manimum 2
8. No.ot leaf ncde in tull
BI= 2h
Dg- ho. of etenel nades
5. No. obN inenal
links in ompete BTsta nces -nt
Appliahon e} BT
I.epreCI9n a in
ompilern
2.MetmAn coding
Node checte 3 BsT- Seaech ince
deehon in O(loge
data|
pointes to lett Chidren poinhe to nght chilarer
Represenabon
Lin ked
et BT Paten
stnuctue
2.
ray repreentahan. elenaF

eX þresion

4 2
2
4
|2 13 l4
- Tnsest lDedetel
Seasch/Traverge
Tree ravercals
L. froceA o; nihing all
2.There aue nedes eh a tre’Tnea Trare
trarells
’he orde Rst order
Pre crde Jraetal ’Ih oder
L V'it the oot
2.Trarase lett sMbree io reorde
3.Travece nght ciebtreîn
prede
Prcorde I2 4 5 36 7

InLerorde Trareucal O(n)


1Trorerçe lettrWbireein inorde
2. Vit t roo
2.1raverge the nght uot tee in tn orda
INordeQ 4 2 5 I
63 7
in pecordeu
IIrarerse tubtre
a.ravàrse nghttubhre in pesherck
3 iáit he
Toshorde 4 5 26 7 31

Breadtn- Fitt Tree Iraverrod.


e
L Vicit all po&ihons at depth d bcfore
Called
iaiF þO&ihons at dapth dtl It ic
breadtn irt ra vaal.
BFTravei Cal | 23 4 56 7

EX Jnset 7,S,S, 3, b,0,9. 4 2


Eind Toordco Freorder - Fost orde Traverlal .

tplications of Tree Travesa.


lo ceae tabe f conten ts preerde tareal.
2. t£renthete representahon af a Tree

ioreenel nedes aee direchaics. Leg.ndeauc fieu


Anthmeic ex pression Tree
Binay Tree astoaated with an aeithemahc eprerston
’inteenal nods aperater
’ tenal node operande
Ex: (2x Ca-) + C3Kb))

2
to orde TaveSal - fo pint
aerthmethe expeseion.
Exp ressiOn Tee Erdeaton
(C2x(5-1))+ (3 x2))
poctordei
* Speialiahon o Traversl
2
5
S teps
Recesive methcd etneung- the Value of c ubtree
L
inhunal hcde , combine th
2. When vsahng- an
Yolues ek srbtrees
torihm evet Expr(v
retnun y.elementl)
elce
nt eral Expr (kettl)
yé eal Expr (nghrC)
D< Opejor Rtpred at V

Buileing an eKp reaon Tre 0lh)


Steps
Cperaor Puth ino Aace
2: AleLal roueV creahe sainge nod exprerOn
tree T dtoning V and push T onO ktacle
3 nqht paeentheaik pop 3items trom atacC
lorstrch tree eing E E2 and root o
Then psh reuehng. Tree inte atak
4 Lett paenthANs aue ignered.
Example
(cC3+1) x4)/CC9-5) +2))
Scanned chaace staek

4
b

1
Binay seach Trce tollowing
trucluee wn the
a binaey tree data
Jt
coneins only nodes wi
tree gf anode
7 kettsus nedet kay
lessee tnan nades wit;
nade. conhains nl
kub tre ef a
2 i¡ht greate than nde's kly
eacn mtt alo be
3 att and Righ subtree
binoly scarth tree (Key)

Search , Ihçeat, Delehe


Search
Alqcnlnm seach«)
K. and Value ot root
root,
element found.
elseif X<not.valu
Search iD not: lett
else is
Seach rnet ight
3. Repeat 2 till no more traresal u boeible
4 return elemert not foundit travere
ex:
reache leat nede
Search 4
4 <ß ’ seach in af subhec
4 > 3
ngb
4< 6 lett
4 = 4 ’ £leneht found.

-tnsat
iqorih m insetlx)
with root lue

move to letteub tree


elee
move to nghtsublree
lcat
nade
bad en loat node'k ve.
2. One the ef Bi3 6 ,o 9,4 2
to nght or
7,5,l
BT

In(ett 4

Inget 2

4oncruc BT ’ 30. 40,24,58, 48


,26, ,13
Practiee
44, lI,88&, 32,28 ,65,47, R2,93, 54,29
76, Bo ortrct

Delete
Cacel: Delete a leat node o B6t
smply remeve it .
deleke
25
4o 60
Case2 De letenede twin kingle chiad.
lopy the child to the nede to be deleta
then delee node

deleke
20
4 (bo
ase Delete node witn tuo Gidren
Fird inorde suceercor
thet ontent |.Inorder predecesco
wih tne noda to be
then de leke ncde deeted
delete
30

In orde (uc

You might also like