Trees and Programs
Trees and Programs
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
and
Level 2
mX!
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
|leit field
ink
)
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 )
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
Alqorithm :
ptr: Yo 0t
0f (ptr! = NVLL)
post orderC ptr > Lc)
post 0rder CptyR)
visit Cptr)
end if
St op
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
L
AOGrithnn.
whilk [ptr! =NvtL) na (1ag::
end while
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
t2rn>ptrial.
Diiiptr
Énd if
i (ptr ! ’ daae iterr)
3. Deleting Data:
()
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
33 45 42 75
2: noYdey tYOwersal!
20 24 27 24 33 35
(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
indegree Outdegre e
2 4
VÊ :
Ve 2
i
lqeithm to
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)
inclvi
the
k= 12345
K2 3 4 , K3=5
kÊt k2+ K3
S
51
HCI23 45)= 51