SlideShare una empresa de Scribd logo
1 de 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.)

Más contenido relacionado

La actualidad más candente

Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
Ami Ranjit
 
Tree data structure
Tree data structureTree data structure
Tree data structure
Dana dia
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMS
koolkampus
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
emailharmeet
 

La actualidad más candente (20)

Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
 
ER MODEL
ER MODELER MODEL
ER MODEL
 
Learn Normalization in simple language
Learn Normalization in simple languageLearn Normalization in simple language
Learn Normalization in simple language
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithm
 
Normalization
NormalizationNormalization
Normalization
 
Normalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NF
Normalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NFNormalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NF
Normalization | (1NF) |(2NF) (3NF)|BCNF| 4NF |5NF
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Matrix chain multiplication by MHM
Matrix chain multiplication by MHMMatrix chain multiplication by MHM
Matrix chain multiplication by MHM
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
10.m way search tree
10.m way search tree10.m way search tree
10.m way search tree
 
Normalization
NormalizationNormalization
Normalization
 
INTRODUCTION TO INTERNET OF THINGS
INTRODUCTION TO INTERNET OF THINGSINTRODUCTION TO INTERNET OF THINGS
INTRODUCTION TO INTERNET OF THINGS
 
Introducing Data Redaction - an enabler to data security in EDB Postgres Adva...
Introducing Data Redaction - an enabler to data security in EDB Postgres Adva...Introducing Data Redaction - an enabler to data security in EDB Postgres Adva...
Introducing Data Redaction - an enabler to data security in EDB Postgres Adva...
 
Cracking the Coding Interview - 7 steps - Udacity
Cracking the Coding Interview - 7 steps - UdacityCracking the Coding Interview - 7 steps - Udacity
Cracking the Coding Interview - 7 steps - Udacity
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMS
 
Sorting
SortingSorting
Sorting
 
Galera Replication Demystified: How Does It Work?
Galera Replication Demystified: How Does It Work?Galera Replication Demystified: How Does It Work?
Galera Replication Demystified: How Does It Work?
 
Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation Bubble Sort Algorithm Presentation
Bubble Sort Algorithm Presentation
 
Lecture 04 normalization
Lecture 04 normalization Lecture 04 normalization
Lecture 04 normalization
 

Destacado

Destacado (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
 

Similar a 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
 
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
SadiaSharmin40
 
btrees.ppt ttttttttttttttttttttttttttttt
btrees.ppt  tttttttttttttttttttttttttttttbtrees.ppt  ttttttttttttttttttttttttttttt
btrees.ppt ttttttttttttttttttttttttttttt
RAtna29
 
2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptx2-3 tree ujjwal matoliya .pptx
2-3 tree ujjwal matoliya .pptx
ujjwalmatoliya
 

Similar a 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
 

Más de Amrinder Arora

Más de 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
 

Último

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
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
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
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
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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.
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
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 ...
 

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.)