SlideShare ist ein Scribd-Unternehmen logo
1 von 36
CS 6213 –
Advanced
Data
Structures
Lecture 4
BTREES
AN EXCELLENT DATA
STRUCTURE FOR DISK ACCESS
 Instructor
Prof. Amrinder Arora
amrinder@gwu.edu
Please copy TA on emails
Please feel free to call as well
 TA
Iswarya Parupudi
iswarya2291@gwmail.gwu.edu
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 2
LOGISTICS
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 3
CS 6213
Basics
Record /
Struct
Arrays / Linked
Lists / Stacks
/ Queues
Graphs / Trees
/ BSTs
Advanced
Trie, B-Tree
Splay Trees
R-Trees
Heaps and PQs
Union Find
 T.K.Prasad @ Purdue University
 Prof. Sin-Min Lee @ San Jose State University
 Rada Mihalcea @ University of North Texas
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 4
CREDITS
 Eventually you run out of RAM
 Plus, you need persistent storage
 Storing information on disk requires different
approach to efficiency
 Access time includes seek time and rotational delay
 Assuming that a disk spins at 3600 RPM, one
revolution occurs in 1/60 of a second, or 16.7ms.
 In other words, one disk access takes about the
same time as 200,000 instructions
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 5
MOTIVATION FOR B-TREES
 Assume that we use an AVL tree to store about 20 million
records
 log2 20,000,000 is about 24
 24 operations in terms of time is very small (4 GHz CPU,
etc).
 Normal data operation should take a few nanoseconds.
 However, a large binary tree in a file will cause lots of
different disk accesses
 24 * 16.7ms = 0.4 seconds
 Suddenly database query response time in seconds starts
making sense.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 6
MOTIVATION FOR B-TREES (CONT.)
 We can’t improve the theoretical log n lower bound
on search for a binary tree
 But, the solution is to use more branches and thus
reduce the height of the tree!
 As branching increases, depth decreases
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 7
MOTIVATION FOR B-TREES (CONT.)
 Invented by Bayer and McCreight in 1972
 (Bayer also invented Red Black Trees)
 Definition is in terms of “order”, which is not always
clear, and different researchers mean different
things, but concepts remain the same.
 We will use Knuth’s terminology, where order
represents the maximum number of children.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 8
B-TREES
 B-tree of order m (where m is an odd number) is an
m-way search tree, where keys partition the keys in
the children in the fashion of a search tree, with
following additional constraints:
1. [max] a node contains up to m – 1 keys and up to m children
(Actual number of keys is one less than the number of
children)
2. [min] all non-root nodes contain at least (m-1)/2 keys
3. [leaf level] all leaves are on the same level
4. [root] the root is either a leaf node, or it has at least two
children
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 9
B-TREE: DEFINITION
 While as per Knuth’s definition B-Tree of order 5 is a
tree where a node has a maximum of 5 children
nodes, the same tree may be defined as a [2,4] tree
in the sense that for any node, the number of keys is
between 2 and 4, both inclusive.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 10
B-TREE: ALTERNATE DEFINITION
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 11
AN EXAMPLE B-TREE
51 6242
6 12
35
55 60 7564 9245
1 2 4 7 8 13 15 18 32
38 40 46 48 53
A B-tree of order 5:
• Root has at least 2 children
• Every other non-leaf node has at
least 2 keys and 3 children
• Each leaf has at least two keys
• All leaves are at same level.
61
 Different approach compared AVL Trees
 Don’t insert a new leaf, rather split the root and add
a new level above the root. This automatically
increases the height of ALL the leaves by one.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 12
KEEPING THE HEIGHT SAME
 We want to construct a B-tree of order 5
 Suppose we start with an empty B-tree and keys
arrive in the following order: 1 12 8 2 25 5 14 28
17 7 52 16 48 68 3 26 29 53 55 45
 The first four items go into the root:
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 13
CONSTRUCTING A B-TREE
1 2 8 12
 To put the fifth item in the root would violate
constraint 1 (max)
 Therefore, when 25 arrives, we pick the middle key
to make a new root
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 14
CONSTRUCTING A B-TREE (CONT.)
1 2
8
12 25
 6, 14, 28 get added to the leaf nodes
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 15
CONSTRUCTING A B-TREE (CONT.)
1 2
8
12 146 25 28
 Adding 17 to the right leaf node would violate
constraint 1 (max), so we promote the middle key
(17) to the root and split the leaf
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 16
CONSTRUCTING A B-TREE (CONT.)
8 17
12 14 25 281 2 6
 7, 52, 16, 48 get added to the leaf nodes
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 17
CONSTRUCTING A B-TREE (CONT.)
8 17
12 14 25 281 2 6 16 48 527
 Adding 68 causes us to split the right most leaf,
promoting 48 to the root, and adding 3 causes us to
split the left most leaf, promoting 3 to the root; 26,
29, 53, 55 then go into the leaves
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 18
CONSTRUCTING A B-TREE (CONT.)
3 8 17 48
52 53 55 6825 26 28 291 2 6 7 12 14 16
 Adding 45 causes a split of
 But we observe that this does not cause the problem
of leaves at different heights.
 Rather, we promote 28 to go to the root.
 However, root is already full:
 So, this causes the root to split: 17 then becomes the
new root.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 19
CONSTRUCTING A B-TREE (CONT.)
25 26 28 29
3 8 17 48
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 20
CONSTRUCTING A B-TREE (CONT.)
17
3 8 28 48
1 2 6 7 12 14 16 52 53 55 6825 26 29 45
 Attempt to insert the new key into a leaf
 If this would result in that leaf becoming too big,
split the leaf into two, promoting the middle key to
the leaf’s parent
 If this would result in the parent becoming too big,
split the parent into two, promoting the middle key
 This strategy might have to be repeated all the way
to the top
 If necessary, the root is split in two and the middle
key is promoted to a new root, making the tree one
level higher
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 21
SUMMARY: INSERTING INTO A B-TREE
 Insert the following keys to a 5-way B-tree:
 13, 27, 51, 3, 2, 14, 28, 1, 7, 71, 89, 37, 41, 44
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 22
EXERCISE IN INSERTING A B-TREE
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 23
REMOVAL FROM A B-TREE – 4
SCENARIOS
Scenario 1:
• Key to delete is a leaf node, and removing it doesn’t
cause that leaf node to have too few keys, then simply
remove the key to be deleted.
Scenario 2:
• Key to delete is not in a leaf and moving its successor or
predecessor does not cause the leaf node to have too
few keys. (We are guaranteed by the nature of a B-tree
that its predecessor or successor will be in a leaf.)
Scenario 3:
• Key to delete is a leaf node, but deleting it will have the
leaf to have too few keys, and we can borrow from an
adjacent leaf node.
Scenario 4:
• Key to delete is a leaf node, but deleting it will have the
leaf to have too few keys, and we cannot borrow from an
adjacent leaf node. Then the lacking leaf and one of its
neighbours can be combined with their shared parent
(the opposite of promoting a key) and the new leaf will
have the correct number of keys; if this step leave the
parent with too few keys then we repeat the process up
to the root itself, if required
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 24
SIMPLE LEAF DELETION
12 29 52
2 7 9 15 22 56 69 7231 43
We want to delete 2:
Since there are enough
keys in the node, we can just
delete it
Scenario1
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 25
SIMPLE LEAF DELETION (CONT.)
12 29 52
7 9 15 22 56 69 7231 43
That’s it, we deleted 2 and we
are done.
Scenario1
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 26
SIMPLE NON-LEAF DELETION
12 29 52
7 9 15 22 56 69 7231 43
Borrow the predecessor
or (in this case) successor
We want to delete 52. So, we
delete it, and see that the
successor can be moved up.
Scenario2
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 27
SIMPLE NON-LEAF DELETION (CONT.)
12 29 56
7 9 15 22 69 7231 43
Done. 52 is gone. 56
promoted to the non-leaf
node. Leaf nodes are still
meeting the min constraint.
Scenario2
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 28
TOO FEW KEYS IN NODE, BUT WE
CAN BORROW FROM SIBLINGS
12 29
7 9 15 22 695631 43
We want to delete 22 – that will
lead to too few keys in the node
(constraint 2). But we can borrow
from the adjacent node (via the
root).
Demote root key and
promote leaf key
Scenario3
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 29
TOO FEW KEYS IN NODE, BUT WE CAN
BORROW FROM SIBLINGS (CONT.)
12
297 9 15
31
695643
Done – 22 is gone. 29 came
down from the parent node, and
31 has gone up from the right
adjacent node.
Scenario3
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 30
TOO FEW KEYS IN NODE AND ITS
SIBLINGS
12 29 56
7 9 15 22 69 7231 43
We want to delete 72. This will lead to too few keys in
this node (constraint 2). We cannot borrow from the
adjacent sibling as it only has two. So, we need to
combine 31, 43, 56 and 69 into one node.
Scenario4
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 31
TOO FEW KEYS IN NODE AND ITS
SIBLINGS (CONT.)
12 29
7 9 15 22 695631 43
Done. 72 is gone. 31, 43, 56 and 69
combined into one node.
Scenario4
 The maximum number of items in a B-tree of order m and
height h:
root m – 1
level 1 m(m – 1)
level 2 m2(m – 1)
. . .
level h mh(m – 1)
 So, the total number of items is
(1 + m + m2 + m3 + … + mh)(m – 1) =
[(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1
 When m = 5 and h = 2 this gives 53 – 1 = 124
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 32
ANALYSIS OF B-TREES
 Since there is a lower bound on the number of child
nodes of non-root nodes, a B-Tree is at least 50%
“full”.
 On average it is 75% full.
 The advantage of not being 100% full is that there
are empty spaces for insertions to happen without
going all the way to the root.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 33
ANALYSIS OF B-TREES (CONT.)
 If m = 3, the specific case of B-Tree is called a 2-3
tree.
 For in memory access, 2-3 Tree may be a good
alternative to Red Black or AVL Tree.
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 34
2-3 TREES
 For small in-memory data structures, BSTs, Arrays, Hashmaps,
etc. work well.
 When we exceed the size of the RAM, or for persistence
reasons, the problem becomes quite different.
 The cost of each disc transfer is high but doesn't depend much on the
amount of data transferred, especially if adjacent items are transferred
 B-Trees are a great alternative (and very highly used) data
structure for disk accesses
 A B-Tree of order m allows each node to have from m/2 up to m children.
 There is flexibility that allows for gaps. This flexibility allows: (i) some
new elements to be stored in leaves with no other changes, and (ii) some
elements to be deleted easily without changes propagating to root
 If we use a B-tree of order 101, a B-tree of order 101 and height 3 can
hold 1014 – 1 items (approximately 100 million) and any item can be
accessed with 3 disc reads (assuming we hold the root in memory)
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 35
CONCLUSIONS &
RECAP OF CENTRAL IDEA
 If we take m = 3, we get a 2-3 tree, in which non-leaf
nodes have two or three children (i.e., one or two
keys)
 B-Trees are always balanced (since the leaves are all at the
same level), so 2-3 trees make a good type of balanced tree
L4 - BTrees CS 6213 - Advanced Data Structures - Arora 36
CONCLUSIONS AND RECAP (CONT.)

Weitere ähnliche Inhalte

Was ist angesagt?

A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Treezhaokatherine
 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Shubham Shukla
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxArbabMaalik
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIMohamed Loey
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheetNishant Upadhyay
 
Normalization PRESENTATION
Normalization PRESENTATIONNormalization PRESENTATION
Normalization PRESENTATIONbit allahabad
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis Peter Reimann
 
K-Means clustring @jax
K-Means clustring @jaxK-Means clustring @jax
K-Means clustring @jaxAjay Iet
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common SubsequenceSwati Swati
 

Was ist angesagt? (20)

A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Complexity analysis in Algorithms
Complexity analysis in AlgorithmsComplexity analysis in Algorithms
Complexity analysis in Algorithms
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Minimum spanning Tree
Minimum spanning TreeMinimum spanning Tree
Minimum spanning Tree
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptx
 
Kruskal's algorithm
Kruskal's algorithmKruskal's algorithm
Kruskal's algorithm
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Heap
HeapHeap
Heap
 
Counting sort
Counting sortCounting sort
Counting sort
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Python matplotlib cheat_sheet
Python matplotlib cheat_sheetPython matplotlib cheat_sheet
Python matplotlib cheat_sheet
 
Normalization PRESENTATION
Normalization PRESENTATIONNormalization PRESENTATION
Normalization PRESENTATION
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis
 
K-Means clustring @jax
K-Means clustring @jaxK-Means clustring @jax
K-Means clustring @jax
 
Longest Common Subsequence
Longest Common SubsequenceLongest Common Subsequence
Longest Common Subsequence
 

Andere mochten auch

Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisAmrinder Arora
 
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Amrinder Arora
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An IntroductionAmrinder Arora
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsAmrinder Arora
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresAmrinder Arora
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsAmrinder Arora
 

Andere mochten auch (9)

Algorithmic Puzzles
Algorithmic PuzzlesAlgorithmic Puzzles
Algorithmic Puzzles
 
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity AnalysisEuclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
Euclid's Algorithm for Greatest Common Divisor - Time Complexity Analysis
 
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
Convex Hull - Chan's Algorithm O(n log h) - Presentation by Yitian Huang and ...
 
Online Algorithms - An Introduction
Online Algorithms - An IntroductionOnline Algorithms - An Introduction
Online Algorithms - An Introduction
 
Binomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci HeapsBinomial Heaps and Fibonacci Heaps
Binomial Heaps and Fibonacci Heaps
 
Splay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data StructuresSplay Trees and Self Organizing Data Structures
Splay Trees and Self Organizing Data Structures
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of PointsDivide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
 

Ähnlich wie BTrees - Great alternative to Red Black, AVL and other BSTs

Presentation on b trees [autosaved]
Presentation on b trees [autosaved]Presentation on b trees [autosaved]
Presentation on b trees [autosaved]AKASHKUMAR1542
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharyaJash Acharya
 
2-3 Tree, Everything you need to know
2-3 Tree,  Everything you need to know2-3 Tree,  Everything you need to know
2-3 Tree, Everything you need to knowLittbarski Santos
 
16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structureSadiaSharmin40
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.pptzalanmvb
 
btrees.ppt ttttttttttttttttttttttttttttt
btrees.ppt  tttttttttttttttttttttttttttttbtrees.ppt  ttttttttttttttttttttttttttttt
btrees.ppt tttttttttttttttttttttttttttttRAtna29
 
2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptx2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptxujjwalmatoliya
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureMeghaj Mallick
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresAmrinder Arora
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data StructuresAmrinder Arora
 

Ähnlich wie BTrees - Great alternative to Red Black, AVL and other BSTs (20)

Presentation on b trees [autosaved]
Presentation on b trees [autosaved]Presentation on b trees [autosaved]
Presentation on b trees [autosaved]
 
B tree by-jash acharya
B tree by-jash acharyaB tree by-jash acharya
B tree by-jash acharya
 
2-3 Tree, Everything you need to know
2-3 Tree,  Everything you need to know2-3 Tree,  Everything you need to know
2-3 Tree, Everything you need to know
 
16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure16807097.ppt b tree are a good data structure
16807097.ppt b tree are a good data structure
 
BTrees-fall2010.ppt
BTrees-fall2010.pptBTrees-fall2010.ppt
BTrees-fall2010.ppt
 
B trees
B treesB trees
B trees
 
unit ii.pptx
unit ii.pptxunit ii.pptx
unit ii.pptx
 
Btree
BtreeBtree
Btree
 
btrees.ppt ttttttttttttttttttttttttttttt
btrees.ppt  tttttttttttttttttttttttttttttbtrees.ppt  ttttttttttttttttttttttttttttt
btrees.ppt ttttttttttttttttttttttttttttt
 
B trees2
B trees2B trees2
B trees2
 
DATASTORAGE.pdf
DATASTORAGE.pdfDATASTORAGE.pdf
DATASTORAGE.pdf
 
DATASTORAGE
DATASTORAGEDATASTORAGE
DATASTORAGE
 
2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptx2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptx
 
DATASTORAGE.pptx
DATASTORAGE.pptxDATASTORAGE.pptx
DATASTORAGE.pptx
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
R-Trees and Geospatial Data Structures
R-Trees and Geospatial Data StructuresR-Trees and Geospatial Data Structures
R-Trees and Geospatial Data Structures
 
2 3 tree
2 3 tree2 3 tree
2 3 tree
 
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data StructuresStacks, Queues, Binary Search Trees -  Lecture 1 - Advanced Data Structures
Stacks, Queues, Binary Search Trees - Lecture 1 - Advanced Data Structures
 
B+ tree
B+ treeB+ tree
B+ tree
 
B trees
B treesB trees
B trees
 

Mehr von Amrinder Arora

Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchAmrinder Arora
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Amrinder Arora
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaAmrinder Arora
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Amrinder Arora
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Amrinder Arora
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine LearningAmrinder Arora
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part IIAmrinder Arora
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1Amrinder Arora
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAmrinder Arora
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationAmrinder Arora
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersAmrinder Arora
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackAmrinder Arora
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 

Mehr von Amrinder Arora (18)

NP-Completeness - II
NP-Completeness - IINP-Completeness - II
NP-Completeness - II
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First SearchGraph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
 
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
Bron Kerbosch Algorithm - Presentation by Jun Zhai, Tianhang Qiang and Yizhen...
 
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet MahanaArima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
Arima Forecasting - Presentation by Sera Cresta, Nora Alosaimi and Puneet Mahana
 
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
Stopping Rule for Secretory Problem - Presentation by Haoyang Tian, Wesam Als...
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
Proof of Cook Levin Theorem (Presentation by Xiechuan, Song and Shuo)
 
Online algorithms in Machine Learning
Online algorithms in Machine LearningOnline algorithms in Machine Learning
Online algorithms in Machine Learning
 
NP completeness
NP completenessNP completeness
NP completeness
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Greedy Algorithms
Greedy AlgorithmsGreedy Algorithms
Greedy Algorithms
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data StructuresAsymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
 
Set Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom FiltersSet Operations - Union Find and Bloom Filters
Set Operations - Union Find and Bloom Filters
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Learning to learn
Learning to learnLearning to learn
Learning to learn
 

Kürzlich hochgeladen

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Kürzlich hochgeladen (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

BTrees - Great alternative to Red Black, AVL and other BSTs

  • 1. CS 6213 – Advanced Data Structures Lecture 4 BTREES AN EXCELLENT DATA STRUCTURE FOR DISK ACCESS
  • 2.  Instructor Prof. Amrinder Arora amrinder@gwu.edu Please copy TA on emails Please feel free to call as well  TA Iswarya Parupudi iswarya2291@gwmail.gwu.edu L4 - BTrees CS 6213 - Advanced Data Structures - Arora 2 LOGISTICS
  • 3. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 3 CS 6213 Basics Record / Struct Arrays / Linked Lists / Stacks / Queues Graphs / Trees / BSTs Advanced Trie, B-Tree Splay Trees R-Trees Heaps and PQs Union Find
  • 4.  T.K.Prasad @ Purdue University  Prof. Sin-Min Lee @ San Jose State University  Rada Mihalcea @ University of North Texas L4 - BTrees CS 6213 - Advanced Data Structures - Arora 4 CREDITS
  • 5.  Eventually you run out of RAM  Plus, you need persistent storage  Storing information on disk requires different approach to efficiency  Access time includes seek time and rotational delay  Assuming that a disk spins at 3600 RPM, one revolution occurs in 1/60 of a second, or 16.7ms.  In other words, one disk access takes about the same time as 200,000 instructions L4 - BTrees CS 6213 - Advanced Data Structures - Arora 5 MOTIVATION FOR B-TREES
  • 6.  Assume that we use an AVL tree to store about 20 million records  log2 20,000,000 is about 24  24 operations in terms of time is very small (4 GHz CPU, etc).  Normal data operation should take a few nanoseconds.  However, a large binary tree in a file will cause lots of different disk accesses  24 * 16.7ms = 0.4 seconds  Suddenly database query response time in seconds starts making sense. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 6 MOTIVATION FOR B-TREES (CONT.)
  • 7.  We can’t improve the theoretical log n lower bound on search for a binary tree  But, the solution is to use more branches and thus reduce the height of the tree!  As branching increases, depth decreases L4 - BTrees CS 6213 - Advanced Data Structures - Arora 7 MOTIVATION FOR B-TREES (CONT.)
  • 8.  Invented by Bayer and McCreight in 1972  (Bayer also invented Red Black Trees)  Definition is in terms of “order”, which is not always clear, and different researchers mean different things, but concepts remain the same.  We will use Knuth’s terminology, where order represents the maximum number of children. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 8 B-TREES
  • 9.  B-tree of order m (where m is an odd number) is an m-way search tree, where keys partition the keys in the children in the fashion of a search tree, with following additional constraints: 1. [max] a node contains up to m – 1 keys and up to m children (Actual number of keys is one less than the number of children) 2. [min] all non-root nodes contain at least (m-1)/2 keys 3. [leaf level] all leaves are on the same level 4. [root] the root is either a leaf node, or it has at least two children L4 - BTrees CS 6213 - Advanced Data Structures - Arora 9 B-TREE: DEFINITION
  • 10.  While as per Knuth’s definition B-Tree of order 5 is a tree where a node has a maximum of 5 children nodes, the same tree may be defined as a [2,4] tree in the sense that for any node, the number of keys is between 2 and 4, both inclusive. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 10 B-TREE: ALTERNATE DEFINITION
  • 11. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 11 AN EXAMPLE B-TREE 51 6242 6 12 35 55 60 7564 9245 1 2 4 7 8 13 15 18 32 38 40 46 48 53 A B-tree of order 5: • Root has at least 2 children • Every other non-leaf node has at least 2 keys and 3 children • Each leaf has at least two keys • All leaves are at same level. 61
  • 12.  Different approach compared AVL Trees  Don’t insert a new leaf, rather split the root and add a new level above the root. This automatically increases the height of ALL the leaves by one. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 12 KEEPING THE HEIGHT SAME
  • 13.  We want to construct a B-tree of order 5  Suppose we start with an empty B-tree and keys arrive in the following order: 1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45  The first four items go into the root: L4 - BTrees CS 6213 - Advanced Data Structures - Arora 13 CONSTRUCTING A B-TREE 1 2 8 12
  • 14.  To put the fifth item in the root would violate constraint 1 (max)  Therefore, when 25 arrives, we pick the middle key to make a new root L4 - BTrees CS 6213 - Advanced Data Structures - Arora 14 CONSTRUCTING A B-TREE (CONT.) 1 2 8 12 25
  • 15.  6, 14, 28 get added to the leaf nodes L4 - BTrees CS 6213 - Advanced Data Structures - Arora 15 CONSTRUCTING A B-TREE (CONT.) 1 2 8 12 146 25 28
  • 16.  Adding 17 to the right leaf node would violate constraint 1 (max), so we promote the middle key (17) to the root and split the leaf L4 - BTrees CS 6213 - Advanced Data Structures - Arora 16 CONSTRUCTING A B-TREE (CONT.) 8 17 12 14 25 281 2 6
  • 17.  7, 52, 16, 48 get added to the leaf nodes L4 - BTrees CS 6213 - Advanced Data Structures - Arora 17 CONSTRUCTING A B-TREE (CONT.) 8 17 12 14 25 281 2 6 16 48 527
  • 18.  Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves L4 - BTrees CS 6213 - Advanced Data Structures - Arora 18 CONSTRUCTING A B-TREE (CONT.) 3 8 17 48 52 53 55 6825 26 28 291 2 6 7 12 14 16
  • 19.  Adding 45 causes a split of  But we observe that this does not cause the problem of leaves at different heights.  Rather, we promote 28 to go to the root.  However, root is already full:  So, this causes the root to split: 17 then becomes the new root. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 19 CONSTRUCTING A B-TREE (CONT.) 25 26 28 29 3 8 17 48
  • 20. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 20 CONSTRUCTING A B-TREE (CONT.) 17 3 8 28 48 1 2 6 7 12 14 16 52 53 55 6825 26 29 45
  • 21.  Attempt to insert the new key into a leaf  If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent  If this would result in the parent becoming too big, split the parent into two, promoting the middle key  This strategy might have to be repeated all the way to the top  If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher L4 - BTrees CS 6213 - Advanced Data Structures - Arora 21 SUMMARY: INSERTING INTO A B-TREE
  • 22.  Insert the following keys to a 5-way B-tree:  13, 27, 51, 3, 2, 14, 28, 1, 7, 71, 89, 37, 41, 44 L4 - BTrees CS 6213 - Advanced Data Structures - Arora 22 EXERCISE IN INSERTING A B-TREE
  • 23. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 23 REMOVAL FROM A B-TREE – 4 SCENARIOS Scenario 1: • Key to delete is a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. Scenario 2: • Key to delete is not in a leaf and moving its successor or predecessor does not cause the leaf node to have too few keys. (We are guaranteed by the nature of a B-tree that its predecessor or successor will be in a leaf.) Scenario 3: • Key to delete is a leaf node, but deleting it will have the leaf to have too few keys, and we can borrow from an adjacent leaf node. Scenario 4: • Key to delete is a leaf node, but deleting it will have the leaf to have too few keys, and we cannot borrow from an adjacent leaf node. Then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required
  • 24. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 24 SIMPLE LEAF DELETION 12 29 52 2 7 9 15 22 56 69 7231 43 We want to delete 2: Since there are enough keys in the node, we can just delete it Scenario1
  • 25. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 25 SIMPLE LEAF DELETION (CONT.) 12 29 52 7 9 15 22 56 69 7231 43 That’s it, we deleted 2 and we are done. Scenario1
  • 26. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 26 SIMPLE NON-LEAF DELETION 12 29 52 7 9 15 22 56 69 7231 43 Borrow the predecessor or (in this case) successor We want to delete 52. So, we delete it, and see that the successor can be moved up. Scenario2
  • 27. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 27 SIMPLE NON-LEAF DELETION (CONT.) 12 29 56 7 9 15 22 69 7231 43 Done. 52 is gone. 56 promoted to the non-leaf node. Leaf nodes are still meeting the min constraint. Scenario2
  • 28. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 28 TOO FEW KEYS IN NODE, BUT WE CAN BORROW FROM SIBLINGS 12 29 7 9 15 22 695631 43 We want to delete 22 – that will lead to too few keys in the node (constraint 2). But we can borrow from the adjacent node (via the root). Demote root key and promote leaf key Scenario3
  • 29. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 29 TOO FEW KEYS IN NODE, BUT WE CAN BORROW FROM SIBLINGS (CONT.) 12 297 9 15 31 695643 Done – 22 is gone. 29 came down from the parent node, and 31 has gone up from the right adjacent node. Scenario3
  • 30. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 30 TOO FEW KEYS IN NODE AND ITS SIBLINGS 12 29 56 7 9 15 22 69 7231 43 We want to delete 72. This will lead to too few keys in this node (constraint 2). We cannot borrow from the adjacent sibling as it only has two. So, we need to combine 31, 43, 56 and 69 into one node. Scenario4
  • 31. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 31 TOO FEW KEYS IN NODE AND ITS SIBLINGS (CONT.) 12 29 7 9 15 22 695631 43 Done. 72 is gone. 31, 43, 56 and 69 combined into one node. Scenario4
  • 32.  The maximum number of items in a B-tree of order m and height h: root m – 1 level 1 m(m – 1) level 2 m2(m – 1) . . . level h mh(m – 1)  So, the total number of items is (1 + m + m2 + m3 + … + mh)(m – 1) = [(mh+1 – 1)/ (m – 1)] (m – 1) = mh+1 – 1  When m = 5 and h = 2 this gives 53 – 1 = 124 L4 - BTrees CS 6213 - Advanced Data Structures - Arora 32 ANALYSIS OF B-TREES
  • 33.  Since there is a lower bound on the number of child nodes of non-root nodes, a B-Tree is at least 50% “full”.  On average it is 75% full.  The advantage of not being 100% full is that there are empty spaces for insertions to happen without going all the way to the root. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 33 ANALYSIS OF B-TREES (CONT.)
  • 34.  If m = 3, the specific case of B-Tree is called a 2-3 tree.  For in memory access, 2-3 Tree may be a good alternative to Red Black or AVL Tree. L4 - BTrees CS 6213 - Advanced Data Structures - Arora 34 2-3 TREES
  • 35.  For small in-memory data structures, BSTs, Arrays, Hashmaps, etc. work well.  When we exceed the size of the RAM, or for persistence reasons, the problem becomes quite different.  The cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if adjacent items are transferred  B-Trees are a great alternative (and very highly used) data structure for disk accesses  A B-Tree of order m allows each node to have from m/2 up to m children.  There is flexibility that allows for gaps. This flexibility allows: (i) some new elements to be stored in leaves with no other changes, and (ii) some elements to be deleted easily without changes propagating to root  If we use a B-tree of order 101, a B-tree of order 101 and height 3 can hold 1014 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory) L4 - BTrees CS 6213 - Advanced Data Structures - Arora 35 CONCLUSIONS & RECAP OF CENTRAL IDEA
  • 36.  If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)  B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree L4 - BTrees CS 6213 - Advanced Data Structures - Arora 36 CONCLUSIONS AND RECAP (CONT.)