lecture 39: ( a,b )- and b-trees
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 39: ( a,b )- and B-Trees. Announcements. CSC213 final exam has been scheduled: Tuesday, May 10 from 10:15 – 12:15 in OM200 Lab mastery exam also on the schedule Thursday, May 12 from 2:45 – 3:45 in OM119. Problems with Search Trees. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/1.jpg)
LECTURE 39:(A,B)- AND B-TREES
CSC 213 – Large Scale Programming
![Page 2: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/2.jpg)
Announcements
CSC213 final exam has been scheduled:Tuesday, May 10 from 10:15 – 12:15 in OM200
Lab mastery exam also on the schedule Thursday, May 12 from 2:45 – 3:45 in OM119
![Page 3: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/3.jpg)
Problems with Search Trees
Great at organizing information for searching Processing is maintained at consistent O(log n) time
But sucks at locality (both spatial and temporal) Each node contains only 1 piece of data Jumps to child after using that piece of data All of these references means nodes
spaced randomly
![Page 4: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/4.jpg)
Big Search Trees
Excellent way to test roommates system
![Page 5: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/5.jpg)
Big Search Trees
Excellent way to test roommates system
![Page 6: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/6.jpg)
Big Search Trees
Excellent way to test roommates system
![Page 7: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/7.jpg)
(a,b) Trees to the Rescue!
Real solution to frequent hikes to Germany Linux & MacOS to track files & directories MySQL & other databases use this to hold
all the data Found in many other places where paging
occurs Simple rules define working of any (a,b)
tree Grows upward so that all leaves found at
same level At least a children for each internal node Every internal node has at most b children
![Page 8: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/8.jpg)
What is “the BTree?”
Common multi-way tree implementation Describe B-Tree using order (“BTree of order m”)
m/2 to m children per internal node Root node can have m or fewer elements
Many variants exist to improve some failing Each variant is specialized for some niche
use Minor differences only between each
variant Describes the most basic B-Tree during
lecture
![Page 9: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/9.jpg)
BTree Order
Select order minimizing paging when created Elements & references to kids in full node fills
page Nodes have at least m/2 elements, even at
their smallest In memory guarantees each page is at least
50% full How many pages touched by each
operation?
![Page 10: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/10.jpg)
Removal from BTree
Swap element with successor in parent of a leaf Process is similar to removal in (2,4) node
If under m/2 elements in node after the removal See if can move element from sibling to
parent & steal element from parent
Else, merge with sibling & steal element from parent But this might propagate underflow to parent
node!
![Page 11: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/11.jpg)
Removal from BTree
Swap element with successor in parent of a leaf Process is similar to removal in (2,4) node
If under m/2 elements in node after the removal See if can move element from sibling to
parent & steal element from parent
Else, merge with sibling & steal element from parent But this might propagate underflow to parent
node!
Remind anyone else of another structure?
![Page 12: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/12.jpg)
(2,4) Tree Is An (a,b) Tree
Grows upward so all leaves found at same level
At least a children for each internal node
Every internal node has at most b children
![Page 13: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/13.jpg)
Underflow and Fusion
Entry deletion may cause underflow Node less than ½ full after removing the Entry
Choice of solution depends on situation Example: remove(15)
15
9 14
10 112 5 7
![Page 14: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/14.jpg)
Underflow and Fusion
Entry deletion may cause underflow Node less than ½ full after removing the Entry
Choice of solution depends on situation Example: remove(15)
15
9 14
2 5 7 10 11
![Page 15: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/15.jpg)
Underflow and Fusion
Entry deletion may cause underflow Node less than ½ full after removing the Entry
Choice of solution depends on situation Example: remove(15)
15
9 14
2 5 7 10 11
![Page 16: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/16.jpg)
Underflow and Fusion
Entry deletion may cause underflow Node less than ½ full after removing the Entry
Choice of solution depends on situation Example: remove(15)
9 14
2 5 7 10 11
![Page 17: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/17.jpg)
Case 1: Transfer
Adjacent sibling Node has Entry to lend Steal parent’s Entry closest to underfilled
node Prevent loneliness & promote sibling’s Entry
No further processing needed in this case Example: remove(15)
9 14
2 5 7 10 11
![Page 18: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/18.jpg)
Case 1: Transfer
Adjacent sibling Node has Entry to lend Steal parent’s Entry closest to underfilled
node Prevent loneliness & promote sibling’s Entry
No further processing needed in this case Example: remove(15) 14
9
2 5 7 10 11
![Page 19: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/19.jpg)
Case 1: Transfer
Adjacent sibling Node has Entry to lend Steal parent’s Entry closest to underfilled
node Prevent loneliness & promote sibling’s Entry
No further processing needed in this case Example: remove(15) 14
9
2 5 7 10 11
![Page 20: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/20.jpg)
Case 1: Transfer
Adjacent sibling Node has Entry to lend Steal parent’s Entry closest to underfilled
node Prevent loneliness & promote sibling’s Entry
No further processing needed in this case Example: remove(15) 14
9 11
2 5 7 10
![Page 21: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/21.jpg)
Case 1: Transfer
Adjacent sibling Node has Entry to lend Steal parent’s Entry closest to underfilled
node Prevent loneliness & promote sibling’s Entry
No further processing needed in this case Example: remove(15) 14
9 11
2 5 7 10
![Page 22: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/22.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
14
9 11
2 5 7 10
![Page 23: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/23.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
9 11
102 5 7
![Page 24: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/24.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
9 11
102 5 7
![Page 25: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/25.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
9
10 112 5 7
![Page 26: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/26.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
9
10 112 5 7
![Page 27: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/27.jpg)
Case 2: Fusion
Emptied node has only ½ filled siblings Merge node & sibling into single nearly
filled node Look to parent & steal Entry between
siblings May propagate underflow to parent!
Example: remove(14)
Mom
9
10 112 5 7
![Page 28: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/28.jpg)
In Case Of Overflow…
If addition overfills node, split into 2 new nodes ½ of the Entrys (& children) for the new
nodes Splitting now makes sure nodes at least ½
full!
15 24
12 18 27 30 32 35
![Page 29: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/29.jpg)
In Case Of Overflow…
If addition overfills node, split into 2 new nodes ½ of the Entrys (& children) for the new
nodes Splitting now makes sure nodes at least ½
full!
15 24
12 18 27 30 32 35 12 27 3018 35
![Page 30: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/30.jpg)
In Case Of Overflow…
If addition overfills node, split into 2 new nodes ½ of the Entrys (& children) for the new
nodes Splitting now makes sure nodes at least ½
full!
15 24
12 18 27 30 32 35 12 18 35
15 24 32
27 30
![Page 31: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/31.jpg)
In Case Of Overflow…
If addition overfills node, split into 2 new nodes ½ of the Entrys (& children) for the new
nodes Splitting now makes sure nodes at least ½
full! Check parent for overflow, since added
1 Entry15 24
12 18 27 30 32 35 12 18 35
15 24 32
27 30
![Page 32: Lecture 39: ( a,b )- and B-Trees](https://reader036.vdocuments.site/reader036/viewer/2022081503/56816690550346895dda6962/html5/thumbnails/32.jpg)
For Next Lecture
Remember, must submit program #3 on Friday Should start today and work through the
week 2nd best debugging technique? Taking a
(short) break! Weekly activity due tomorrow
Come and ask me any questions you may have!
Final problem day in class on Wednesday
At end of lab time Friday, lab phase #4 due