0% found this document useful (0 votes)
23 views21 pages

Trees and Programs

The document discusses tree data structures, focusing on basic terminologies such as nodes, parents, children, and levels. It explains binary trees, their types, and methods of representation, including implicit and linked representations. Additionally, it covers tree traversal techniques and operations like searching, inserting, and deleting data in binary search trees.

Uploaded by

chaithanya
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)
23 views21 pages

Trees and Programs

The document discusses tree data structures, focusing on basic terminologies such as nodes, parents, children, and levels. It explains binary trees, their types, and methods of representation, including implicit and linked representations. Additionally, it covers tree traversal techniques and operations like searching, inserting, and deleting data in binary search trees.

Uploaded by

chaithanya
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/ 21

Trees

Into:
AYu's Gtacks, Queues and inked List whch ae knoun
QS da StrUCtureshese are caled inea becctise the
lements are autanged in linear order Another VÇ ry uiet
data situtuTe is trees, where the edements are annnqed
in non-lineay order. Irees belons to non- linear datn
structures. tq of such xepreseniation s as folows

Tree

Basic Terminölogies :
1: NOde : This is the main component of ans tree struCtuYe
Ihe concept of node I5 sume as iinkcd tist AnCde
0f the tree StOres the atual daua and inks to the
Othey node. he strUCUre 0f a node is as follous
lett dlaua right}
iink type link

the immediate
2: Parent: ine Parent ot the node is
pyedecce ssor Ot a node

node are (ailec s


3. Child: the immediate suCCessoY of a
child ncdes.
the. tree. left chid
4 Link: Thissa pointeY tO a node. in
ncde
and \ght child are twe links Of a
5-ROot' Ihus 1e a cprialy diesignaved node which has nt
PCnL
not
(t the end , Luhich cioes
A leat
hve (ny childrern s caled leaf node
nede S also termd as terminc node.
Level : levei U lhé ynk in hpyaYchu The YOOt onoie

and
Level 2

8 Height : he mcximum no of n0des ie possible in a patn


Starting from a rOOt node to a leaf node
known as heiqht 0t the tree.

mX!

from {he (bcvé. tree , longest path u X, Y, M, N


nd
hence heiqht of tne trel iu 4.

q Deqiee:The masimum po0t childyen that is possibl2 f0r


node 14 knOwn as decYQe.
IO' 5iblings: The nodes which have the same parent ae calia
siblings
Deinitipn ot tree:

nDdes are paititio ned into disjoi ned Ge t t)


l) tn are culed sub tree5 of

nCm tne abOve eKCMple Sample tree, th00 avo s01 r l) na


Heye A 15 a Specia hode Caled To0t node. of tree
Tenaining nodes ave Partítioned into 3, $e15, I ; .
where thege are sub trees ot the goot hode A
B{nary Trees :
A Binars tree is a specia i0TM Of a tree.. A binary trec
more important and frequentls used in ariOUs applicatit
4 COmputer sience. . Like a qenera! iree, a binary tt
coun aiso be defined as finite ser of nodes such tnat
1: Tis empty Ccued the empty binary tree)
2T Contains a specialls designated node caled ihe roi
the tree and Yemaining nodes o the ttee Can icim a:
MOSt 2 dis joint sets T1: ad l . hich aye Caued the
Ieft Sub tree and iqht suo tree
"The mun diffey ence blw a tree
and a binary tree are
( A trec can never be empts,
bt a oinary tree may be
evnpts:
9 Ln CoLse of biny tree, A node Binare
Mauy hCWe at most 2 childrens, whereas incas cai!
a noae may have ny no 0f child1ens
Full binars tree
2. Complte binary tree:
hnay te c ia Said to be complete binary tee u
ktie ¿kcept possibly in the last
lst level have. the raximm
I nCdes nt ! the nodes in the last le vel cppeYs
e ft

(Ompleie binary ttee


Representation Of Binary trees:
Thre AY2 OMnNan methods foy gepesenting a binary tre e.
i: IMplicit appYOah cled linear o
Gequential yepyes enton
2 Expicit appn Cach cled Tinked
POnirs o1 links epresentations usina

1. LineaY D) 5Qq uenciau


epieStatIon Di a Binau trep:
lhis pe i YeDYCSentilon s Stoic mehod in which a
blcCk oiC memorr (01 n auras is aiocated before stoving
thi aciiial ttee. Once. the memory b alocaied , the Si
4

Adyantages:
-ting index
2. Heye. only the dat u GtCred without 0ng pomt,C
D0Saavantaqes:
I Dinei than tne ful binary tree, the mcujoríts 1 iy
e ntries moy be empty.
9. 7t ulows ony statiC Yepresentaion
3. 2nsertion and deletion Opeatons are ineticient ith tr
Tepresentation

2: Linked Tepresentaion of a Binory 1ree:


Ihe strUUUre 0f a node thau is
iQtion is as folloS:

|leit field
ink

iink fisi iu4tA


He1e le ft link and riqht link aYe the two
store the addyess 0f le tt Child and iaht chíid
Data is the inforarion content bf the node
with thís epresentalion, if we tnow the a dies, ct ore
node then is easy to asCess the address Ci 6nCir.!
node.

)
Tree 7aver5al:
The traVersrl operaion b frequen tly usod Opert10n n
binary IYee. ihu pperuion u used tC visit ech arnd
tree
here ce 3 tundamnta trave rscu Lechniq ues thl are
pOssible .
PrC OYdey traversi (R,Tr)
2 Înoder traversal
(IL, R, Ip )

P+e oTder aversa:


1CCl
L5 triC Ul hen the Yiaht node s Visited first, then the iejt
sub tree. 1he DIe o1dei
s deiined as traveras
follos.
Sicp i Roct node.
Step 2 TVer5e he left s ub tree
st ep 3TCverse the Yiqht Sub
tree
Alqori thm: eq
ptr= sOOt
A
if (pti! - NULL)
Visit (pt)
pre O1der (ptr > LC)

End if
Stop

2. înorde îraversa:
ln incrdei trversai, firót lett
n0de and then Yiqht swbtree u
visited, then yoot
Sub tree u
Visited Inorde trawersai
StepI: traverS2 1et Sub tree
Step2 visit r001 node.
Step3: traverse. Yiqht Suo
tree.
Algozithm:

i| (ptr- NUT)
inoszler (ptyL)

inordeicptI R)
SLop

POct ovder TraverSal!

and then lhe Yo04 hode In lhu trm yi/(ai


Yiaht s ub tree
oOt node is VI5Ied a last, Po5t 01de rayerSA) .
the
delined as follow5.
Sub tree
Step I: Irayerse le(t
Step 2: Trave rse qht sub tree

S1ep 3 Root n0de

Alqorithm :
ptr: Yo 0t
0f (ptr! = NVLL)
post orderC ptr > Lc)
post 0rder CptyR)
visit Cptr)
end if
St op

ipre o1dey: 1- hBAC |EF


in 01der: A- B+c* E(F
POSt Order: AB- CEF +
hat P i o mcn a binar seach ree ae

11ting dara
- e r s i n g the tre
I. StaYChing daia:
Stacing dta in a b i n r 5eYcn tree is much iaste tnan
spp0se n a binry
$tart <5eNTcMa
hen search n 1ght sub tree OY if he i t n is i:3s
TCdi
tnn T001 node tnen serCn in est sub treC Thú pSI
uiH Di tinued intil the item s fvnd oy

thi Dinars SearCh trec:

let sarch for th.


¬ierment : 4 4

L
AOGrithnn.
whilk [ptr! =NvtL) na (1ag::

end while

print item ngt ioud


end it

2 nserting data :
operaIOn on a binaru siYch ire 15 VE
ne insertion moYe than tn sATCNn
iniact One step
Simple 2t is witih data inio a reE.
a node
bpiroi On TC insert Searched siartina irem ^
be. she
tree is Yequired to tound. inseYtion s not p0s51c
is
YOOt node I| item inerted. The foilowina ia
Otherwise iterm is to be
the insertion of a new node
Yepresents

Atei insitin
ptri= pir

P7in: "item 2xísts 4

t2rn>ptrial.
Diiiptr

Énd if
i (ptr ! ’ daae iterr)

3. Deleting Data:

if tt xist in the tree


ce
On tn no Cf us chiiden. Hen
CUse !:

Caie 2: N s éXactly 4 child


Case 3: N nas 2 Childien
node-2

()

Befoye deletion

node 2s

(Tö)

Atter deleon
Before
dledton

node-35
Case 3:

15)
(4)

node- 24
ls Traversing
Abinary search tree Can be traversed in 3 ways.
i: Pre cdey traversal

3. PCSt order ttaverGa


Pre OTder TYOversa:
xplanaIcn
Ugorithm

33 45 42 75

2: noYdey tYOwersal!
20 24 27 24 33 35

3- Pos DTde ttauersa:


C ? 24 33 24 20 42 75 5 35
pr 1eiminologies

(urtiectrd qo

Ch(hVs).

tights
(weignred'
directed qraph)

4i Adjacent veYtices:
A vertex V djacent to unOther ex
VeYteX Ví it there.
is an eclqe frorm V; to Vj

Foy Vi = V) nd Va aYe
adjacents
Vs - n0 adjacent sides
Vs =V, V4, dse adyacents

5- 6elf Loop: ]! theve an eclae whose Siarting cnd ercung


et
vertexes Cre 5ame then tt is ccuied a

there is more than one edqe between


6. Paralel edges: L vertices is coled poralel
SCLme pair of
edes

qiaph: Aq1aph it u doesnt hwe. anu sels loop or para


7Simpte <imple araph:
Qda es then it is caled
complete iu Qach
B. Cormptete qraph: AqTaph G is said to be
vertex v; s adjacent,to every vertex y
L0 1s0lated veYtex:
A ventex is iolte if thee 1s no edae ena
any other vextex connected ytom

u" degee of vextex:


The no. tf edaes conneted with the vestex V;
Caled-lbe
degree 04 vertex
From fiq
deqree 0f V; : 2
undireted
qiaph
dearee 0 Va 2: 2

for a uYeced qraph there are twe degiees


ie incdegnee Out degiee

indegree Outdegre e
2 4
VÊ :
Ve 2

12. Pendent vertex:


indegyee - 1 and Cucteqie C
tS
AvezteX VË is pendent if
caled as
pendent ve xAex.
type ot Vertex is
tlch
S

GRAPH TRAVERSAL: the vevtices in the.


qraph means VISiting al
GUNcdard meThods tt
YVerS|ng a Theye are 2
q1aph exactly chce
onc .
trawerse, a qraph.
search (BFS)
first
1 Breadth Seaich (DF$)
9.Depth fiYst
VVavsVVsiv

i
lqeithm to

miti hode qrafh lhe Dis


lear node OY Lh
node
alwilhqcathm
nC
ln11h)). he Pr0e
cs
oOplerne niinq thiid?r.

vetex and mare t a visited

unvisited adj acent


(oY)
veriex OB p ard dispny
cnVisited acjacnt Vertex 0f p, then pop
I: no
Endwhile

StOP

V.

(OY)
Hashin:
unshing s technique or PTOCess O maPing he keys anå you
into the hash table by usih aa hashhash funeion
funHOn. t i dore
foT faster aceeSS to elements The estiiercy ot mappinq
epends Gon the tne etficiency o hash funuion used
Ler hash tunttion HCk) wheve hu hash tunton and kisk
maps the value k at the index kyn in a hash tosle.
HeYe n s si 2e Of the tabte.
FoT cq: The ist D keus are l, (2, 13, 4, 15 will be store
at positi ons 1, 2 3,4,5 in the hash table Ye speciey

HC = k mod n
à
tables mnne.
dara strucuyp, whjch stove o a n
HashSOCte mat
aYYCy
fOr t(lble data, is SDYEd
ownanlnique índex
in
yeryt a tIf we Lnow volue
the index
dea Volue hs ts
Accescing
of the desired dasa.
of dua
an beomes
Sh
iable
uSes.
WseS
Ha hashteg
array toas
technique a
stOIage Medi unn and
n elenent
be inceTted: qenerate an .ind ex uhere
Hashing technigç COmpaisíons
seThesthe
which usey les key
elernent in
andandin
olN)
oCN)ime in the uorst
Case. an awerC4e Case
sime.This metho
the keys qeneraty
Lnto a
uses the hash funcion
to map tble which u caU ed a hash
Fatle.
HOshtuncHion:
Hashtunchfon is funtions which.
it produces an, integer,
is applied ona key
bywhich it
hash tahle whfch tin be used
t an address of
one, Cari use he eme hah tuncion 0 aScessina
Hence
the dosa and &toring bhe dCa çn. hosh taole.
function .
Tupes of Hash
AYe vayious typesx Dt hash tuncHiOns which are
The
piace the data the hash table. They are
uwd to
vËsiDn nethod
division | modeuar di
1 Mid square rmethod
3 Folding method.
diisior Method:
eA iest nnethed,to qenerat
the most simple and diills the Value
Tha i hauun ction
rhu
hahh value.
Demainder ob teuneed.
t by n and uses the

KMod N
fomuda! HL) 2
Here k: key volue
Of hcsh table.
N si2e
k 34c, H(34)

2. Mid gcquare met hd i

inclvi

¬q' CQuare the Value o k


? Ext)at the mt ddle digits o the hoh vaue

Here k key Ualue


midcle dígirs con be' dè cided base on ne si
The
Of the tale
Suppo&e the hethtaole: hax 0g memory !9tai
fon so two digfdsre requred wi th ap the
Vey. tO the memo ry Lo Cation
12t k:60 H (60) 60 x6o
= 36 0D

3. Diqit folcing mehod:


Thu method invo lves 2 Steps:
t. divi de the key vaUue. a k nto
a

S tad tho individua pcrts

the
k= 12345

K2 3 4 , K3=5
kÊt k2+ K3
S
51

HCI23 45)= 51

0f uqTs tn eacath pait varies


The no- ot the hauh table.
the
eíze
ppore eq he síze Df hash dependi
table iu the
ng
part mst Owe 2 daits
l00 ech

You might also like