t0 TREES Hanan Samet ompute Science epatment and ente fo utomation Reseach and Institute fo dvanced ompute Studies Univesity of Mayland ollege Pak, Mayland 074 e-mail: hjs@umiacs.umd.edu opyight 1997 Hanan Samet These notes may not e epoduced y any means (mechanical o electonic o any othe) without the expess witten pemission of Hanan Samet
TREE EFINITION t1 TREE a anching stuctue etween nodes finite set T of one o moe nodes such that: 1. one element of the set is distinguished, ROOT(T). the emaining nodes of T ae patitioned into m 0 disjoint sets T 1, T, T m and each of these sets is in tun a tee. tees T 1, T, T ae the sutees of the oot Recusive definition easy to pove theoems aout popeties of tees. Ex: pove tue fo 1 node assume tue fo n nodes pove tue fo n+1 nodes ORERE TREE if the elative ode of the sutees T 1, T, T is impotant ORIENTE TREE ode is not impotant m m odeed? = E E ompute epesentation odeed! opyight 1998 y Hanan Samet
TERMINOLOGY 1 t E F G ounteintuitive! EGREE nume of sutees of a node Teminal node leaf degee 0 RNH NOE non-teminal node Root is the fathe of the oots of its sutees Roots of sutees of a node ae othes Roots of sutees of a node ae sons of the node The oot of the tee has no fathe! is an ancesto of, E, G, G is a descendant of level(x) if fathe(x)=ω then 0 else 1+level(fathe(X)); Ex: level(g) = 1+level(F) 1+level() 1+level() 0 opyight 1998 y Hanan Samet
TERMINOLOGY 1 t level 0 ounteintuitive! E F G level 1 level level 3 EGREE nume of sutees of a node Teminal node leaf degee 0 RNH NOE non-teminal node Root is the fathe of the oots of its sutees Roots of sutees of a node ae othes Roots of sutees of a node ae sons of the node The oot of the tee has no fathe! is an ancesto of, E, G, G is a descendant of level(x) if fathe(x)=ω then 0 else 1+level(fathe(X)); Ex: level(g) = 1+level(F) 1+level() 1+level() 0 opyight 1998 y Hanan Samet
FORESTS N INRY TREES t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? opyight 1998 y Hanan Samet
FORESTS N INRY TREES t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? NO! n entiely diffeent concept 1 and ae diffeent inay tees opyight 1998 y Hanan Samet
FORESTS N INRY TREES 3 z t3 FOREST a set (usually odeed) of 0 o moe disjoint tees, o equivalently: the nodes of a tee excluding the oot has the foest E F E F INRY TREE a finite set of nodes which eithe is empty o a oot and two disjoint inay tees called the left and ight sutees of the oot Is a inay tee a special case of a tee? NO! n entiely diffeent concept 1 and 1 has an empty ight sutee has an empty left sutee ut as tees 1 and ae identical! ae diffeent inay tees opyight 1998 y Hanan Samet
OTHER REPRESENTTIONS OF TREES t4 Nested sets (also known as ule diagams ) G E F G E F Nested paentheses Tee (oot sutee 1 sutee... sutee n ) ( ( () ()) (G (E (F)))) inay tee Indentation G E F (oot left ight) ( ( ( () ()) ( () ())) (G (E (F () ()) ()) ())) ewey decimal notation:.1...3.4.5 opyight 1998 y Hanan Samet
t5 PPLITIONS Segmentation of lage ectangula aays [n,m] each ow is a segment (uoughs computes) [1] [] [n] [1,1] [1,] [1,m] [n,1] [n,] [n,m] lgeaic fomulas opeato opeand opeand +(( ) ) + 1. no need fo paentheses ut + = ( )+ (+). code geneation LW 1, LW, W, MW, W,1 opyight 1998 y Hanan Samet
LISTs (with a capital L!) t6 LIST a finite sequence of 0 o moe atoms o LISTS L=(,(,,),(),,((()))) () empty list Index notation: L[]=(,,) L[,1]= L[5,] L[5,1,1] iffeences etween LISTS and tees: 1. no data appeas in the nodes epesenting LISTS - i.e.,. LISTS may e ecusive M=(M) [M] Lael [M] 3. LISTS may ovelap (i.e., need not e disjoint) equivalently, sutees may e shaed N=(M,M,,N) [N] [M] [M] [N] [M] opyight 1998 y Hanan Samet
TRVERSING INRY TREES t7 Repesentation LLINK INFO RLINK Ω Ω Ω Ω Ω Ω Ω pplications: 1. code geneation in compiles. game tees in atificial intelligence 3. detect if a stuctue is eally a tee TREE one path fom each node to anothe node (unlike gaph) no cycles and opyight 1998 y Hanan Samet
TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G H F J inode = postode = Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet
TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet
TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation 3 z t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = I K E G H F J Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet
TRVERSL ORERS 1. Peode oot, left sutee, ight sutee depth-fist seach. Inode left sutee, oot, ight sutee inay seach tee 3. Postode left sutee, ight sutee, oot code geneation 4 g 3 z t8 inay seach tee: left < oot < ight 30 15 45 10 0 inode yields 10 15 0 30 45 Ex: peode = I K E G F H J I K E G H F J inode = postode = I K E G H F J I K G E H J F Inode tavesal equies a stack to go ack up the tee: opyight 1998 y Hanan Samet
INORER TRVERSL LGORITHM t9 pocedue inode(tee pointe T); egin stack ; tee pointe P; Ω; P T; while not(p=ω and =Ω) do egin if P=Ω then egin P ; /* Pop the stack */ visit(root(p)); P RLINK(P); end else egin P; /* Push on the stack */ P LLINK(P); Using ecusion: pocedue inode(tee pointe T); egin if T=Ω then etun else egin inode(llink(t)); visit(root(t)); inode(rlink(t)); opyight 1998 y Hanan Samet
THREE INRY TREES inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee t10 Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet
THREE INRY TREES t10 inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet
THREE INRY TREES 3 z t10 inay tee epesentation has too many Ω links Use 1-it tag fields to indicate pesence of a link If Ω link, then use field to stoe links to othe pats of the stuctue to aid the tavesal of the tee Untheaded: Theaded: LLINK(p) = Ω LTG(p) = 0, LLINK(p) = $p = inode pedecesso of p LLINK(p) = q Ω LTG(p) = 1, LLINK(p) = q RLINK(p) = Ω RTG(p) = 0, RLINK(p) = p$ = inode successo of p RLINK(p) = q Ω RTG(p) = 1, RLINK(p) = q LLINK LTG INFO RTG RLINK Ex: HE E F G H J If addess of ROOT(T) < addess of left and ight sons, then don't need the TG fields Theads will point to lowe addesses! opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES Find the inode successo of node P (P$) t11 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES Find the inode successo of node P (P$) t11 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES 5 v 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
OPERTIONS ON THREE INRY TREES 6 5 v 4 g 3 z t11 Find the inode successo of node P (P$) 1. Q RLINK(P); /* ight thead points to P$ */. if RTG(P)=1 then egin /* not a thead */ while LTG(Q)=1 do Q LLINK(Q); Inset node Q as the ight sutee of node P P P Q Q 1. RLINK(Q) RLINK(P); RTG(Q) RTG(P); RLINK(P) Q; LLINK(Q) P; RTG(P) 1; LTG(Q) 0;. if RTG(Q)=1 then LLINK(Q$) Q; opyight 1998 y Hanan Samet
SUMMRY OF THREING t1 1. dvantages no need fo a stack fo tavesal will not un out of memoy duing inode tavesal can find inode successo of any node without having to tavese the entie tee. isadvantages insetion and deletion of nodes is slowe can t shae common sutees in the theaded epesentation Ex: two choices fo the inode successo of F G? G E F E F E F 3. Right-theaded tees inode algoithms make little use of left theads LTG(P)=1 test can e eplaced y LLINK(P)=Ω test opyight 1998 y Hanan Samet
PRINIPLES OF REURSION t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? opyight 1998 y Hanan Samet
PRINIPLES OF REURSION t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not opyight 1998 y Hanan Samet
PRINIPLES OF REURSION 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F opyight 1998 y Hanan Samet
PRINIPLES OF REURSION 4 g 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F if then T else simila(t 1,T ) = if empty(t 1 ) then if empty(t ) then T else F else if empty(t ) then F else if simila(left(t 1 ),left(t )) then simila(ight(t 1 ),ight(t )) else F ; opyight 1998 y Hanan Samet
PRINIPLES OF REURSION 5 v 4 g 3 z t13 Two inay tees T1 and T ae said to e simila if they have the same shape o stuctue Fomally: 1. they ae oth empty o?. they ae oth non-empty and thei left and ight sutees espectively ae simila simila(t 1,T ) = if empty(t 1 ) and empty(t ) then T else if empty(t 1 ) o empty(t ) then F else simila(left(t 1 ),left(t )) and simila(ight(t 1 ),ight(t )); Will simila wok? No! ase case does not handle case when one of the tees is empty and the othe one is not Simplifying: and = if then o = else F if then T else simila(t 1,T ) = if empty(t 1 ) then empty(t ) if empty(t ) then T else F else if empty(t ) then F and else if simila(left(t 1 ),left(t )) then simila(ight(t 1 ),ight(t )) else F ; opyight 1998 y Hanan Samet
EQUIVLENE OF INRY TREES t14 Two inay tees T1 and T ae said to e equivalent if they ae simila and coesponding nodes contain the same infomation? equivalent(t1,t) = if empty(t1) and empty(t) then T else if empty(t1) o empty(t) then F else oot(t1)=oot(t) and equivalent(left(t1),left(t)) and equivalent(ight(t1),ight(t)); opyight 1998 y Hanan Samet
EQUIVLENE OF INRY TREES t14 Two inay tees T1 and T ae said to e equivalent if they ae simila and coesponding nodes contain the same infomation? NO! we ae dealing with inay tees and the left sutee of is not the same in the two cases equivalent(t1,t) = if empty(t1) and empty(t) then T else if empty(t1) o empty(t) then F else oot(t1)=oot(t) and equivalent(left(t1),left(t)) and equivalent(ight(t1),ight(t)); opyight 1998 y Hanan Samet
REURSION SUMMRY t15 voids having to use an explicit stack in the algoithm Polem fomulation is analogous to induction ase case, inductive case Ex: Factoial n! = n (n 1)! fact(n) = if n=0 then 1 else n*fact(n-1); The esult is otained y peeling one s way ack along the stack fact(3) = 3*fact() *fact(1) 1*fact(0) 1 = 6 Using an accumulato vaiale and a call fact(n,1): fact(n,total) = if n=0 then total else fact(n-1,n*total); Solution is iteative Recusion implemented on compute using stack instuctions. ec-system 10: PUSH, POP, PUSHJ, POPJ Stack pointe fomat: (count, addess) an simulate stack if no stack instuctions opyight 1998 y Hanan Samet
OMPLETE INRY TREES t16 When a inay tee is easonaly complete (most Ω links ae at the highest level), use a sequential stoage allocation scheme so that links ecome unnecessay E F level 0 level 1 level 1 3 4 5 6 7 Ω E F If n is the highest level at which a node is found, then at most n+1 1 wods ae needed Stoage allocation method: 1. oot has addess 1. left son of x has addess addess(x) 3. ight son of x has addess addess(x) + 1 When should a complete inay tee e used? n = highest level of the tee at which a node is found x = # of nodes in tee 3 wods pe node (left link, ight link, info) use a complete inay tee when x > ( n+1 1) / 3 opyight 1998 y Hanan Samet
FORESTS t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode opyight 1998 y Hanan Samet
FORESTS t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G opyight 1998 y Hanan Samet
FORESTS 3 z t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G postode = K H E J F G opyight 1998 y Hanan Samet
FORESTS 4 g 3 z t17 foest is an odeed set of 0 o moe tees Thee exists a natual coespondence etween foests and inay tees E F G K H J Rigoous definition of (F) F = (T 1, T,, T n ) H T i,1, T i,,, T i,m ae sutees of T i 1. If n = 0, (F) is empty J. If n > 0, oot of (F) is oot(t 1 ) left sutee of (F) is (T 1,1, T 1,,, T 1,m ) ight sutee of (F) is (T, T 3,, T n ) K E F G Tavesal of foests peode: postode: 1. visit oot of fist tee 1. tavese sutees of. tavese sutees of fist tee in postode fist tee in peode. visit oot of fist tee 3. tavese emaining 3. tavese emaining sutees in peode sutees in postode peode = K E H F J G postode = K H E J F G inode of inay tee opyight 1998 y Hanan Samet
EQUIVLENE RELTION t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? opyight 1998 y Hanan Samet
EQUIVLENE RELTION t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? Yes, since 7 3 9 8 6 Patitions S into disjoint susets o equivalence classes Two elements equivalent iff they elong to same class What ae the equivalence classes in this example? opyight 1998 y Hanan Samet
EQUIVLENE RELTION 3 z t18 Given: elations as to what is equivalent to what (a ) Goal: is x y? Fomal definition of an equivalence elation 1. if x y and y z then x z (tansitivity). if x y then y x (symmety) 3. x x (eflexivity) Ex: S = {1.. 9} 1 5 6 8 7 9 8 3 7 4 9 3 is 6? Yes, since 7 3 9 8 6 Patitions S into disjoint susets o equivalence classes Two elements equivalent iff they elong to same class What ae the equivalence classes in this example? {1,5} and {,3,4,6,7,8,9} opyight 1998 y Hanan Samet
LGORITHM t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 5 1 8 7 3 4 fathe(k): k: 1 3 4 5 6 7 8 9 9 6 opyight 1998 y Hanan Samet
LGORITHM t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 k: 1 3 4 5 6 7 8 9 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet
LGORITHM 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 6 8 k: 1 3 4 5 6 7 8 9 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet
LGORITHM 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 6 8 k: 1 3 4 5 6 7 8 9 7 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet
LGORITHM 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 9 6 opyight 1998 y Hanan Samet
LGORITHM 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 9 6 opyight 1998 y Hanan Samet
LGORITHM 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 opyight 1998 y Hanan Samet
LGORITHM 8 g 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 9 3 opyight 1998 y Hanan Samet
LGORITHM 9 8 g 7 z 6 5 g 4 v 3 z t19 Repesent each element as a node in foest of tees Tees consist only of fathe links (nil at oots) Each (nonedundant) elation meges two tees into one asic stategy: fo each elation a do egin find oot node of tee containing a; /* Find step */ find oot node s of tee containing ; if they diffe, mege the two tees; /* Union step */ a s mege(a,) a s lgoithm (also known as union-find ): fo evey element i do fathe(i) Ω while input_not_exhausted do egin get_pai(a,); while fathe(a) Ω do a fathe(a); while fathe() Ω do fathe(); if (a ) then fathe(a) ; 1 5 5 fathe(k): 5 8 8 6 8 k: 1 3 4 5 6 7 8 9 7 9 8 1 8 7 3 4 3 7 4 9 6 9 3 Moe efficient with path compession and weight alancing Execution time almost linea (invese of ckemann function) opyight 1998 y Hanan Samet