Welome nge Li Gørt. everse tehing n isussion of exerises: 02110 nge Li Gørt 3 tehing ssistnts 8.00-9.15 Group work 9.15-9.45 isussions of your solutions in lss 10.00-11.15 Leture 11.15-11.45 Work on exerises in the new mteril 11.45-12.00 oun up Weekly ssignments (You hve to get 40 points + pss 2 implementtion exerises in orer to e le to tten the written exm). Prerequisites: 02105/02326 lgorithms n t trutures Thnk you to Kevin Wyne for inspirtion to slies 2 lne serh trees lne erh Trees 2-3-4 trees re-lk trees ynmi sets erh nsert elete Mximum Minimum uessor(x) (fin minimum element x) Preeessor(x) (fin mximum element x) eferenes: L hpter 13, lgorithms in Jv (hnout) This leture: 2-3-4 trees, re-lk trees ext time: ply trees 4
ynmi set implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) 2-3-4 trees n worst se h=n. n est se h= log n (fully lne inry tree) Toy: ow to keep the trees lne. 5 2-3-4 trees erhing in 2-3-4 tree 2-3-4 trees. llow noes to hve multiple keys. erh. Perfet lne. very pth from root to lef hs sme length. llow 1, 2, or 3 keys per noe 2-noe: one key, 2 hilren 3-noe: 2 keys, 3 hilren 4-noe: 3 keys, 4 hilren ompre serh key ginst keys in noe. in intervl ontining serh key ollow ssoite link (reursively) x. erh for L smller thn K lrger thn etween K n M O X etween K n M O X smller thn M G J L Q V Y Z G J L Q V Y Z foun L 7 8
nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. erh to ottom for key. smller thn K M O X x. nsert smller thn M O X G J L Q V Y Z G J L Q V Y Z 9 not foun 10 nsertion in 2-3-4 tree nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe nsertion in 2-3-4 tree nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe smller thn K lrger thn x. nsert X x. nsert smller thn M O X M O U lrger thn U G J L Q V Y Z G J L Q T Y Z fits here 11 X not foun12
nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. nsert. erh to ottom for key. erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe x. nsert X lrger thn smller thn K M O U lrger thn W x. nsert M O X lrger thn G J L Q T X Y Z G J L Q V Y Z X fits here 13 not foun 14 nsertion in 2-3-4 tree plitting 4-noe in 2-3-4 tree nsert. e: split the 4-noe to mke room G erh to ottom for key. 2-noe t ottom: onvert to 3-noe J 3-noe t ottom: onvert to 4-noe 4-noe t ottom:?? G J oes fit here! smller thn K oes not fit here x. nsert G M O X lrger thn Prolem: oesn t work if prent is 4-noe olution 1: plit the prent (n ontinue splitting J G J L Q V Y Z while neessry). olution 2: plit 4-noes on the wy own. oes not fit here! 15 16
plitting 4-noes in 2-3-4 tree nsertion in 2-3-4 tree e: split 4-noes on the wy own the tree. nsures lst noe is not 4-noe. Trnsformtions to split 4-noes: G J x y v G J x y v nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe not 4-noe G 4-noe t ottom:?? nvrint. urrent noe is not 4-noe. onsequene. nsertion t ottom is esy sine it's not 4-noe. G J x y v root G x y v x y J v G x. nsert not 4-noe M O X 4-noe G J L Q V Y Z x y v 17 18 nsertion in 2-3-4 tree nsertion in 2-3-4 tree nsert. nsert. erh to ottom for key. 2-noe t ottom: onvert to 3-noe erh to ottom for key. 2-noe t ottom: onvert to 3-noe 3-noe t ottom: onvert to 4-noe 3-noe t ottom: onvert to 4-noe 4-noe t ottom:?? 4-noe t ottom:?? x. nsert G M O X x. nsert G M O X J L Q V Y Z J L Q V Y Z 19 20
plitting 4-noes in 2-3-4 tree plitting 4-noes in 2-3-4 tree Lol trnsformtions tht work nywhere in the tree. Lol trnsformtions tht work nywhere in the tree x. plitting 4-noe tthe to 2-noe x. plitting 4-noe tthe to 3-noe Q Q - K Q W - K W - -G K Q W K W - -G -J L-P -V X-Z -J L-P -V X-Z -J L-P -V X-Z -J L-P -V X-Z oul e huge unhnge oul e huge unhnge 21 22 plitting 4-noes in 2-3-4 tree nsertion 2-3-4 trees Lol trnsformtions tht work nywhere in the tree. nsert U nsert G plitting 4-noe tthe to 4-noe never hppens when we split noes on the wy own the tree. U U nvrint. urrent noe is not 4-noe. nsert U nsert G plit nsert G nsert T plit G U G U G T U nsert T 23 24
elete minimum: elete minimum: minimum lwys in leftmost lef minimum lwys in leftmost lef f 3- or 4-noe: elete key f 3- or 4-noe: elete key x. elete minimum x. elete minimum M O X M O X is minimum G J L Q V Y Z elete G J L Q V Y Z 25 26 elete minimum: minimum lwys in leftmost lef e: On the wy own mintin the invrint tht urrent noe is not 2-noe. hil of root n root is 2-noe: f 3- or 4-noe: elete key 2-noe?? x. elete minimum x y on the wy own: w x y w or x y w r s x y w r s M O X G G or elete? G J L Q V Y Z x y w r s x y w r s 27 x y w x y w 28
elete minimum: elete minimum: minimum lwys in leftmost lef minimum lwys in leftmost lef f 3- or 4-noe: elete key f 3- or 4-noe: elete key 2-noe: split/merge on wy own. 2-noe: split/merge on wy own. x. elete minimum x. elete minimum not 2-noe M O X M O X 2-noe G J L Q V Y Z G J L Q V Y Z 29 30 elete minimum: minimum lwys in leftmost lef f 3- or 4-noe: elete key 2-noe: split/merge on wy own. x. elete minimum M O X M O X G J L Q V Y Z 31 G J L Q V Y Z 32
uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key M O X M O X G J L Q V Y Z 33 G J L Q V Y Z 34 uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K M O X M O X G J L Q V Y Z 35 G J L Q V Y Z 36
uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor in suessor M O X M O X G J L Q V Y Z G J L Q V Y Z 37 38 uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor not 2-noe in suessor not 2-noe M O X M O X 2-noe G J L Q V Y Z G J L Q V Y Z 39 40
uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor O X elete L from lef O X G J L M Q V Y Z G J L M Q V Y Z 41 42 uring serh mintin invrint tht urrent noe is not 2-noe uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor in suessor elete L from lef O X elete L from lef O X eple K with L G J M Q V Y Z G J M Q V Y Z 43 44
2-3-4 Tree: lne Property. ll pths from root to lef hve sme length. uring serh mintin invrint tht urrent noe is not 2-noe f key is in lef: elete key Key not in lef: reple with suessor (lwys lef in sutree) n elete suessor from lef. x. elete K in suessor elete L from lef eple K with L L O X G J M Q V Y Z 45 Tree height. Worst se: lg [ll 2-noes] est se: log 4 = 1/2 lg [ll 4-noes] etween 10 n 20 for million noes. etween 15 n 30 for illion noes. 46 ynmi set implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) e-lk trees 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) 47
e-lk tree (Guis-egewik, 1979) e-lk tree (Guis-egewik, 1979) epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. G K O G K O or onnetion etween 2-3-4 trees n re-lk trees: L L or L 49 50 e-lk tree (Guis-egewik, 1979) e-lk tree epresent 2-3-4 tree s inry serh tree Use olors on noes to represent 3- n 4-noes. onnetion etween 2-3-4 trees n re-lk trees: or Properties of re-lk trees: The root is lwys lk ll root-to-lef pths hve the sme numer of lk noes. e noes o not hve re hilren L: ll leves (L) re lk 51 52
e-lk tree onnetion etween 2-3-4 trees n re-lk trees: e-lk tree onnetion etween 2-3-4 trees n re-lk trees: 53 54 e-lk tree onnetion etween 2-3-4 trees n re-lk trees: nsertion in re-lk trees nsertion in 2-noe: nsert or nsertion in 3-noe: nsert 55 56
e-lk tree: plitting 4-noes nsertion in re-lk trees Two esy ses: M M M M nsertion in 3-noe (ontinue): nsert M M M M nsert nsert???? 57 58 e-lk trees: plitting of 4-noes xmple of hr se: ottions in re-lk trees Two types of rottions L K L K K?? olution: ottions! 60
ottions in re-lk trees Two types of rottions: nsertion in re-lk trees nsertion in 3-noe: nsert?? nsert?? 61 62 nsertion in re-lk trees nsertion in 3-noe: nsertion in re-lk trees nsertion in 3-noe: nsert nsert nsert?? nsert 63 64
nsert x: erh to ottom fter key (x) nsert re lef lne: 3 ses (+ symmetri) nsertion in re-lk tree 65 Keep lning with 66 ksempel U nsert U nsert V U V V U otte U 67 xmple U V nsert G U V G U V G U V G U V G otte otte unning times in re-lk trees Time for insertion: erh to ottom fter key: nsert re lef: Perform reoloring n rottions on wy up: n reolor mny times (ut t most h) t most 2 rottions. Totl O(h). Time for serh me s T: O(h) eight: O(log n) 68 O(h) O(1) O(h)
ynmi set implementtions lne trees: implementtions Worst se running times mplementtion serh insert elete minimum mximum suessor preeessor linke lists O(n) O(1) O(1) O(n) O(n) O(n) O(n) orere rry O(log n) O(n) O(n) O(1) O(1) O(log n) O(log n) T O(h) O(h) O(h) O(h) O(h) O(h) O(h) elk trees: Jv: jv.util.treemp, jv.util.treeet. ++ TL: mp, multimp, multiset. Linux kernel: linux/rtree.h. 2-3-4 tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) re-lk tree O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) O(log n) 69 70 xerise time trt working on the exerises on weekpln T. 11.45 tlk out solutions to the first 2-3 exerises + info out the mntory exerises. Mntory exerise To e hne in efore 8PM the following uny on TU Lern. This week: inry tries from weekpln wrmup. 71 72
Gring of mntory ssignments You n get up to 20 points for n exerise: 5 points for the exmple (rw tree/run lgorithm et) 15 points for esign n lgorithm exerise: 4 points for the esription of your lgorithm 4 points for the qulity of your lgorithm 4 points for the omplexity nlysis 3 points for the orretness rguments 73