+ All Categories
Home > Documents > unit-IV -

unit-IV -

Date post: 20-Feb-2018
Category:
Upload: mayank-yadav
View: 217 times
Download: 0 times
Share this document with a friend
139
7/24/2019 unit-IV - http://slidepdf.com/reader/full/unit-iv- 1/139 | Jul 2012| © 2012 UPES UPES TREES
Transcript
Page 1: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 1/139

| Jul 2012| © 2012 UPES

UPES

TREES

Page 2: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 2/139

© 2012 UPESJul 2012

Jul 2012

Contents TREES: Introduction terminology, Tyes !"in#ry, Comlete "in#ry,

E$tended "in#ry, , T%re#ded "in#ry Trees&

 'lic#tion #nd (er#tion on trees)

 'lgorit%m: Imlement#tion #nd Tr#*ers#l "in#ry Se#rc% Tree: !"ST&,

Insertion #nd +eletion in "ST

m-#y Se#rc% Trees,

" #nd ". Tress, '/ Tress)

Page 3: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 3/139

© 2012 UPESJul 2012

Jul 2012

Trees

 'rr#ys, st#cs, ueues #nd lined lists, #re no3n #s line#r d#t# structures)

T%ese #re termed #s line#r 4ec#use t%e elements #re #rr#nged in # line#r5#s%ion !t%#t is, one dimension#l reresent#tion&)

 'not%er *ery use5ul d#t# structure is tree, 3%ere elements #e#r in #nonline#r 5#s%ion, 3%ic% reuire t3odimension#l reresent#tion) 's s%o3n in5igures)

Page 4: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 4/139

© 2012 UPESJul 2012

Jul 2012

Trees : "#sic Terminologies

Node - T%is is t%e m#in comonent o5 #ny

tree structure) T%e concet o5 node is s#me#s used in lined)

Parent - P#rent o5 # node is t%e immedi#teredecessor o5 # node) 6ere, 7 is t%e#rent o5 8 #nd 9)

Child - 'll immedi#te successors o5 # node#re no3n #s child ) or e$#mle, in t%e

5igure, Y #nd Z #re t%e t3o c%ild o5 X )

Link - T%is is # ointer to # node in # tree)

or e$#mle, #s s%o3n in igure 1, C #nd

RC #re t3o lins o5 # node)

ig)1 Structure o5 node in # tree

Page 5: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 5/139

© 2012 UPESJul 2012

Jul 2012

Trees : "#sic Terminologies !cont))&

Root - T%is is # seci#lly design#ted node 3%ic% %#s no #rent) In 5igure, A is

t%e root node)

Leaf - T%e node 3%ic% is #t t%e end #nd 3%ic% does not %#*e #ny c%ild isc#lled le#5 node) In igure, 6, I, ;, , #nd < #re t%e le#5 nodes) e#5 node is#lso #ltern#ti*ely termed #s termin#l node)

Level - e*el is t%e r#n o5 t%e %ier#rc%y #nd root node is termed #s in le*el

0) I5 # node is #t le*el l t%en its c%ild is #t le*el l . 1 #nd #rent is #t le*el l =1) T%is is true 5or #ll nodes e$cet t%e root node) e*el o5 t%e root node is>ero) In igure, le*els o5 *#rious nodes #re deicted)

Page 6: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 6/139

© 2012 UPESJul 2012

Jul 2012

Trees : "#sic Terminologies !cont))&

Height - <#$imum num4er o5 nodes t%#t is ossi4le in # #t% st#rting 5rom

root node to # le#5 node is c#lled t%e %eig%t o5 # tree)

or e$#mle, in igure, t%e longest #t% is ' = C = = J = < #nd %ence t%e%eig%t o5 t%is tree is ?)

It c#n 4e e#sily o4t#ined t%#t % @ lm#$ . 1, 3%ere % is t%e %eig%t #nd lm#$ is t%e

m#$imum le*el o5 t%e tree)

Page 7: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 7/139© 2012 UPESJul 2012Jul 2012

Trees : "#sic Terminologies !cont))&

Degree - <#$imum num4er o5 c%ildren t%#t is ossi4le 5or # node is no3n

#s t%e degree o5 # node) or e$#mle, t%e degree o5 e#c% node o5 t%e tree #ss%o3n in t%e 5igure is 2)

Sibling - T%e nodes 3%ic% %#*e t%e s#me #rent #re c#lled si4lings) ore$#mle, in 5igure, J #nd ; #re si4lings)

Page 8: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 8/139© 2012 UPESJul 2012Jul 2012

Trees : "#sic Terminologies !cont))&

+i55erent te$t use di55erent term 5or t%e re*iously discussed terms =

 = depth 5or %eig%t

 = branch or edge 5or lin

 =  arity 5or degree

 = external node 5or le#5 node

 = internal node 5or node ot%er t%#n le#5 node

Page 9: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 9/139© 2012 UPESJul 2012Jul 2012

Trees : +e5inition

et us de5ine # tree) ' tree is # 5inite set o5 one or more nodes suc%t%#t:

A !i& t%ere is # seci#lly design#ted node c#lled t%e root,

A !ii& rem#ining nodes #re #rtitioned into n !n B 0& disoint sets T1, T2, ) ) ),

Tn, 3%ere e#c% Ti  !i @ 1, 2, ) )), n& is # treeD T1, T2, ) ) ), Tn #re c#lled

su4trees o5 t%e root)

In t%e s#mle tree T, t%ere is #set o5 12 nodes)

A 6ere ' is # seci#l node4eing t%e root o5 t%e tree)

A Rem#ining nodes #re#rtitioned into sets T1, T2,

#nd TD t%ey #re su4trees o5t%e root node ')

A "y de5inition, e#c% su4tree is#g#in # tree) (4ser*e t%#t #tree is de5ined recursi*ely)

ig) ' s#mle tree T

Page 10: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 10/139

© 2012 UPESJul 2012Jul 2012

"in#ry Trees

 ' 4in#ry tree is # seci#l 5orm o5 # tree) ie3ise # gener#l tree, #4in#ry tree c#n #lso 4e de5ined #s:

A  ' 4in#ry tree T is # 5inite set o5 nodes, suc% t%#t

A !i& T is emty !c#lled emty 4in#ry tree&, or 

A !ii&T cont#ins # seci#lly design#ted node c#lled t%e root o5 T, #nd t%e rem#iningnodes o5 T 5orm t3o disoint 4in#ry trees T1 #nd T2 3%ic% #re c#lled le5t su4tree

#nd t%e rig%t su4tree resecti*ely)

ig) ' s#mle 4in#ry tree 3it% 11 nodes

Page 11: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 11/139

© 2012 UPESJul 2012Jul 2012

"in#ry Trees

Full binary treeA  ' 4in#ry tree is # 5ull 4in#ry tree, i5 it

cont#ins m#$imum ossi4le num4er o5nodes in #ll le*el)

Complete binary treeA  ' 4in#ry tree is s#id to 4e # comlete

4in#ry tree, i5 #ll its le*el, e$cet

ossi4ly t%e l#st le*el, %#*e t%em#$imum num4er o5 ossi4le nodes,#nd #ll t%e nodes #t t%e l#st le*el#e#r #s 5#r le5t #s ossi4le)

Page 12: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 12/139

© 2012 UPESJul 2012Jul 2012

Proerties o5 "in#ry Tree

In any binary tree, maximum number of nodes on level l is 2 l ,where l ≥ 0.

Proo5: T%e roo5 o5 t%e #4o*e lemm# c#n 4e done 4y induction on l) T%e root is t%e onlynode on le*el l @0) 6ence, t%e m#$imum num4er o5 nodes on le*el l @ 0 is 20 @ 1 @ 2l)

Suose, 5or #ll i , 0 F i G l #nd 5or some l , t%e #4o*e 5ormul# is true, t%#t is, t%e m#$imum

num4er o5 nodes #t le*el i is 2i ) Since e#c% node in # 4in#ry tree %#s degree 2, so 5rom

e#c% node #t le*el i , t%ere m#y 4e #t most 2 nodes #t le*el i . 1) T%us, t%e m#$imum

num4er o5 nodes #t le*el i . 1 is 2 H 2i  @ 2i .1

T%ere5ore, i5 t%e 5ormul# is true 5or #ny i, t%en it is #lso true 5or i . 1)

6ence, t%e roo5)

Page 13: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 13/139

© 2012 UPESJul 2012Jul 2012

Proerties o5 "in#ry Tree

Maximum number of nodes possible in a binary tree of height h is 2 h  !.

Proo5: <#$imum num4er o5 nodes is ossi4le in # 4in#ry tree, i5 m#$imum num4er o5nodes #re resent in e#c% le*el)

T%us, m#$imum num4er o5 nodes in # 4in#ry tree o5 %eig%t % is

Page 14: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 14/139

© 2012 UPESJul 2012Jul 2012

Proerties o5 "in#ry Tree

Minimum number of nodes possible in a binary tree of height h is h.

Proo5: ' 4in#ry tree %#s minimum num4er o5 nodes i5 e#c% le*el %#s minimum num4er o5nodes) <inimum nodes ossi4le #t e*ery le*el is only one 3%en e*ery #rent %#s onec%ild) Suc% ind o5 trees c#lled se3 4in#ry trees #re s%o3n in 5igure)

T%us, # se3 4in#ry tree is t%e tree %#*ing only one #t% 3%ic% cont#ins % num4er o5nodes i5 % is its

%eig%t) 6ence nmin @ %)

Se3 4in#ry trees cont#ining minimum num4er o5 nodes)

Page 15: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 15/139

© 2012 UPESJul 2012Jul 2012

Proerties o5 "in#ry Tree

For any non"empty binary tree, if n is the number of nodes and e is the

number of edges, then n # e $ !.

Page 16: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 16/139

© 2012 UPESJul 2012Jul 2012

Proerties o5 "in#ry Tree

For any non"empty binary tree %, if n0  is the number of leaf nodes &degree

# 0' and n2 is the number of internal node &degree # 2', then n0  # n2 $ !.

Page 17: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 17/139

© 2012 UPESJul 2012Jul 2012

Linear Representation of a Binar Tree

T%is tye o5 reresent#tion is st#tic in t%e sense t%#t # 4loc o5 memory

5or #n #rr#y is to 4e #lloc#ted 4e5ore going to store t%e #ctu#l tree in it,

#nd once t%e memory is #lloc#ted, t%e si>e o5 t%e tree 3ill 4e restricted

#s t%e memory ermits)

In t%is reresent#tion, t%e nodes #re stored le*el 4y le*el, st#rting 5rom

t%e >ero le*el 3%ere only root node is resent) Root node is stored in

t%e 5irst memory loc#tion !#s t%e 5irst element in t%e #rr#y&)ollo3ing

rules c#n 4e used to decide t%e loc#tion o5 #ny node o5 # tree in t%e

#rr#y !#ssuming t%#t #rr#y inde$ st#rts 5rom 1&:

Page 18: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 18/139

© 2012 UPESJul 2012Jul 2012

1) T%e root node is #t loc#tion 1)

2) or #ny node 3it% inde$ i , 1 G i F n, !5or some n&:

!#& P'RET!i & @ ⎣i / 2⎦

or t%e node 3%en i @ 1, t%ere is no #rent)

!4& C6I+!i & @ 2 ∗ i

I5 2 ∗ i B n, t%en i %#s no le5t c%ild)

!c& RC6I+!i & @ 2 ∗ i . 1

I5 2 ∗ i . 1 B n, t%en i %#s no rig%t c%ild)

Page 19: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 19/139

© 2012 UPESJul 2012Jul 2012

E$#mle

! A = B& . C !∗ DE &

Page 20: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 20/139

© 2012 UPESJul 2012Jul 2012

e$t uestion #rises is %o3 c#n 4e t%e si>e o5 #n #rr#y estim#tedK

T%is *#lue c#n 4e o4t#ined e#sily i5 t%e 4in#ry tree is # 5ull 4in#ry tree)

 's 3e no3, 5rom emm# 2, # 4in#ry tree o5 %eig%t h c#n %#*e #t

most 2h  = 1 nodes) So, t%e si>e o5 t%e #rr#y to 5it suc% # 4in#ry tree is

2h  = 1)

(ne c#n e#sily re#li>e t%e 5#ct t%#t i5 t%e tree is # 5ull 4in#ry tree t%en

t%ere 3ill 4e #n e55icient use o5 stor#ge in t%e #rr#y reresent#tion !no

#rr#y loc#tion 3ill 4e le5t emty&D on t%e ot%er %#nd, 5or # 4in#ry tree

ot%er t%#n 5ull 4in#ry tree t%ere is # 3#st#ge o5 memory

Page 21: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 21/139

© 2012 UPESJul 2012Jul 2012

<#$imum #nd minimum si>e t%#t #n #rr#y m#y reuire to store #

4in#ry tree 3it% n num4er o5 nodes #re

Si>e m#$ @ 2n  = 1

Si>e min @ 2 log⎡ 2!n.1& = 1⎤

Proof : I5 t%e %eig%t o5 # 4in#ry tree denotes t%e m#$imum num4er o5

nodes in t%e longest #t% o5 t%e tree , t%en 5or # tree 3it% n nodes, t%e

m#$imum %eig%t ossi4le is h m#$ @ n) Suc% # 4in#ry tree is termed

#s se! binary tree

Page 22: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 22/139

© 2012 UPESJul 2012Jul 2012

I5 3e store suc% # 4in#ry tree, t%en it c#n 4e seen t%#t t%e 5irst loc#tion

is 5or t%e root node, t%ird loc#tion !22 = 1& 5or rig%t c%ild o5 root node

!second node& Lt% !2 = 1& loc#tion 5or t%e rig%t c%ild o5 t%e second node

!t%ird node&, #nd so on) T%us, 5or # node #t le*el i Mit is #ctu#lly !i . 1&t% nodeMits loc#tion in t%e #rr#y is #t 2i .1 = 1) So, t%e l#st node !nt%

node& 3%ic% is #t !n = 1&t% le*el 3ill 4e #t 2n  = 1) T%is is t%ere5ore t%e

m#$imum si>e o5 t%e #rr#y) 6ence, Si>em#$ @ 2n = 1

o3, 3%en t%e tree is # 5ull !or comlete& 4in#ry tree, t%en 3e need

minimum si>ed #rr#y to #ccommod#te t%e entire tree) In t%e c#se o5 #5ull or comlete 4in#ry tree, 3it% n nodes, minimum %eig%t t%#t is

ossi4le, is h min @ log2!⎡ n . 1& ) In t%#t c#se, t%e l#st element 3ill 4e⎤stored #t !2%min 1& t% loc#tion) 6ence t%e minimum si>e reuired is

Page 23: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 23/139

© 2012 UPESJul 2012Jul 2012

Page 24: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 24/139

© 2012 UPESJul 2012Jul 2012

Linked Representation of Binar Tree

In lined reresent#tion o5 4in#ry trees, #ll t%ese o*er%e#ds #re t#en

c#re o5) ined reresent#tion #ssumes t%e structures o5 # node #ss%o3n in igure)

Page 25: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 25/139

© 2012 UPESJul 2012Jul 2012

#d*#nt#ge o5 t%is reresent#tion is t%#t it #llo3s dyn#mic memory

#lloc#tionD %ence si>e o5 t%e tree c#n 4e c%#nged #s #nd 3%en need

#rises 3it%out #ny limit#tion e$cet t%e limit#tion o5 t%e #*#il#4ility o5t%e tot#l memory)

Page 26: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 26/139

© 2012 UPESJul 2012Jul 2012

Imlement#tion o5 "in#ry Tree

4in#ry tree imlement#tion using seuenti#l !#rr#y& or 

4in#ry tree imlement#tion lined reresent#tion)

Page 27: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 27/139

© 2012 UPESJul 2012Jul 2012

4in#ry tree imlement#tion using #rr#y

!lgorith" B#ildTree$%

&np#t' ITE< is t%e d#t# content o5 t%e node I) ' 4in#ry tree currently 3it% node #t I

(#tp#t' ' 4in#ry tree 3it% t3o su4trees o5 node I)

Data str#)t#re' 'rr#y reresent#tion o5 tree)

Steps'

1) I5 !I N 0& t%en I5 t%e tree is not emty

2) 'OI @ ITE< Store t%e content o5 t%e node I into t%e #rr#y '

) ode I %#s le5t su4tree !Qi*e otion @ 8&K

) I5 !otion @ 8& t%en I5 node I %#s le5t su4 tree

Page 28: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 28/139

© 2012 UPESJul 2012Jul 2012

4in#ry tree imlement#tion using #rr#y

?) "uildTree1 !2 I, E-& T%en it is #t 2 I 3it% ne$t item #s E-∗ ∗

) Else

L) "uildTree1 !0, U& Emty su4tree

) EndI5 

V) ode I %#s rig%t su4tree !Qi*e otion @ 8&K

10) I5 !otion @ 8& I5 node I %#s rig%t su4tree 11) "uildTree1 !2WI.1, E-R& T%en it is #t 2 I.1 3it% ne$t item #s E-R∗

12) Else

1) "uildTree1 !0, U& Emty su4tree

1) EndI5 

1?) EndI5 

1) Sto

Page 29: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 29/139

© 2012 UPESJul 2012Jul 2012

4in#ry tree imlement#tion using lin list

!lgorith" B#ildTree$*

&np#t' ITE< is t%e content o5 t%e node 3it% ointer PTR)

(#tp#t: ' 4in#ry tree 3it% t3o su4trees o5 node PTR)

Data str#)t#re: ined list structure o5 4in#ry tree)

Steps'

1) I5 !PTR N U& t%en I5 t%e tree is not emty 2) PTRX+'T' @ ITE< Store t%e content o5 node #t PTR

) ode PTR %#s le5t su4tree !Qi*e otion @ 8&K

) I5 !otion @ 8& t%en

?) lctr @ Qetode!(+E& 'lloc#te memory 5or t%e le5t c%ild

) PTRXC @ lctr 'ssign it to e5t lin

L) "uildTree2 !lctr, E-& "uild le5t su4tree 3it% ne$t item #s E-

Page 30: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 30/139

© 2012 UPESJul 2012Jul 2012

4in#ry tree imlement#tion using lin list

) Else

V) lctr @ U

10) PTRXC @ U 'ssign 5or #n emty le5t su4tree

11) "uildTree2!lctr, U& Emty su4tree

12) EndI5 

1) ode PTR %#s rig%t su4tree !gi*e otion @ 8&K 1) I5 !otion @ 8& t%en

1?) rctr @ Qetode !(+E& 'lloc#te memory 5or t%e rig%t c%ild

1) PTRXRC @ rctr 'ssign it to Rig%t lin

1L) "uildTree 2!rctr, E-R& "uild rig%t su4tree 3it% ne$t item #s E-R

Page 31: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 31/139

© 2012 UPESJul 2012Jul 2012

4in#ry tree imlement#tion using lin list

Else

1V) rctr @ U

20) PTRXRC @ U 'ssign 5or #n emty rig%t su4tree

21) "uildTree 2!rctr, U&

22) EndI5 

2) EndI5  2) Sto

Page 32: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 32/139

© 2012 UPESJul 2012Jul 2012

(er#tion (n "in#ry Tree

Insertion To include # node into #n e$isting !m#y 4e emty& 4in#ry

tree)

+eletion To delete # node 5rom # nonemty 4in#ry tree)

Tr#*ers#l To *isit #ll t%e nodes in # 4in#ry tree)

<erge To merge t3o 4in#ry trees into # l#rger one)

Page 33: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 33/139

© 2012 UPESJul 2012Jul 2012

Insertion

 ' ne3 node c#n 4e inserted into #ny osition in # 4in#ry tree)

Inserting # node #s #n intern#l node is gener#lly 4#sed on some

criteri# 3%ic% is usu#lly in conte$t o5 # seci#l 5orm o5 # 4in#ry tree)

-e 3ill discuss %ere # simle c#se o5 insertion #s e$tern#l nodes)

igure, s%o3s %o3 # node cont#ining d#t# content #s Q c#n 4e

inserted #s # le5t c%ild o5 # node %#*ing d#t# content E)

Page 34: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 34/139

© 2012 UPESJul 2012Jul 2012

Insertion into # seuenti#l reresent#tion o5 # 4in#rytree !#s # le#5 node&

 'lgorit%m Insert"in#ryTreeSEY &np#t' ;E8 4e t%e d#t# o5 # node #5ter 3%ic% # ne3 node %#s to 4e inserted 3it% d#t# #s ITE<)

(#tp#t' e3ly inserted node 3it% d#t# #s ITE< #s # le5t or rig%t c%ild o5 t%e node 3it% d#t# ;E8)

Data str#)t#re' 'rr#y ' storing t%e 4in#ry tree)

Stes:

1) l @ Se#rc%SEY!1, ;E8& Se#rc% 5or t%e ey node in t%e tree

2) I5 !l @ 0& t%en

) Print ZSe#rc% is unsuccess5ul : o insertion[

) E$it

?) EndI5 Yuit t%e e$ecution

) I5 !'O2 W l @ U& or !'O2 W l . 1 @ U& t%en I5 t%e ey node %#s emty lin!s& L) Re#d otion to re#d #s le5t !& or rig%t !R& c%ild !gi*e otion @ R&

) I5 !otion @ & t%en

V) I5 'O2 W l @ U t%en e5t lin is emty

10) 'O2 W l @ ITE< Store it in t%e #rr#y '

Page 35: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 35/139

© 2012 UPESJul 2012Jul 2012

Insertion into # seuenti#l reresent#tion o5 # 4in#rytree !#s # le#5 node&

11) Else C#nnot 4e inserted #s le5t c%ild 12) Print Z+esired insertion is not ossi4le[ #s it #lre#dy %#s # le5t c%ild

1) E$it Return to t%e end o5 t%e rocedure

1) EndI5 

1?) EndI5 

1) I5 !otion @ R& t%en <o*e to t%e rig%t side

1L) I5 !'O2 l . 1 @ U& t%en Rig%t lin is emty∗

1) 'O2 l . 1 @ ITE< Store it in t%e #rr#y '∗

1V) Else C#nnot 4e inserted #s rig%t c%ild

20) Print \+esired oer#tion is not ossi4le] #s it #lre#dy %#s # le5t c%ild

Page 36: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 36/139

© 2012 UPESJul 2012Jul 2012

Insertion into # seuenti#l reresent#tion o5 # 4in#rytree !#s # le#5 node&

21) E$it Return to t%e end o5 t%e rocedures 22) EndI5 

2) EndI5 ;ey node is %#*ing 4ot% t%e c%ild

2) Else ;ey node does not %#*e #ny emty lin

2?) Print ZITE< c#nnot 4e inserted #s # le#5 node[

2) EndI5 

2L) Sto

Page 37: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 37/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m Se#rc%SEY &np#t' ;E8 4e t%e item o5 se#rc%, I+E7 4eing t%e inde$ o5 t%e node 5rom 3%ere

se#rc%ing 3ill st#rt)

(#tp#t' (C'TI(, 3%ere t%e item ;E8 is loc#ted, i5 #ny)

Data str#)t#re' 'rr#y ' storing t%e 4in#ry tree) SI9E denotes t%e si>e o5 ')

Stes: 1) i @ I+E7 St#rt 5rom root node

2) I5 !'Oi N ;E8& t%en T%e resent node is not t%e ey node

) I5 !2 i F SI9E& t%en I5 le5t su4tree is not e$%#usted∗

) Se#rc%SEY!2 i, ;E8& Se#rc% in t%e le5t su4tree∗

?) Else e5t su4tree is e$%#usted #nd ;E8 not 5ound

) I5 !2 i . 1 F SI9E& t%en I5 rig%t su4tree is not e$%#usted∗

L) Se#rc%SEY !2 i . 1, ;E8& Se#rc% in t%e rig%t su4tree∗

Page 38: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 38/139

© 2012 UPESJul 2012Jul 2012

) Else T%e ;E8 is 5ound neit%er in le5t nor in rig%t su4tree

V) Return!0& Return U #ddress 5or unsuccess5ul se#rc%

10) EndI5 

11) EndI5 

12) Else

1) Return!i& Return t%e #ddress 3%ere ;E8 is 5ound

1) EndI5 

1?) Sto

Page 39: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 39/139

© 2012 UPESJul 2012Jul 2012

Insertion into # lined reresent#tion o5 # 4in#ry tree!#s # le#5 node&  'lgorit%m Insert"in#ryTreeI;

&np#t' ;E8, t%e d#t# content o5 t%e ey node #5ter 3%ic% # ne3 node is to 4e inserted#nd ITE< is t%e d#t# content o5 t%e ne3 node t%#t %#s to 4e inserted)

(#tp#t' ' node 3it% d#t# comonent ITE< inserted #s #n e$tern#l node #5ter t%e node

%#*ing d#t# #s ;E8 i5 suc% # node e$ist 3it% emty lin!s&, t%#t is, eit%er or 4ot%

c%ildren is#re #4sent)

Data str#)t#re' ined structure o5 4in#ry tree) R((T is t%e ointer to t%e root node)

1) tr @ Se#rc%I;!R((T, ;E8&

2) I5 !tr @ U& t%en

) Print ZSe#rc% is unsuccess5ul: o insertion[

) E$it

?) EndI5 

Page 40: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 40/139

© 2012 UPESJul 2012Jul 2012

Insertion into # lined reresent#tion o5 # 4in#ry tree!#s # le#5 node& ) I5 !trXC @ U& or !trXRC @ U& I5 eit%er or 4ot% lin!s& is#re emty

L) Re#d otion to insert #s le5t !& or rig%t !R& c%ild !gi*e otion @ R&

) I5 !otion @ & t%en To insert #s le5t c%ild

V) I5 !trXC @ U& t%en I5 t%e le5t lin is emty t%en insert

10) ne3 @ Qetode!(+E&

11) ne3X+'T' @ ITE< 12) ne3XC @ ne3XRC @ U

1) trXC @ ne3

1) Else T%e ey node #lre#dy %#s le5t c%ild

1?) Print ZInsertion is not ossi4le #s le5t c%ild[

1) E$it Yuit t%e e$ecution

1L) EndI5 

1) Else

Page 41: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 41/139

© 2012 UPESJul 2012Jul 2012

Insertion into # lined reresent#tion o5 # 4in#ry tree!#s # le#5 node& 1V) I5 !trXRC @ U& I5 t%e rig%t lin is emty t%en insert

20) ne3 @ Qetode !(+E&

21) ne3X+'T' @ ITE<

22) ne3XC @ ne3XRC @ U

2) trXRC @ ne3

2) Else T%e ey node #lre#dy %#s rig%t c%ild 2?) Print ZInsertion is not ossi4le #s rig%t c%ild[

2) E$it Yuit t%e e$ecution

2L) EndI5 

2) Else

2V) Print ZT%e ey node #lre#dy %#s c%ild[ T%e ey node %#s no emty c%ild

0) EndI5 

1) EndI5 

2) Sto

Page 42: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 42/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m Se#rc%I;

&np#t' ;E8, t%e item o5 se#rc%, PTR0 is t%e ointer to t%e lined list 5rom 3%ere t%ese#rc% 3ill st#rt)

(#tp#t' (C'TI(, 3%ere t%e item ;E8 is loc#ted, i5 suc% ITE< e$ists)

Data str#)t#re' ined structure o5 4in#ry tree %#*ing R((T #s t%e ointer to t%e root

node)

Stes: 1) tr @ PTR0 St#rt 5rom # gi*en node

2) I5 !trX+'T' N ;E8& I5 t%e current node is not t%e ey node

) I5 !trXC N U& I5 t%e node %#s le5t su4tree

) Se#rc%I;!trXC& Se#rc% t%e le5t su4tree

?) Else ;ey is not in t%e le5t su4tree

) Return!0&

L) EndI5 

Page 43: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 43/139

© 2012 UPESJul 2012Jul 2012

) I5 !trXRC N U& I5 t%e node %#s rig%t su4tree

V) Se#rc%I;!trXRC& Se#rc% t%e rig%t su4tree

10) Else ;ey is not in t%e rig%t su4tree

11) Return!0& Return null ointer 

12) EndI5 

1) Else

1) Return!tr& Return t%e ointer to t%e ey node

1?) EndI5 

1) Sto

Page 44: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 44/139

© 2012 UPESJul 2012Jul 2012

+eletion

6ere, 3e 3ill consider t%e c#se o5 deletion o5 le#*e node in #ny 4in#ry tree) igure

s%o3s %o3 # node cont#ining d#t# #s Q c#n 4e deleted 5rom # 4in#ry tree)

In order to delete # node in # 4in#ry tree, it is reuired to re#c% #t t%e #rent node o5 t%e

node to 4e deleted) T%e lin 5ield o5 t%e #rent node 3%ic% stores t%e loc#tion o5 t%e

node to 4e deleted is t%en set 4y # U entry)

+eletion o5 #n e$tern#l node in # 4in#ry tree

Page 45: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 45/139

© 2012 UPESJul 2012Jul 2012

+eleting # le#*e node 5rom seuenti#l reresent#tion !lgorith" DeleteBinTree$SE+

&np#t: Qi*en ITE< #s d#t# o5 t%e node 3it% 3%ic% t%e node c#n 4e identi5ied 5or deletion) (#tp#t: ' 4in#ry tree 3it%out # node %#*ing d#t# #s ITE<)

Data str#)t#re: 'rr#y ' storing t%e 4in#ry tree) SI9E denotes t%e si>e o5 ')

Steps'

1) 5l#g @ 'SE St#rt 5rom root node

2) l @ Se#rc%SEY!1, ;E8& St#rt se#rc%ing 5rom st#rting inde$

) I5 !'O2 l @ U& #nd !'O2 l . 1 @ U& Test 5or t%e le#*e node∗ ∗

) 5l#g @ TRUE I5 so, t%en delete it

?) 'Ol @ U

) Else

L) Print ZT%e node cont#ining ITE< is not # le#*e node[

)

V) EndI5 

Page 46: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 46/139

© 2012 UPESJul 2012Jul 2012

+eleting # le#*e node 5rom seuenti#l reresent#tion 10) I5 !5l#g @ 'SE&

11) Print Zode does not e$ist : o deletion[ 12) EndI5 

1) Sto

ote : Se#rc%SEY!1, ;E8& 5unction is re*iously de5ined) So, 3e #re not going to

de5ine t%e s#me %ere)

Page 47: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 47/139

© 2012 UPESJul 2012Jul 2012

+eleting # le#*e node 5rom lined reresent#tion

"np#t$ ' 4in#ry tree 3%ose #ddress o5 t%e root ointer is no3n 5rom %&&T #nd "TE' is t%e

d#t# o5 t%e node identi5ied 5or deletion)

&#tp#t$ ' 4in#ry tree 3it%out %#*ing # node 3it% d#t# #s "TE' )

Data str#ct#re$ ined structure o5 4in#ry tree %#*ing %&&T #s t%e ointer to t%e root node)

(teps)

1) tr @ R((T St#rt se#rc% 5rom t%e root

2) &f !tr @ U& then

) Print ZTree is emty[

) E,it Yuit t%e e$ecution

?) End&f 

) #rent @ Sear)hParent!R((T, ITE<& To 5ind t%e #rent o5 t%e desired node

L) &f !#rent N U& then I5 t%e node 3it% ITE< e$ists

) tr1 @ #rentXC, tr2 @ #rentXRC Qet t%e si4ling o5 #rent node

Page 48: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 48/139

© 2012 UPESJul 2012Jul 2012

+eleting # le#*e node 5rom lined reresent#tion V) &f !tr1X+'T' @ ITE<& then C%oose t%e le5t si4ling 3it% d#t# #s ITE< 5or deletion

10) &f !tr1XC @ U& #nd !tr1XRC @ U& then

11) #rentXC @ U e5t c%ild is deleted

12) Else

1) Print Zode is not # le#*e node: o deletion[

1) End&f 

1?) Else C%oose t%e rig%t si4ling 3it% d#t# #s ITE< 5or deletion

1) &f !tr2XC @ U& #nd !tr2XRC @ U& then

1L) #rentXRC @ U Rig%t c%ild is deleted

1) Else

1V) Print Zode is not # le#*e node: o deletion[

20) End&f 

21) End&f 

22) Else

2) Print Zode 3it% d#t# ITE< does not e$ist: +eletion 5#ils[

2) End&f 

2?) Stop

Page 49: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 49/139

© 2012 UPESJul 2012Jul 2012

!lgorith" Sear)h Parent

Input) ITE< is t%e item o5 se#rc%, PTR is t%e ointer to t%e node 5rom 3%ere t%e se#rc%

3ill st#rt) *utput) \#rent] is t%e #ddress o5 t%e #rent node o5 t%e node cont#ining t%e d#t# #s

ITE<)

+ata struture) ined structure o5 4in#ry tree)

(teps)

1) #rent @ PTR 2) &f !PTRX+'T' N ITE<& then

) tr1 @ PTRXC, tr2 @ PTRXRC

) &f !tr1 N U& then

?) Sear)hParent!tr1& Se#rc% in t%e le5t su4tree

) Else ;ey is not in t%e le5t su4tree

L) #rent @ U ITE< is not in le5t su4tree, U ointer is returned

) End&f 

Page 50: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 50/139

© 2012 UPESJul 2012Jul 2012

!lgorith" Sear)h Parent

V) &f !tr2 N U& then

10) Sear)hParent !tr2& Se#rc% in t%e rig%t su4tree

11) Else ;ey is not in t%e rig%t su4tree

12) #rent @ U ITE< is not in rig%t su4tree, U ointer is returned

1) End&f 

1) Else

1?) Ret#rn!#rent& Return t%e ointer to t%e #rent node, i5 #ny

1) End&f 

1L) Stop

Page 51: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 51/139

© 2012 UPESJul 2012Jul 2012

Traversals

Tr#*ers#l is used to *isit e#c% node in t%e tree e$#ctly once)

 = Preorder tr#*ers#l = Inorder tr#*ers#l

 = Postorder tr#*ers#l

Preorder traversal

A In t%is tr#*ers#l, root is *isited 5irst t%en le5t su4tree in reorder 5#s%ion #nd t%en

rig%t su4tree in reorder 5#s%ion) It c#n 4e de5ined #s 4elo3:

 = /isit t%e root node % )

 = Tr#*erse t%e le5t su4tree o5 % in reorder)

 = Tr#*erse t%e rig%t su4tree o5 % in reorder)

Page 52: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 52/139

© 2012 UPESJul 2012Jul 2012

Traversals

Inorder traversal 

A Tr#*erse t%e le5t su4tree o5 t%e root node R is inorder)A /isit t%e root node R)

A Tr#*erse t%e rig%t su4tree o5 t%e root node R is inorder)

Postorder traversal

A Tr#*erse t%e le5t su4tree o5 t%e root R in ostorder 

A

Tr#*erse t%e rig%t su4tree o5 t%e root R in ostorder A /isit t%e root node R)

Page 53: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 53/139

© 2012 UPESJul 2012Jul 2012

!lgorith" Preorder 

"np#t$ R((T is t%e ointer to t%e root node o5 t%e 4in#ry tree)

&#tp#t$ /isiting o5 #ll t%e nodes in reorder 5#s%ion)

Data str#ct#re$ ined structure o5 4in#ry tree)

(teps)

1) tr @ R((T St#rt 5rom t%e R((T

2) &f !tr N U& then I5 it is not #n emty node

) isit!tr& /isit t%e node

) Preorder !trXC& Tr#*erse t%e le5t su4tree o5 t%e node in reorder 

?) Preorder !trXRC& Tr#*erse t%e rig%t su4tree o5 t%e node in reorder 

) End&f 

L) Stop

Page 54: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 54/139

© 2012 UPESJul 2012Jul 2012

!lgorith" &norder 

"np#t$ R((T is t%e ointer to t%e root node o5 t%e 4in#ry tree)

&#tp#t$ /isiting o5 #ll t%e nodes in inorder 5#s%ion)

Data str#ct#re$ ined structure o5 4in#ry tree)

(teps)

1) tr @ R((T St#rt 5rom R((T

2) &f !tr N U& then I5 it is not #n emty node

) &norder !trXC& Tr#*erse t%e le5t su4tree o5 t%e node in inorder 

) isit!tr& /isit t%e node

?) &norder !trXRC& Tr#*erse t%e rig%t su4tree o5 t%e node in inorder 

) End&f 

L) Stop

Page 55: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 55/139

© 2012 UPESJul 2012Jul 2012

!lgorith" Postorder 

"np#t$ R((T is t%e ointer to t%e root node o5 t%e 4in#ry tree)

&#tp#t$ /isiting o5 #ll t%e nodes in reorder 5#s%ion)

Data str#ct#re$ ined structure o5 4in#ry tree)

(teps)

1) tr @ R((T St#rt 5rom t%e root

2) &f !tr N U& then I5 it is not #n emty node

) Postorder !trXC& Tr#*erse t%e le5t su4tree o5 t%e node in inorder 

) Postorder !trXRC& Tr#*erse t%e rig%t su4tree o5 t%e node in inorder 

?) isit!tr& /isit t%e node

) End&f 

L) Stop

Page 56: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 56/139

© 2012 UPESJul 2012Jul 2012

Suose, inorder #nd reorder tr#*ers#ls o5 # 4in#ry tree #re #s 4elo3:

Inorder + " 6 E ' I J C Q

Preorder ' " + E 6 C I J Q

-e %#*e to construct t%e 4in#ry tree)

Solve

1) rom t%e reorder tr#*ers#l, it is e*ident t%#t A is t%e root node)

2) In inorder tr#*ers#l, #ll t%e nodes 3%ic% #re #t t%e le5t side o5 A 4elong to t%e le5t su4

tree #nd t%ose #re #t rig%t side o5 A 4elong to t%e rig%t su4tree)

) o3 t%e ro4lem reduces to 5orm su4trees #nd t%e s#me rocedure c#n 4e #lied

ree#tedly)

Page 57: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 57/139

© 2012 UPESJul 2012Jul 2012

Page 58: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 58/139

© 2012 UPESJul 2012Jul 2012

Page 59: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 59/139

© 2012 UPESJul 2012Jul 2012

ollo3ing is t%e inorder #nd ostorder tr#*ers#ls o5 # 4in#ry tree:

Inorder n1 n2 n n n? n nL n nV

Postorder n1 n n? n n2 n nL nV n

-e %#*e to construct t%e 4in#ry tree)

Solve

Page 60: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 60/139

© 2012 UPESJul 2012Jul 2012

Page 61: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 61/139

© 2012 UPESJul 2012Jul 2012

.erging of T/o Binar Trees

Page 62: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 62/139

© 2012 UPESJul 2012Jul 2012

.erging of T/o Binar Trees

!lgorith" .ergeTrees

"np#t$ T3o ointers %&&T 1 #nd %&&T 2 #re t%e roots o5 t%e t3o 4in#ry tree T 1 #nd T 2,resecti*ely 3it% ined structure)

&#tp#t$ ' 4in#ry tree cont#ining #ll t%e nodes o5 T 1 #nd T 2 %#*ing ointer to t%e root #s %&&T )

Data str#ct#re$ ined structure o5 4in#ry trees #re considered)

(teps)

1) &f !R((T1 @ U& then I5 T 1 is emty t%en T 2 is t%e result

2) R((T @ R((T2 Tree T 2 is t%e result

) E,it

) Else

?) &f !R((T2 @ U& then I5 T 2 is emty t%en T 1 is # result

) R((T @ R((T1

L) E,it

Page 63: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 63/139

© 2012 UPESJul 2012Jul 2012

.erging of T/o Binar Trees

) Else I5 4ot% trees #re non emty

V) &f !R((T1XC6I+ @ U& then

10) R((T1XC6I+ @ R((T2 <erge T 2 #s t%e le5t c%ild o5 T 1

11) R((T @ R((T1

12) Else

1) &f !R((T1XRC6I+ @ U& then

1) R((T1XRC6I+ @ R((T2 <erge T 2 #s t%e rig%t c%ild o5 

T 1

1?) R((T @ R((T1

1) Else

1L) &f !R((T2XC6I+ @ U& then 1) R((T2XC6I+ @ R((T1 <erge T 1 #s t%e le5t c%ild o5 T 2

1V) R((T @ R((T

Page 64: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 64/139

© 2012 UPESJul 2012Jul 2012

.erging of T/o Binar Trees

20) Else

21) &f !R((T2XRC6I+ @ U& then 22) R((T2XRC6I+ @ R((T1 <erge T 1 #s t%e rig%t c%ild o5 T 2

2) R((T @ R((T2

2) Else

2?) Print ZTrees #re not com#ti4le 5or merge oer#tion[

2) E,it

2L) End&f 

2) End&f 

2V) End&f 

0) End&f 

1) End&f 

2) End&f 

) Stop

Page 65: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 65/139

© 2012 UPESJul 2012Jul 2012

T0PES (1 B&N!R0 TREES

1) E$ression tree

2) "in#ry se#rc% tree

) 6e# tree

) T%re#ded 4in#ry tree

?) 6u55m#n tree

) 6eig%t 4#l#nced tree !#lso no3n #s '/ tree&

L) Red 4l#c tree

) Sl#y tree

V) +ecision tree

Page 66: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 66/139

© 2012 UPESJul 2012Jul 2012

E,pression Tree

 'n expression tree is # 4in#ry tree 3%ic% stores #n #rit%metic e$ression) T%e le#*es o5

#n e$ression tree #re oer#nds, suc% #s const#nts or *#ri#4le n#mes, #nd #ll intern#lnodes #re t%e oer#tors)

E$ression tree is #l3#ys # 4in#ry tree 4ec#use #n #rit%metic e$ression cont#ins

eit%er 4in#ry oer#tors or un#ry oer#tors)

Page 67: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 67/139

© 2012 UPESJul 2012Jul 2012

!rith"eti) E,pression to E,pression Tree

I5 t%e #rit%metic e$ressions gi*en in in5i$ not#tion)

Using st#c, #n e$ression in in5i$ not#tion c#n e#sily 4e con*erted into ost5i$ not#tion)

et us consider # ost5i$ not#tion o5 t%e #rit%metic e$ression #s s%o3n =

 ABC .∗ DE ∗( . ) =

-e %#*e to sc#n one sym4ol #t # time, i5 t%e sym4ol encountered is #n oer#nd 3e

cre#te # onenode tree #nd us% # ointer to it onto # st#c)

I5 t%e sym4ol is #n oer#tor !#ssume t%e c#se o5 4in#ry oer#tors only&, 3e o ointersto t3o trees T 1 #nd T 2 5rom t%e st#c !T 1 is oed 5irst& #nd 5orm # tree 3%ose root is

t%e oer#tor #nd 3%ose le5t #nd rig%t c%ildren oint to T 2 #nd T 1 resecti*ely)

 ' ointer to t%is ne3 tree is t%en us%ed onto t%e st#c)

Page 68: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 68/139

© 2012 UPESJul 2012Jul 2012

Constr#)tion of an E,pression Tree

Page 69: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 69/139

© 2012 UPESJul 2012Jul 2012

Constr#)tion of an E,pression Tree

Page 70: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 70/139

© 2012 UPESJul 2012Jul 2012

Constr#)tion of an E,pression Tree

Page 71: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 71/139

© 2012 UPESJul 2012Jul 2012

(er#tions on t%e e$ression tree

Tr#*ers#l

A It is s#me #s t%e 4in#ry tree tr#*ers#ls #s #lre#dy discussed)A It c#n 4e noted t%#t inorder, reorder #nd ostorder tr#*ers#ls on #n e$ression tree

3ill result t%e #rit%metic e$ressions in in5i$, re5i$ #nd ost5i$ !re*erse olis%&

not#tions resecti*ely)

E*#lu#tionA \e*#lu#tion] o5 t%e e$ression is to e*#lu#te t%e e$ression 5or 3%ic% t%e tree is 5ormed

Page 72: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 72/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m E$ressionTreeE*#lu#tion

"np#t$ R((T is t%e ointer to t%e root o5 e$ression tree) /'UES stores 5or *#lues o5

oer#nds) &#tp#t$ /#lue o5 t%e #rit%metic e$ression stored in t%e e$ression tree)

Data str#ct#re$ ined structure o5 t%e 4in#ry tree)

(teps)

1) tr @ R((T St#rt 5rom t%e root o5 t%e tree

2) &f !tr N U& then I5 t%e node is nonnull

) ltr @ trXC6I+ Pointer to le5t su4tree

) rtr @ trXRC6I+ Pointer to rig%t su4tree

?) &f (perand!ltrX+'T'& t%en I5 le5t su4tree is 3it% only node o5 #n oer#nd

) T 1 @ Read!ltrX+'T'& Re#d t%e *#lue 5or oer#nd 5rom *#lues into T 1

L) Else

) T 1 @ E,pressionTreeEval#ation !ltr& E*#lu#te t%e le5t su4tree

V) End&f 

Page 73: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 73/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m E$ressionTreeE*#lu#tion

10) &f (er#nd!rtrX+'T'& then I5 rig%t su4tree is 3it% only node o5 #n oer#nd

11) T 2 @ Re#d!rtrX+'T'& Re#d t%e *#lue 5or oer#nd 5rom /'UES into T 2 12) Else

1) T 2 @ E,pressionTreeEval#ation !rtr& E*#lu#te t%e rig%t su4tree

1) End&f 

1?) oer#tor @ trX+'T' (4t#in t%e oer#tion

1) *#l @ T 1 oer#tor T 2 (4t#in t%e result 3%en le5t #nd rig%t su4trees

1L) Return!*#l&

1) End&f 

1V) Stop

Page 74: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 74/139

© 2012 UPESJul 2012Jul 2012

"in#ry Se#rc% Tree

 ' 4in#ry tree T is termed #s binary search tree !or binary sorted tree& i5 e#c% node * o5

T s#tis5ies t%e 5ollo3ing roerty:A T%e *#lue #t * is gre#ter t%#n e*ery *#lue in t%e le5t su4tree o5 * #nd is less t%#n

e*ery *#lue in t%e rig%t su4tree o5 * )

Page 75: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 75/139

© 2012 UPESJul 2012Jul 2012

(er#tions in "in#ry Se#rc% Tree

Se#rc%ing 5or # d#t#

Inserting #ny d#t# into it

+eleting #ny d#t# 5rom it

Tr#*ersing t%e tree)

Page 76: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 76/139

© 2012 UPESJul 2012Jul 2012

Se#rc%ing in "ST

Se#rc%ing 5or # d#t# in # 4in#ry se#rc% tree is muc% 5#ster t%#n in #rr#ys or lined lists)

Suose, in # 4in#ry se#rc% tree T , ITE< 4e t%e item o5 se#rc%) -e 3ill #ssume t%#t,t%e tree is reresented using lined structure)

-e st#rt 5rom t%e root node % ) T%en, i5 ITE< is less t%#n t%e *#lue in t%e root node % ,

3e roceed to its le5t c%ildD i5 ITE< is gre#ter t%#n t%e *#lue in t%e node % , 3e roceed

to its rig%t c%ild)

T%e rocess 3ill 4e continued till t%e ITE< is not 5ound or 3e re#c% to # de#d end, t%#t

is, le#*e node)

igure s%o3s t%e tr#c !in s%#ded line& 5or se#rc%ing o5 ? in # 4in#ry se#rc% tree)

Page 77: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 77/139

© 2012 UPESJul 2012Jul 2012

Se#rc%ing in "ST

!lgorith" Sear)h$BST

"np#t$ ITE< is t%e d#t# t%#t %#s to 4e se#rc%ed) &#tp#t$ I5 5ound t%en ointer to t%e node cont#ining #s d#t# ITE< else # mess#ge)

Data str#ct#re$ ined structure o5 t%e 4in#ry tree) Pointer to t%e root node is R((T)

(teps)

1) tr @ R((T, 5l#g @ 'SE St#rt 5rom root

2) 2hile !tr N U& #nd !5l#g @ 'SE& do

) Case' ITE< G trX+'T' Qo to t%e le5t su4tree

) tr @ trXC6I+

?) Case' trX+'T' @ ITE< Se#rc% is success5ul

) 5l#g @ TRUE

L) Case' ITE< B trX +'T' Qo to t%e rig%t su4tree

) tr @ trXRC6I+

Page 78: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 78/139

© 2012 UPESJul 2012Jul 2012

Se#rc%ing in "ST

!lgorith" Sear)h$BST

"np#t$ ITE< is t%e d#t# t%#t %#s to 4e se#rc%ed) &#tp#t$ I5 5ound t%en ointer to t%e node cont#ining #s d#t# ITE< else # mess#ge)

Data str#ct#re$ ined structure o5 t%e 4in#ry tree) Pointer to t%e root node is R((T)

(teps)

1) tr @ R((T, 5l#g @ 'SE St#rt 5rom root

2) 2hile !tr N U& #nd !5l#g @ 'SE& do

) Case' ITE< G trX+'T' Qo to t%e le5t su4tree

) tr @ trXC6I+

?) Case' trX+'T' @ ITE< Se#rc% is success5ul

) 5l#g @ TRUE

L) Case' ITE< B trX +'T' Qo to t%e rig%t su4tree

) tr @ trXRC6I+

Page 79: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 79/139

© 2012 UPESJul 2012Jul 2012

Se#rc%ing in "ST

V) EndCase

10) End2hile 11) &f !5l#g @ TRUE& then Se#rc% is success5ul

12) Print ZITE< %#s 5ound #t t%e node[, tr 

1) Else

1) Print ZITE< does not e$ist: Se#rc% is unsuccess5ul[

1?) End&f 

1) Stop

Page 80: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 80/139

© 2012 UPESJul 2012Jul 2012

Inserting into # 4in#ry se#rc% tree

To insert # node 3it% d#t#, s#y ITE<, into # tree, t%e tree is to 4e se#rc%ed st#rting 5rom

t%e root node) I5 ITE< is 5ound, do not%ing, ot%er3ise ITE< is to 4e inserted #t t%e de#d

end 3%ere se#rc% %#lts)

S

Page 81: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 81/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m Insert"ST

"np#t$ ITE< is t%e d#t# comonent o5 # node t%#t %#s to 4e inserted)

&#tp#t$ I5 t%ere is no node %#*ing d#t# ITE<, it is inserted into t%e tree else # mess#ge) Data str#ct#re$ ined structure o5 4in#ry tree) Pointer to t%e root node is R((T)

(teps)

1) tr @ R((T, 5l#g @ 'SE St#rt 5rom root node

2) 2hile !tr N U& #nd !5l#g @ 'SE& do

) Case' ITE< G trX+'T' Qo to t%e le5t su4tree

) tr1 @ tr 

?) tr @ trXC6I+

) Case' ITE< B trX+'T' Qo to t%e rig%t su4tree

L) tr1 @ tr 

) tr @ trXRC6I+

'l it% I t "ST

Page 82: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 82/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m Insert"ST

V) Case' trX+'T' @ ITE< ode e$ists

10) 5l#g @ TRUE 11) Print ZITE< #lre#dy e$ists[

12) E,it Yuit t%e e$ecution

1) EndCase

1) End2hile

1) &f !tr @ U& then Insert 3%en se#rc% %#lts #t de#d end

1L) ne3 @ 3etNode!(+E&

1) ne3X+'T' @ ITE< '*#il # node #nd t%en initi#li>e it

1V) ne3XC6I+ @ U

20) ne3XRC6I+ @ U

'l it% I t "ST

Page 83: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 83/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m Insert"ST

21) &f !tr1X+'T' G ITE<& then Insert #s t%e rig%t c%ild

22) tr1XRC6I+ @ ne3 2) Else

2) tr1XC6I+ @ ne3 Insert #s t%e le5t c%ild

2?) End&f 

2) End&f 

2L) Stop

+ l ti d 5 4i % t

Page 84: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 84/139

© 2012 UPESJul 2012Jul 2012

+eleting # node 5rom # 4in#ry se#rc% tree

Suose T is # 4in#ry se#rc% tree, #nd ITE< is t%e in5orm#tion gi*en 3%ic% %#s to 4e

deleted 5rom T i5 it e$ists in t%e tree)

Suose * 4e t%e node 3%ic% cont#ins t%e in5orm#tion ITE<)

et us #ssume P'RET!* & denotes t%e #rent node o5 * #nd SUCC!* & denotes t%e

inorder successor o5 t%e node * !inorder s#ccessor me#ns t%e node 3%ic% comes #5ter

* during t%e inorder tr#*ers#l o5 T &)

T%en t%e deletion o5 t%e node * deends on t%e num4er o5 its c%ildren) 6ence, t%ree

c#ses m#y #rise:

Case +$ * is t%e le#*e node

Case $ * %#s e$#ctly one c%ild

Case -$ * %#s t3o c%ilds

+ l ti d 5 4i % t

Page 85: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 85/139

© 2012 UPESJul 2012Jul 2012

+eleting # node 5rom # 4in#ry se#rc% tree

Case %' * is deleted 5rom T 4y simly setting t%e ointer o5 * in t%e #rent node

P'RET!* & 4y U *#lue)

Case *' * is deleted 5rom T 4y simly rel#cing t%e ointer o5 * in P'RET!* & 4y t%e

ointer o5 t%e only c%ild o5 * )

Case 4' * is deleted 5rom T 4y 5irst deleting SUCC!* & 5rom T !4y using C#se 1 or C#se

2D !it c#n 4e *eri5ied t%#t SUCC!* & ne*er %#s # le5t c%ild& #nd t%en rel#ce t%e d#t#

content in node * 4y t%e d#t# content in node SUCC!* &)

+ l ti d 5 4i % t

Page 86: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 86/139

© 2012 UPESJul 2012Jul 2012

+eleting # node 5rom # 4in#ry se#rc% tree

+ l ti d 5 4i % t

Page 87: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 87/139

© 2012 UPESJul 2012Jul 2012

+eleting # node 5rom # 4in#ry se#rc% tree

+ l ti d 5 4i % t

Page 88: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 88/139

© 2012 UPESJul 2012Jul 2012

+eleting # node 5rom # 4in#ry se#rc% tree

'lgorit%m +elete "ST

Page 89: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 89/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

"np#t$ ITE< is t%e d#t# o5 t%e node to 4e deleted)

&#tp#t$ I5 t%e node 3it% d#t# #s ITE< e$ist it is deleted else # mess#ge) Data str#ct#re$ ined structure o5 4in#ry tree) Pointer to t%e root node is R((T)

(teps)

1) tr @ R((T, 5l#g @ 'SE

2) 2hile !tr N U& #nd !5l#g @ 'SE& do Ste to 5ind t%e loc#tion o5 t%e node

) Case' ITE< G trX+'T'

) #rent @ tr 

?) tr @ trXC6I+

) Case' ITE< B trX+'T'

L) #rent @ tr 

) tr @ trXRC6I+

'lgorit%m +elete "ST

Page 90: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 90/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

V) Case' trX+'T' @ ITE<

10) 5l#g @ TRUE 11) EndCase

12) End2hile

1) &f !5l#g @ 'SE& then -%en t%e node does not e$ist

1) Print ZITE< does not e$ist: o deletion[

1?) E,it Yuit t%e e$ecution

1) End&f 

W +ECI+E T6E C'SE ( +EETI( W

1L) &f !trXC6I+ @ U& #nd !trXRC6I+ @ U& then ode %#s no c%ild

1) c#se @ 1

1V) Else

'lgorit%m +elete "ST

Page 91: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 91/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

20) &f !trXC6I+ N U& #nd !trXRC6I+ N U& then ode cont#ins 4ot% t%e

c%ild

21) c#se @

22) Else ode cont#ins only one c%ild

2) c#se @ 2

2) End&f 

2?) End&f  W +EETI(: C'SE 1 W

2) &f !c#se @ 1& then

2L) &f !#rentXC6I+ @ tr& then I5 t%e node is # le5t c%ild

2 #rentXC6I+ @ U Set ointer o5 its #rent

2V) Else

0) #rentXRC6I+ @ U

'lgorit%m +elete "ST

Page 92: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 92/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

1) End&f 

2) Ret#rnNode!tr& Return deleted node to t%e memory 4#n ) End&f 

W +EETI(: C'SE 2 W

) &f !c#se @ 2& then -%en t%e node cont#ins only one c%ild

?) &f !#rentXC6I+ @ tr& then I5 t%e node is # le5t c%ild

) &f !trXC6I+ @ U& then

L) #rentXC6I+ @ trXRC6I+

) Else

V) #rentXC6I+ @ trXC6I+

0) End&f 

'lgorit%m +elete "ST

Page 93: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 93/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

1) Else

2) &f !#rentXRC6I+ @ tr& then ) &f !trXC6I+ @ U& then

) #rentXRC6I+ @ trXRC6I+

?) Else

) #rentXRC6I+ @ trXC6I+

L) End&f 

) End&f

V) End&f 

?0) Ret#r"nNode!tr& Return deleted node to t%e memory 4#n

?1) End&f 

'lgorit%m +elete "ST

Page 94: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 94/139

© 2012 UPESJul 2012Jul 2012

 'lgorit%m +elete"ST

W +EETI(: C'SE W

?2) &f !c#se @ & -%en cont#ins 4ot% c%ild ?) tr1 @ SUCC!tr& ind t%e in order successor o5 t%e node

?) item1 @ tr1X+'T'

??) Delete$BST !item1& +elete t%e inorder successor 

?) trX+'T' @ item1 Rel#ce t%e d#t#

3it% t%e d#t# o5 in order successor 

?L) End&f 

?) Stop

Note - ote t%#t, 3e #ssume t%e 5unction .CC !tr& 3%ic% returns ointer to t%e inorder

successor o5 t%e node tr) It c#n 4e *eri5ied t%#t inorder successor o5 tr #l3#ys occurs

in t%e rig%t su4tree o5 tr, #nd inorder successor o5 tr does not %#*e le5t c%ild)

inding t%e inorder successor

Page 95: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 95/139

© 2012 UPESJul 2012Jul 2012

inding t%e inorder successor 

!lgorith" SUCC

"np#t$ Pointer to # node PTR 3%ose inorder successor is to 4e no3n)

&#tp#t$ Pointer to t%e inorder successor o5 tr)

Data str#ct#re$ ined structure o5 4in#ry tree)

(teps)

1) tr1 @ PTRXRC6I+ <o*e to t%e rig%t su4tree

2) &f !tr1 N U& then I5 rig%t su4tree is not emty

) 2hile !tr1XC6I+ N U& do <o*e to t%e le5tmost end

) tr1 @ tr1XC6I+

?) End2hile

) End&f 

L) Ret#rn!tr1& Return t%e ointer o5 t%e inorder successor 

) Stop

Tr#*ers#ls on 4in#ry se#rc% tree

Page 96: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 96/139

© 2012 UPESJul 2012Jul 2012

Tr#*ers#ls on 4in#ry se#rc% tree

 'll t%e tr#*ers#l oer#tions #lre#dy discussed in re*ious slide, #re #lic#4le in 4in#ry

se#rc% trees 3it%out #ny #lter#tion)

It c#n 4e *eri5ied t%#t inorder tr#*ers#l on # 4in#ry se#rc% tree 3ill gi*e t%e sorted order

o5 d#t# in #scending order)

I5 3e reuire to sort # set o5 d#t#, # 4in#ry se#rc% tree c#n 4e 4uilt 3it% t%ose d#t# #nd

t%en inorder tr#*ers#l c#n 4e #lied)

T%is met%od o5 sorting is no3n #s 4in#ry sort #nd t%is is 3%y 4in#ry se#rc% tree is #lso

termed #s 4in#ry sorted tree) T%is sorting met%od is considered #s one o5 t%e e55icientsorting met%od)

-ppliation of binary searh trees

Page 97: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 97/139

© 2012 UPESJul 2012Jul 2012

 -ppliation of binary searh trees

It c#n 4e used in sorting met%od

It c#n 4e used in se#rc%ing met%od

It c#n 4e used in storing t%e d#t#

In 5#ct, 4in#ry se#rc% tree is used to de5ine ot%er d#t# structure, 5or e$#mle,

" tree

Heap Trees

Page 98: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 98/139

© 2012 UPESJul 2012Jul 2012

Heap Trees

Suose, 0 is # comlete 4in#ry tree) T%en it 3ill 4e termed #s heap tree, i5 it s#tis5ies

t%e 5ollo3ing

roerties:

!i& or e#c% node * in 0 , t%e *#lue #t * is gre#ter t%#n or eu#l to t%e *#lue o5 e#c% o5

t%e c%ildren o5 * )

!ii& (r in ot%er 3ords, * %#s t%e *#lue 3%ic% is gre#ter t%#n or eu#l to t%e *#lue o5

e*ery successor o5 * )

Suc% # %e# tree is c#lled 1ax heap) Simil#rly, 1in heap is ossi4le, 3%ere #ny node *

%#s t%e *#lue less t%#n or eu#l to t%e *#lue o5 #ny o5 t%e successors o5 * )

Reresent#tion o5 %e# tree

Page 99: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 99/139

© 2012 UPESJul 2012Jul 2012

Reresent#tion o5 %e# tree

 ' %e# tree c#n 4e reresented using lined structure) "ut single #rr#y reresent#tion

%#s cert#in #d*#nt#ges 5or # %e# tree o*er its lined reresent#tion)

 's %e# tree is # comlete 4in#ry tree, so t%ere 3ill 4e no 3#st#ge o5 #rr#y s#ce

4et3een t3o nonnull entriesD i5 #ny null entries, t%ese #re only #t t%e t#il o5 t%e #rr#y)

 'not%er 5#cility is t%#t 3e do not %#*e to m#int#in #ny lins o5 descend#nts !c%ild&, %ere,

t%ese #re #utom#tic#lly imlied, to #ccess # c%ild only # 5e3 simle #rit%metic

comut#tions #re in*ol*ed)

T%e m#or #d*#nt#ge 3it% t%is reresent#tion is t%#t, 5rom # node 3e c#n go in 4ot%directions, to3#rds its #ncestors #nd to3#rds its successor) T%is is, #lt%oug% ossi4le in

lined structure 4ut is # m#tter o5 m#inten#nce o5 e$tr# lin 5ield)

&nsertion into a heap tree

Page 100: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 100/139

© 2012 UPESJul 2012Jul 2012

&nsertion into a heap tree

T%is oer#tion is used to insert # node into #n e$isting %e# tree s#tis5ying t%e

roerties o5 %e# tree) Ree#ted insertions o5 d#t#, st#rting 5rom #n emty %e# tree,

one c#n 4uild u # %e# tree)

et us consider t%e %e# !m#$& tree #s s%o3n in igure !ne$t slide&) -e 3#nt to insert

1V #nd 111 in it)

T%e rincile o5 insertion is t%#t, 5irst 3e %#*e to #doin t%e d#t# in t%e comlete 4in#ry

tree)

e$t, 3e %#*e to com#re it 3it% t%e d#t# in its #rentD i5 t%e *#lue is gre#ter t%#n t%#t#t #rent t%en interc%#nge t%e *#lues)

T%is 3ill continue 4et3een t3o nodes on #t% 5rom t%e ne3ly inserted node to t%e root

node till 3e get # #rent 3%ose *#lue is gre#ter t%#n its c%ild or 3e re#c%ed #t t%e root)

&nsertion into a heap tree

Page 101: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 101/139

© 2012 UPESJul 2012Jul 2012

&nsertion into a heap tree

Deletion of a node fro" heap tree

Page 102: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 102/139

© 2012 UPESJul 2012Jul 2012

Deletion of a node fro" heap tree

Re#d t%e root node into # temor#ry stor#ge s#y, ITE<)

Rel#ce t%e root node 4y t%e l#st node in t%e %e# tree) T%en re%e# t%e tree#s st#ted 4elo3:

A et ne3ly modi5ied root node 4e t%e current node) Com#re its *#lue 3it% t%e

*#lue o5 its t3o c%ild)

A et 7 4e t%e c%ild 3%ose *#lue is t%e l#rgest) Interc%#nge t%e *#lue o5 7 3it% t%e

*#lue o5 t%e current node)

A <#e 7 #s t%e current node)A Continue re%e#, i5 t%e current node is not #n emty node)

Deletion of a node fro" heap tree

Page 103: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 103/139

© 2012 UPESJul 2012Jul 2012

Deletion of a node fro" heap tree

Height Balan)ed Binar Tree

Page 104: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 104/139

© 2012 UPESJul 2012Jul 2012

Height Balan)ed Binar Tree

 ' 4in#ry se#rc% tree is s#id to 4e height balanced 4in#ry se#rc% tree i5 #ll its nodes

%#*e # 4#l#nce 5#ctor o5 1, 0 or =1) T%#t is, 5or e*ery node in t%e tree)

|bf | @ |h = hR| F 1

It c#n 4e noted t%#t # %eig%t 4#l#nced 4in#ry tree is #l3#ys # 4in#ry se#rc% tree #nd #

comlete 4in#ry se#rc% tree is #l3#ys %eig%t 4#l#nced, 4ut t%e re*erse m#y not 4e true)

T%e 4#sic o4ecti*e o5 t%e %eig%t 4#l#nced 4in#ry se#rc% tree is to er5orm t%e

se#rc%ing, insertion #nd deletion oer#tions e55iciently)

Height Balan)ed Binar Tree

Page 105: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 105/139

© 2012 UPESJul 2012Jul 2012

Height Balan)ed Binar Tree

 In t%e 5ollo3ing discussion, 3e 3ill see %o3 #n un4#l#nced 4in#ry se#rc% tree c#n 4e

con*erted into # %eig%t 4#l#nced 4in#ry se#rc% tree)

Suose, initi#lly t%ere is # %eig%t 4#l#nced 4in#ry tree) -%ene*er # ne3 node is

inserted into it !or deleted 5rom it&, it m#y 4ecome un4#l#nced)

-e 3ill 5irst see t%e mec%#nism to 4#l#nce #n un4#l#nce tree due to insertion o5 #

node)

A 1) "nsert node into a binary search tree$ Insert t%e node into its roer osition

5ollo3ing t%e roerties o5 4in#ry se#rc% tree)A 2) Co1p#te the balance factors$ (n t%e #t% st#rting 5rom t%e root node to t%e

node ne3ly inserted, comute t%e 4#l#nce 5#ctors o5 e#c% node)

Height Balan)ed Binar Tree

Page 106: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 106/139

© 2012 UPESJul 2012Jul 2012

Height Balan)ed Binar Tree

A ) +ecide t%e i*ot node: (n t%e #t% #s tr#ced in Ste 2, determine 3%et%er t%e

#4solute *#lue o5 #ny node]s 4#l#nce 5#ctor is s3itc%ed 5rom 1 to 2) I5 so, t%e tree

4ecomes un4#l#nced) T%e node 3%ose #4solute *#lue o5 4#l#nce 5#ctor iss3itc%ed 5rom 1 to 2, m#r it #s # seci#l node c#lled i*ot node) !T%ere m#y 4e

more t%#n one node 3%ose 4#l#nce 5#ctor |45| is s3itc%ed 5rom 1 to 2 4ut t%e

ne#rest node to ne3ly inserted node 3ill 4e t%e i*ot node)&

A "#l#nce t%e un4#l#nce tree: It is necess#ry to m#niul#te ointers centred #t t%e

i*ot node to 4ring t%e tree 4#c into %eig%t 4#l#nce) T%is ointer m#niul#tion is

3ell no3n #s '/ rot#tion))

-/ rotations

Page 107: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 107/139

© 2012 UPESJul 2012Jul 2012

 -/ rotations

In order to 4#l#nce # tree, one eleg#nt met%od de*ised in 1V2 4y t3o Russi#n

m#t%em#tici#ns, Q)<) 'delson/elsii #nd E)<) endis, #nd t%e met%od is n#med #s

 '/ rot#tion in t%eir %onour) T%ere #re 5our c#ses o5 rot#tions ossi4le

Case %' nbalance occ#rred d#e to the insertion in the left s#b2tree of the left child of

the pi3ot node4 igure illustr#tes t%e rot#tion 5or C#se 1) In t%is c#se, 5ollo3ing

m#niul#tions in ointers t#e l#ce:

^ Rig%t su4tree ! AR& o5 le5t c%ild ! A& o5 i*ot node !P & 4ecomes t%e le5t su4tree o5 P )

^ P 4ecomes t%e rig%t c%ild o5 A)

^ e5t su4tree ! A& o5 A rem#ins t%e s#me)

T%is c#se is c#lled ETT(ET insertion)

ETT(ET insertion

Page 108: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 108/139

© 2012 UPESJul 2012Jul 2012

ET T( ET insertion

ETT(ET insertion

Page 109: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 109/139

© 2012 UPESJul 2012Jul 2012

ET T( ET insertion

RIQ6TT(RIQ6T insertion

Page 110: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 110/139

© 2012 UPESJul 2012Jul 2012

RIQ6T T( RIQ6T insertion

Case *' nbalance occ#rs d#e to the insertion in the right s#b2tree of the right child of

the pi3ot node4

T%is c#se is t%e re*erse #nd symmetric to C#se 1) T%is rot#tion is illustr#ted in igure)

In t%is c#se, t%e 5ollo3ing m#niul#tions in ointers t#e l#ce:

^ e5t su4tree !B& o5 rig%t c%ild !B& o5 i*ot node !P & 4ecomes t%e rig%t su4tree o5 P )

^ P 4ecome t%e le5t c%ild o5 B)

^ Rig%t su4tree !BR

& o5 B rem#ins s#me)

T%is c#se is no3n #s RIQ6TT(RIQ6T insertion)

RIQ6TT(RIQ6T insertion

Page 111: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 111/139

© 2012 UPESJul 2012Jul 2012

Q ( Q se o

ETT(RIQ6T insertion

Page 112: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 112/139

© 2012 UPESJul 2012Jul 2012

Case 4' nbalance occ#rs d#e to the insertion in the right s#b2tree of the left child of

the pi3ot node4 T%is c#se is no3n #s ETT(RIQ6T insertion)

C#se in*ol*es t3o rot#tions 5or t%e m#niul#tion in ointers:

%otation +

^ e5t su4tree !B& o5 t%e rig%t c%ild !B& o5 t%e le5t c%ild o5 t%e i*ot node !P & 4ecomes

t%e rig%t su4tree o5 t%e le5t c%ild ! A&)

^ e5t c%ild ! A& o5 t%e i*ot node !P & 4ecomes t%e le5t c%ild o5 B)

%otation  

^ Rig%t su4tree !BR& o5 t%e rig%t c%ild !B& o5 t%e le5t c%ild ! A& o5 t%e PI/(T node !P &

4ecomes t%e le5t su4tree o5 P )

^ P 4ecomes t%e rig%t c%ild o5 B)

ETT(RIQ6T insertion

Page 113: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 113/139

© 2012 UPESJul 2012Jul 2012

ETT(RIQ6T insertion

Page 114: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 114/139

© 2012 UPESJul 2012Jul 2012

ETT(RIQ6T insertion

Page 115: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 115/139

© 2012 UPESJul 2012Jul 2012

RIQ6TT(ET insertion

Page 116: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 116/139

© 2012 UPESJul 2012Jul 2012

Case 5' nbalance occ#rs d#e to the insertion in the left s#b2tree of right child of the

 pi3ot node4 T%is c#se is no3n #s RIQ6TT(ET insertion)

T%ere #re t3o rot#tions 5or t%e m#niul#tions o5 ointers in t%is c#se, t%ese #re:

%otation +

^ Rig%t su4tree !BR& o5 t%e le5t c%ild !B& o5 t%e rig%t c%ild ! A& o5 t%e i*ot node !P &

4ecomes t%e le5t su4tree o5 A)

^ Rig%t c%ild ! A& o5 t%e i*ot node !P & 4ecomes t%e rig%t c%ild o5 B)

%otation  

^ e5t su4tree !B& o5 t%e rig%t c%ild !B& o5 t%e rig%t c%ild ! A& o5 t%e i*ot rode !P &

4ecomes t%e rig%t su4tree o5 P )

^ P 4ecomes t%e le5t c%ild o5 B)

T%is c#se is no3n #s RIQ6TT(ET insertion)

RIQ6TT(ET insertion

Page 117: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 117/139

© 2012 UPESJul 2012Jul 2012

RIQ6TT(ET insertion

Page 118: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 118/139

© 2012 UPESJul 2012Jul 2012

Threaded Binar Trees

Page 119: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 119/139

© 2012 UPESJul 2012Jul 2012

 ' 4in#ry se#rc% tree is t%re#ded 4y m#ing #ll le5t c%ild ointers , 3%ic%

#re norm#lly null to oint to t%eir inorder redecessors, #nd #ll rig%t c%ildointers, 3%ic% #re norm#lly null to oint to t%eir inorder successors,

resecti*ely

T%e inorder tr#*ers#l o5 t%is tree yields:

 ' " C + E Q 6 I)

!dvantages of threaded binar tree

Page 120: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 120/139

© 2012 UPESJul 2012Jul 2012

T%e tr#*ers#l oer#tion is more 5#ster t%#n t%#t o5 its unt%re#ded *ersion, 4ec#use 3it%

t%re#ded 4in#ry tree nonrecursi*e imlement#tion is ossi4le 3%ic% c#n run 5#ster #nd

does not reuire t%e 4ot%er#tion o5 st#c m#n#gement)

T%e second #d*#nt#ge is more su4tle 3it% # t%re#ded 4in#ry tree, 3e c#n e55iciently

determine t%e redecessor #nd successor nodes st#rting 5rom #ny node) In c#se o5

unt%re#ded 4in#ry tree, %o3e*er, t%is t#s is more time consuming #nd di55icult)

 'ny node c#n 4e #ccessi4le 5rom #ny ot%er node) T%re#ds #re usu#lly more to u3#rd

3%ere#s lins #re do3n3#rd) T%is is not ossi4le in unt%re#ded counter #rt 4ec#use

t%ere 3e c#n mo*e only in do3n3#rd direction st#rting 5rom root)

E$tended "in#ry Tree

Page 121: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 121/139

© 2012 UPESJul 2012Jul 2012

When every node of a tree has either 0 or 2 children then such a tree is called

extended binary tree or 2-tree. The nodes with two children are called

internal nodes. The nodes without children are known as external nodes.

At some places in order to identify internal nodes in figures circles are used.

To identify external nodes suares are used.

The nodes in binary tree that have only one child can be extended with onemore child. This extended binary tree can be used for implementing the

algebraic euation because in the algebraic euation the left and right child

nodes are operands and the parent of the child represents the operator.

1!ay search tree

Page 122: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 122/139

© 2012 UPESJul 2012Jul 2012

1) 'n 13#y se#rc% tree T is # tree in 3%ic% #ll nodes #re o5 degree F 1)

2) E#c% node in t%e tree cont#ins t%e 5ollo3ing #ttri4utes:

3%ere

1 F n G 1

5i !1 F i F n& #re ey *#lues in t%e node

Pi !0 F i F n& #re ointers to t%e su4trees o5 T )

) 5 i G 5 i .1, 1 F i G n

) 'll t%e ey *#lues in t%e su4tree ointed 4y P i #re less t%#n t%e ey *#lues 5 i .1, 0 F i

G n)

?) 'll t%e ey *#lues in t%e su4tree ointed 4y P n is gre#ter t%#n 5 n)

) 'll t%e su4trees ointed 4y P i  !0 F i F n& #re #lso 13#y se#rc% trees)

3#y se#rc% tree

Page 123: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 123/139

© 2012 UPESJul 2012Jul 2012

B Tree

Page 124: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 124/139

© 2012 UPESJul 2012Jul 2012

 '  tree T o5 order 1 is #n 13#y se#rc% tree t%#t is eit%er emty, or it s#tis5ies t%e

5ollo3ing

roerties:

1) T%e root node %#s #t le#st 2 c%ildren

2) 'll nodes ot%er t%#n t%e root node %#*e #t le#st _12_ c%ild

) 'll 5#ilure nodes #re #t t%e s#me le*el)

*ote$ ' fail#re node reresents # node 3%ic% c#n 4e re#c%ed during # se#rc% only i5 t%e*#lue, s#y, X , 4eing se#rc%ed 5or, is not in t%e tree) or con*enience, t%ese emty su4

trees #re rel#ced 4y %yot%etic#l nodes c#lled 5#ilure nodes)

" tree o5 order )

Page 125: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 125/139

© 2012 UPESJul 2012Jul 2012

(perations on a B Tree

Page 126: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 126/139

© 2012 UPESJul 2012Jul 2012

se#rc%

"ursting # node in insertion

Page 127: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 127/139

© 2012 UPESJul 2012Jul 2012

Insertion

Page 128: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 128/139

© 2012 UPESJul 2012Jul 2012

10, 20, 0, 0, ?0, 0, L0, 0, V0 in # " tree o5 order

Page 129: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 129/139

© 2012 UPESJul 2012Jul 2012

Page 130: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 130/139

© 2012 UPESJul 2012Jul 2012

+eletion

Page 131: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 131/139

© 2012 UPESJul 2012Jul 2012

-%#te*er 4e t%e situ#tion, deletion o5 node must ensure t%e

roerties o5 # " tree) T%e m#in t3o roerties #re to 4e t#en c#re o5during deletion #re st#ted #s 4elo3:

1) T%e root node must %#*e #t le#st one ey *#lue)

2)  'll ot%er nodes !e$cet t%e root node& must %#*e #t le#st ⎡12 = 1⎤ey *#lues)

Case %' Deletion of a ke val#e fro" a leaf node

C + + % l f l l d t th b f ` ⎡ /⎤

Page 132: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 132/139

© 2012 UPESJul 2012Jul 2012

Case +4+$ %e1o3al of a ey 3al#e leads to the n#1ber of eys ` ⎡1/   ⎤ 6

+4

Case 1.2: Removal of key value leads to the number of keys < ⎡m/2  ⎤ – 1.

!n this case when a key value is removed the number of key value will be # ⎡m$2 &⎤

Page 133: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 133/139

© 2012 UPESJul 2012Jul 2012

!n this case" when a key value is removed" the number of key value will be # ⎡m$2 % &⎤

!f such a case occurs" then we have to move the key value from the sibling 'left or

right( of the node. Three situations may be possible in this case)

&. The nearest right sibling contains more than ⎡m$2 % & key values.⎤

2. The nearest left sibling contains more than ⎡m$2 % & key values.⎤

*. +either the nearest left sibling nor the right sibling contain more than ⎡m$2 % &⎤key values.

Page 134: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 134/139

© 2012 UPESJul 2012Jul 2012

e$t, let us consider t%e c#se o5 deletion o5 e 5rom t%e " tree #s s%o3n

in #4o*e igure "ot% o5 its le5t #nd rig%t si4lings cont#in only 2 ey*#lues in e#c%, so no \mo*e le5t] or \mo*e rig%t] is ossi4le in t%is c#se)

-%#t is reuired in t%is c#se is c#lled Zcom4ine[) T%is is illustr#ted in

igure

Page 135: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 135/139

© 2012 UPESJul 2012Jul 2012

Page 136: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 136/139

© 2012 UPESJul 2012Jul 2012

Case *' Deletion of a ke val#e fro" a non-leaf node

Page 137: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 137/139

© 2012 UPESJul 2012Jul 2012

". Tree

Page 138: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 138/139

© 2012 UPESJul 2012Jul 2012

". Tree *s) " tree

Page 139: unit-IV -

7/24/2019 unit-IV -

http://slidepdf.com/reader/full/unit-iv- 139/139

-e %#*e seen t%#t, ". tree structure imoses some o*er%e#d on

insertion #nd deletion #s 3ell #s some #dded s#ce o*er%e#d)

rom t%e er5orm#nce oint o5 *ie3, " tree is 4etter t%#n ". tree)

In ". tree, e*ery ey *#lues #e#rs in some o5 t%e le#5 node) 'lso, #

lin is m#int#ined #mong #ll t%e le#5 nodes so t%#t one c#n mo*e 5rom

t%e le5tmost le#5 node to t%e rig%tmost le#5 node) It is not %olds 5or "

tree)


Recommended