DIT960: 2-3 Trees, AA Trees, B-Trees
Total Page:16
File Type:pdf, Size:1020Kb
2-3 trees, AA trees, B-trees Announcement Consultation times – 10-12, room ES61! ● Monday 9th May ● Thursday 12th May ● Thursday 19th May ● Monday 23rd May No plan – just "ome and as# questions %&S& don't forget there are e*er"ises on the +ebsite! 2-3 trees In a binary tree, ea"h node has t+o "hildren In a 2-3 tree, ea"h node has either 2 "hildren .a 2-node/ or 3 .a 3-node/ 0 2-node is a normal BST node: ● 4ne data value x, which is )reater than all values in the le(t su,tree and less than all values in the ri)ht su,tree 0 3-node is di6erent3 ● Two data 5alues x and y ● 0ll the values in the le(t su,tree are less than x ● 0ll the values in the middle su,tree are ,et+een x and y ● 0ll the values in the right su,tree are )reater than y 2-3 trees 0n e*am le of a 2-3 tree3 Why 2-3 trees? 7ith a 2-3 tree +e "an maintain the in5ariant3 ● The tree is always perfectly balanced! In5ariant3 all "hildren of ea"h node always ha5e the same hei)ht ● In arti"ular, any non-lea( 2-node has 2 children ● 0ny non-leaf 3-node has 3 children This wasn't ossi,le +ith binary sear"h trees Which of these are 2-3 trees? 8 8 2 11 19 2 11 19 1 3 : 9 9 13 18 19 1 9 13 18 19 8 8 2 11 19 2 11 19 1 9 9 13 18 19 1 9 9 18 19 Which of these are 2-3 trees? 8 8 2 11 19 2 11 19 1 3 : 9 9 13 18 19 1 9 13 18 19 4nly this one 8 .why?/ 8 2 11 19 2 11 19 1 9 9 13 18 19 1 9 9 18 19 Insertion into a 2-3 tree 2o insert a 5alue (e&). 4/ into a 2-3 tree, +e start by doin) a normal insertion&&& 8 2 11 19 1 9 9 13 18 19 : 7e bro#e the balan"e in5ariant! Insertion into a 2-3 tree 2o <* the balan"e in5ariant, we absorb the bad node into its parent! 8 2 11 19 1 4 5 9 13 18 19 : got a,sorbed into 5 Insertion into a 2-3 tree No+ su ose we want to insert 3& 7e'll absorb it into its parent as be(ore&&& 8 2 11 19 1 : 9 9 13 18 19 3 Insertion into a 2-3 tree 7e )et a 4-node, whi"h is not allo+ed! 7e <x this by a method "alled splitting& 8 2 11 19 1 3 4 5 9 13 18 19 Splitting a 4-node 2o )et rid of a :-node, we split it into se5eral 2-nodes! This creates an e*tra le5el in the tree& 7e +ill fix this by absor,in) the red node into its parent& y * y = * = 0 1 C > 0 1 C > Insertion into a 2-3 tree 0(ter s littin), +e absorb the ?e*tra le5el” node into its arent& 8 0,sor, 4 into 2& 2 11 19 1 4 9 13 18 19 3 5 Insertion into a 2-3 tree 7e restored the in5ariant! Aet's try insertin) 1B& 8 2 4 11 19 1 3 5 9 13 18 19 Insertion into a 2-3 tree Cirst add and absorb& 7e )ot a 4-node, s lit it& 8 2 : 11 19 1 3 9 9 13 18 18 19 Insertion into a 2-3 tree 0bsorb the e*tra node into the parent& 8 2 : 11 19 1 3 9 9 13 1B 18 19 Insertion into a 2-3 tree 7e )ot a 4-node a)ain, so s lit and absorb& 19 will be a,sor,ed 8 2 : 11 19 1B 1 3 9 9 13 18 19 Insertion into a 2-3 tree >one! .-( we insert e5en more, e5entually the root will s lit, whi"h adds a ne+ le5el to the tree&/ 8 19 2 : 11 1B 1 3 9 9 13 18 19 2-3 insertion algorithm Insert the ne+ node into the tree Then alternate 2 steps3 ● absorb the node into its arent, mo5e up to the arent ● if the node is a 4-node, split it into 2-nodes Stop on"e you don't need to s lit 2-3 trees, summary 2-3 trees do not use rotation, unlike ,alan"ed 1S2s – instead, they #eep the tree er(e"tly ,alan"ed ● -n5ariant maintained using absorption (to remove un+anted nodes/ and splitting (to eliminate 4-nodes/ Complexity is 4.lo) n), as tree is er(e"tly ,alan"ed Con"eptually mu"h simpler than AVL trees! 1ut im lementation is really annoyin) 3. ● See T+oThree&hs AA trees AA trees AA trees implement a 2-3 tree usin) a BST! 0 2-node be"omes a BST node 0 3-node be"omes two BST nodes3 * y * 0 1 C 0 y 7e'll always translate a 3-node 1 C into a node and its right child AA trees, the plan An 00 tree is really a 2-3 tree, ,ut we store it in a binary sear"h tree ● 0 ,it like what we did (or ,inary heaps 7e'll need to add e*tra information to the nodes, and in5ariants, so that3 ● 0ny 00 tree must "orrespond to a 2-3 tree ● 7e "an tell +hether ea"h node in the tree is a 2-node, or part o( a 3-node Then we can adapt 2-3 insertion to 00 trees! ● Cor sear"hin), +e "an !ust use 1S2 sear"h AA trees 7e store +ith ea"h node a level, whi"h is the height of the "orres onding 2-3 tree node k+1 k+1 * y * k k+1 k k k 0 1 C 0 y Noti"e that * and y ha5e the k k same le5el. That's how +e "an 1 C tell they re resent a 3-node& 4ur in5ariant +ill talk a,out le5els& AA trees -f a node has the same level as its parent, +e'll dra+ them ne*t to ea"h other& k+1 k+1 k+1 * y * y k k k k k k 0 1 C 0 1 C This em hasises the le5els in the tree& 2-3 trees as AA trees Eere was the 2-3 tree from ,e(ore&&& 8 19 2 : 11 1B 1 3 9 9 13 18 19 2-3 trees as AA trees &&.and here is the corres ondin) A0 tree! 7e "an identi(y the 2- and 3-nodes ,y lookin) at the level of the nodes (ho+;/ 3 3 8 19 2 2 2 2 2 : 11 1B 1 1 1 1 1 1 1 1 3 9 9 13 18 19 AA trees 7e "an translate a 2-3 tree to an 00 tree 0nd, ,y loo#in) at the levels, we can go the other way ● -f a node has the same le5el as its right "hild, the two nodes to)ether ma#e a 3-node ● 4ther+ise it's a 2-node No+ we need an in5ariant to "he"# that3 ● 7e only ha5e 2-nodes and 3-nodes ● The le5els match the heights in the 2-3 tree ● The 2-3 tree is er(ectly balan"ed AA tree invariant, a first attempt 0n A0 tree only contains su,trees of the (ollo+in) shape: k+1 k+1 k+1 * * y k k k k k 0 1 0 1 C Noti"e that the level of */y must be e*a"tly one more than the level of AF1/C .+e consider null to ha5e a level of 0 – this means a leaf must ha5e a level o( 1/ AA tree invariant, part -t turns out to ,e better to ,rea# this in5ariant into pie"es, so that it says somethin) a,out ea"h BST node Cirst, the level of a "hild node in the BST must ,e either3 ● e$ual to the le5el of its arent, or ● one less than the le5el of its arent .+here the le5el of null is 0/ AA tree invariant, part 2 -f a node has the same level as its child, it must ,e the * y root of a 3-node& So +e "an say3 0 1 C ● 0 node's le5el must ,e )reater than its left "hild: le5el(node/ > le5el(node&left) ● 0nd also )reater than its right-right )ran"hild3 le5el(node/ > le5el(node&right&right) AA tree ! not allo"ed 1ad: mal(ormed 3- 1ad: 4-node (ri)ht node (le(t "hild at )rand"hild at same same hei)ht/ hei)ht/ * y * y = 0 1 C 0 1 C > 7e'll get these trees durin) insertion! AA tree invariant, summary 7e "onsider the level of null to be 0 Cor each node in the tree, the following must hold: ● The node's "hildren must ha5e a le5el either e$ual to or one less than the node itsel( ● le5el.node/ G le5el.node&le(t/ .* ← y not allowed) ● le5el.node/ G le5el.node&ri)ht&ri)ht/ .* → y → = not allowed) This implies that any lea( node has a level of 1 7e also ha5e the normal BS2 in5ariant! Why is this not an AA tree? 11's right child .null) has le5el 0, 3 3 should be 1 or 2 8 19 2 2 2 2 2 : 11 1B 1 1 1 1 1 1 1 3 9 9 18 19 Why is this not an AA tree? 8 is le(t child o( 19, has same le5el 3 3 8 19 2 2 2 2 2 : 11 1B 1 1 1 1 1 1 1 1 3 9 9 13 18 19 Why is this not an AA tree? Children o( 7 and 19 ha5e too small le5el 4 4 8 19 2 2 2 2 2 : 11 1B 1 1 1 1 1 1 1 1 3 9 9 13 18 19 Why is this not an AA tree? Aea( nodes ha5e le(t child null .height 0/, so their hei)ht 2 2 should be 1 8 19 1 1 1 1 2 : 11 1B 0 0 0 0 0 0 0 1 3 9 9 13 18 19 AA tree insertion 2o insert into an 00 tree, +e start +ith a normal 1S2 insertion& The ne+ node is a leaf so +e )ive it a le5el of 1& Note that its arent also has level 1 .+hy?/ -( +e are lu"#y the arent +as a 2-node and +e insert into the right of it, )i5ing a 3-node: * * y 4ther+ise, the in5ariant is bro#en& 1ut there are only two +ays it can break! #ase 1: skew Eere, +e ha5e inserted into the left of a 2- node, ,reakin) the in5ariant& 7e can <* it ,y doin) a right rotation! k+1 k+1 k+1 k+1 * y * y k k k k k k 0 1 C 0 1 C This operation is called skew.