Presented By:
Fatimah M. Alqadheeb
218002332
Content
 Introduction
 B-Tree Properties
 The Height of a B-Tree
 Basic Operation in a B-Tree
 Inserting
 Deleting
 Time Complexity
 Usage of B-Tree
 References
2
Quote of the Day!
3
Introduction:
 In case of too large data that could not be fit in a main memory
 So, that’s require to access the disk multiple of times
 Accessing the disk requires long time
 We need to reduce the number of disk access!
 The solution is to use the B-Tree, which gives more branches with
less height
4
B-Tree
 B-Tree: is a balanced rooted tree whose root is T root.
 Properties of B- Tree:
1. For every node x in B- Tree:
a. x.n the number of keys in x node
b. The x.n keys themselves x. key1, x. key2, …, x. keyn (increasing
order)
c. x.leaf is a Boolean value that is true if x is a leaf & false if x is an
internal node
x.n
x. 𝒌𝒆𝒚 𝟏 x. 𝒌𝒆𝒚 𝟐 x. 𝒌𝒆𝒚 𝟑
5
10 16
1 2 3
9 11 12 13 14 17 20
Cont. Properties of B- Tree:
…
1. Each internal node x also contains x.n+1 pointers x. 𝒄 𝟏, x. 𝒄 𝟐, …,
x. 𝒄 𝒙.𝒏+𝟏
2. The keys x. 𝒌𝒆𝒚𝐢 separates the ranges of keys in the subtree x. 𝒄𝐢
as 𝐤 𝐢 <= x. 𝒌𝒆𝒚𝐢 <= x. 𝒌𝒆𝒚𝐢+𝟏<= …<= x. 𝒌𝒆𝒚 𝒏 <=x. 𝒌𝒆𝒚 𝒏+𝟏
1 2 3
…
x. 𝒄 𝟏 x. 𝒄 𝟐 x. 𝒄 𝟑
6
4. All the leaves have the same depth which is the tree’s height h
o h <= 𝑙𝑜𝑔𝑡
𝑛+1
2
5. Nodes have upper and lower bounds of the keys they can contain,
the minimum degree of the B- Tree is t >= 2
a. Every node other than the root must have at least t-1 keys
o The internal node has at least t children
b. Every node may contain the root at most 2t-1 keys
o The internal node has at most 2t children
Cont. Properties of B- Tree:
7
Basic Operation in a B- Tree
 Create
 Search
 Insert
 Delete
8
Insert a Key into a B- Tree
when x is a leaf node
when x has children decide
which one to go !
when child is full => splitting
9
Example 1: (Insertion into B- Tree)
 Insert key = 2 ?
7 13 16 24
1 3 4 5 10 11 14 15 18 19 20 21 22 25 26
10
 Insert key = 2?
o i=x.n = 4
o if x.leaf? => false
o else while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖)
• i=4 >1 && 2<24 => i=4-1 = 3
• i=3>1 && 2<16 => i=3-1=2
• i=2 && 2<13 => i=2-1=1
• i=1 && 2<7 => i=1-1=0
o Out from while loop i=0+1=1
o DISK-READ (x. 𝑐1)
o if x. 𝑐1 .n == 2t-1? x. 𝑐1 .n =4 = 5 it’s not full!
o B-TREE-INSERT-NONFULL(x. 𝑐1 .n,2)
x
11
 Insert key = 2?
o i=x.n = 4
o if x.leaf? => true
o while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖)
• i=4 >1 && 2<5 => x.𝑘𝑒𝑦4+1=5 , i=3
• i=3>1 && 2<4 => x.𝑘𝑒𝑦3+1=4 , i=2
• i=2 && 2<3 => x.𝑘𝑒𝑦2+1=3 , i=1
• i=1 && 2<1 => Out from while loop i=1
o x.𝑘𝑒𝑦1+1=2 , x.n = 5
o DISK-WRITE(x)
x
12
Example 1: (Insertion into B- Tree)
7 13 16 24
1 2 3 4 5 10 11 14 15 18 19 20 21 22 25 26
x.𝑘𝑒𝑦2
 The B-Tree after inserting key =2
13
Example 2: (Insertion into B- Tree)
7 13 16 24
1 2 3 4 5 10 11 14 15 18 19 20 21 22 25 26
 Insert key = 17 ?
14
 Insert key = 17?
o i=x.n = 4
o if x.leaf? => false
o else while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖)
• i=4 >1 && 17<24 => i=4-1 = 3
• i=3>1 && 17<16 => Out from while loop i=3+1=4
• DISK-READ (x. 𝑐4)
o if x. 𝑐1 .n == 2t-1? x. 𝑐1 .n = 5 = 5 it’s full!
o B-TREE-SPLIT-CHILD(x. 𝑐4, 4)
x
15
Example 2: (Insertion into B- Tree)
7 13 16 24
1 3 4 5 10 11 14 15 18 19 20 21 22 25 26
20
10 11 10 11
16
Example 2: (Insertion into B- Tree)
 The new B-Tree after splitting
7 13 16 20 24
1 2 3 4 5 10 11 14 15 18 19 25 2621 22
17
 Insert key = 17?
o if k > x.𝑘𝑒𝑦4 ? => 17 > 22
o B-TREE-INSERT-NONFULL(x. 𝑐4 .n, 17)
o …
x.𝑘𝑒𝑦2
 The B-Tree after inserting key = 17
18
Deleting a key in a B-Tree
1. If the key k is in node x and x is a leaf, delete the key k from x.
2. If the key k is in node x and x is an internal node, do the
following:
a. If the child y that precedes k in node x has at least t keys, then
find the predecessor of k in the subtree rooted at y. then
delete k and replace k by its predecessor
b. If y has fewer than t keys, then, look at the next z child that
follows y child if z has at least t keys, then find the successor
of k then delete the k and replace it with its successor.
c. If both y and z have only t-1 keys, merge k with all keys in z
to the child y, then delete the k from y
19
Deleting a key in a B-Tree
3. If the key k is not present in internal node x, determine the
root x.𝒄𝒊 of the appropriate subtree that must contain k. Note:
necessary to guarantee that we descend to a node containing at
least t keys, do the following:
a. If x.𝒄𝒊 has only t -1 keys but has an immediate sibling with at
least t keys, give x.𝑐𝑖 an extra key by moving a key from x
down into x.𝑐𝑖, moving a key from x.𝑐𝑖’s immediate left or
right sibling up into x. then delete the key
b. If x.𝑐𝑖 and both of x.𝑐𝑖 ’s immediate siblings have t -1 keys,
merge x.𝒄𝒊 with one sibling, which involves moving a key
from x down into the new merged node to become the median
key for that node, then delete the key
20
Example 1: Deleting a key in a B-Tree
 Delete key 6:
 Key 6 is in a leaf node, this is Case 1 , Just delete it
21
t=3
Example 2: Deleting a key in a B-Tree
 Delete key 13:
 Key 6 is in a leaf node, this is Case 2,
22
Example 2: Deleting a key in a B-Tree
 Delete key 13:
 Key 6 is in a leaf node, this is Case 2,
 Case 2.a delete k and find its predecessor and place it in key’s
position
23
Example 2: Deleting a key in a B-Tree
 Delete key 13:
 Key 6 is in a leaf node, this is Case 2,
 Case 2.a delete k and find its predecessor and place it in key’s
position
24
Time complexity:(insert & delete)
 It takes O(h) for a B-Tree of height h in disk operation
 CPU time takes O(th)
= O (t 𝑙𝑜𝑔𝑡 n )
25
Usage of B-Tree
 Database: useful in searching
 File system: in NTFS directory to make finding an entry faster
26
Unindexed and unsorted data => O(n)
Key n
B-Tree => O(log n)
References
1 Cormen, T., Leiserson, C., Rivest, R. and Stein, C. (2014). Introduction to
algorithms. Cambridge, Massachusetts: The MIT Press.
2 Kaltenbrunner, A., Kellis, L. and Mart´, D. (n.d.). B-trees. [ebook] Available at:
http://www.di.ufpb.br/lucidio/Btrees.pdf [Accessed 25 Dec. 2017].
3 Koruga, P. and Bača, M. (n.d.). Analysis of B-tree data structure and its usage in
computer forensics. [ebook] p.4. Available at:
https://www.scribd.com/document/112071035/B-Tree-Application [Accessed 27
Dec. 2017].
27
Any Question!
Thank you 😘
28

B tree

  • 1.
    Presented By: Fatimah M.Alqadheeb 218002332
  • 2.
    Content  Introduction  B-TreeProperties  The Height of a B-Tree  Basic Operation in a B-Tree  Inserting  Deleting  Time Complexity  Usage of B-Tree  References 2
  • 3.
  • 4.
    Introduction:  In caseof too large data that could not be fit in a main memory  So, that’s require to access the disk multiple of times  Accessing the disk requires long time  We need to reduce the number of disk access!  The solution is to use the B-Tree, which gives more branches with less height 4
  • 5.
    B-Tree  B-Tree: isa balanced rooted tree whose root is T root.  Properties of B- Tree: 1. For every node x in B- Tree: a. x.n the number of keys in x node b. The x.n keys themselves x. key1, x. key2, …, x. keyn (increasing order) c. x.leaf is a Boolean value that is true if x is a leaf & false if x is an internal node x.n x. 𝒌𝒆𝒚 𝟏 x. 𝒌𝒆𝒚 𝟐 x. 𝒌𝒆𝒚 𝟑 5
  • 6.
    10 16 1 23 9 11 12 13 14 17 20 Cont. Properties of B- Tree: … 1. Each internal node x also contains x.n+1 pointers x. 𝒄 𝟏, x. 𝒄 𝟐, …, x. 𝒄 𝒙.𝒏+𝟏 2. The keys x. 𝒌𝒆𝒚𝐢 separates the ranges of keys in the subtree x. 𝒄𝐢 as 𝐤 𝐢 <= x. 𝒌𝒆𝒚𝐢 <= x. 𝒌𝒆𝒚𝐢+𝟏<= …<= x. 𝒌𝒆𝒚 𝒏 <=x. 𝒌𝒆𝒚 𝒏+𝟏 1 2 3 … x. 𝒄 𝟏 x. 𝒄 𝟐 x. 𝒄 𝟑 6
  • 7.
    4. All theleaves have the same depth which is the tree’s height h o h <= 𝑙𝑜𝑔𝑡 𝑛+1 2 5. Nodes have upper and lower bounds of the keys they can contain, the minimum degree of the B- Tree is t >= 2 a. Every node other than the root must have at least t-1 keys o The internal node has at least t children b. Every node may contain the root at most 2t-1 keys o The internal node has at most 2t children Cont. Properties of B- Tree: 7
  • 8.
    Basic Operation ina B- Tree  Create  Search  Insert  Delete 8
  • 9.
    Insert a Keyinto a B- Tree when x is a leaf node when x has children decide which one to go ! when child is full => splitting 9
  • 10.
    Example 1: (Insertioninto B- Tree)  Insert key = 2 ? 7 13 16 24 1 3 4 5 10 11 14 15 18 19 20 21 22 25 26 10
  • 11.
     Insert key= 2? o i=x.n = 4 o if x.leaf? => false o else while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖) • i=4 >1 && 2<24 => i=4-1 = 3 • i=3>1 && 2<16 => i=3-1=2 • i=2 && 2<13 => i=2-1=1 • i=1 && 2<7 => i=1-1=0 o Out from while loop i=0+1=1 o DISK-READ (x. 𝑐1) o if x. 𝑐1 .n == 2t-1? x. 𝑐1 .n =4 = 5 it’s not full! o B-TREE-INSERT-NONFULL(x. 𝑐1 .n,2) x 11
  • 12.
     Insert key= 2? o i=x.n = 4 o if x.leaf? => true o while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖) • i=4 >1 && 2<5 => x.𝑘𝑒𝑦4+1=5 , i=3 • i=3>1 && 2<4 => x.𝑘𝑒𝑦3+1=4 , i=2 • i=2 && 2<3 => x.𝑘𝑒𝑦2+1=3 , i=1 • i=1 && 2<1 => Out from while loop i=1 o x.𝑘𝑒𝑦1+1=2 , x.n = 5 o DISK-WRITE(x) x 12
  • 13.
    Example 1: (Insertioninto B- Tree) 7 13 16 24 1 2 3 4 5 10 11 14 15 18 19 20 21 22 25 26 x.𝑘𝑒𝑦2  The B-Tree after inserting key =2 13
  • 14.
    Example 2: (Insertioninto B- Tree) 7 13 16 24 1 2 3 4 5 10 11 14 15 18 19 20 21 22 25 26  Insert key = 17 ? 14
  • 15.
     Insert key= 17? o i=x.n = 4 o if x.leaf? => false o else while(i >= 1 and 2 < x.𝑘𝑒𝑦𝑖) • i=4 >1 && 17<24 => i=4-1 = 3 • i=3>1 && 17<16 => Out from while loop i=3+1=4 • DISK-READ (x. 𝑐4) o if x. 𝑐1 .n == 2t-1? x. 𝑐1 .n = 5 = 5 it’s full! o B-TREE-SPLIT-CHILD(x. 𝑐4, 4) x 15
  • 16.
    Example 2: (Insertioninto B- Tree) 7 13 16 24 1 3 4 5 10 11 14 15 18 19 20 21 22 25 26 20 10 11 10 11 16
  • 17.
    Example 2: (Insertioninto B- Tree)  The new B-Tree after splitting 7 13 16 20 24 1 2 3 4 5 10 11 14 15 18 19 25 2621 22 17
  • 18.
     Insert key= 17? o if k > x.𝑘𝑒𝑦4 ? => 17 > 22 o B-TREE-INSERT-NONFULL(x. 𝑐4 .n, 17) o … x.𝑘𝑒𝑦2  The B-Tree after inserting key = 17 18
  • 19.
    Deleting a keyin a B-Tree 1. If the key k is in node x and x is a leaf, delete the key k from x. 2. If the key k is in node x and x is an internal node, do the following: a. If the child y that precedes k in node x has at least t keys, then find the predecessor of k in the subtree rooted at y. then delete k and replace k by its predecessor b. If y has fewer than t keys, then, look at the next z child that follows y child if z has at least t keys, then find the successor of k then delete the k and replace it with its successor. c. If both y and z have only t-1 keys, merge k with all keys in z to the child y, then delete the k from y 19
  • 20.
    Deleting a keyin a B-Tree 3. If the key k is not present in internal node x, determine the root x.𝒄𝒊 of the appropriate subtree that must contain k. Note: necessary to guarantee that we descend to a node containing at least t keys, do the following: a. If x.𝒄𝒊 has only t -1 keys but has an immediate sibling with at least t keys, give x.𝑐𝑖 an extra key by moving a key from x down into x.𝑐𝑖, moving a key from x.𝑐𝑖’s immediate left or right sibling up into x. then delete the key b. If x.𝑐𝑖 and both of x.𝑐𝑖 ’s immediate siblings have t -1 keys, merge x.𝒄𝒊 with one sibling, which involves moving a key from x down into the new merged node to become the median key for that node, then delete the key 20
  • 21.
    Example 1: Deletinga key in a B-Tree  Delete key 6:  Key 6 is in a leaf node, this is Case 1 , Just delete it 21 t=3
  • 22.
    Example 2: Deletinga key in a B-Tree  Delete key 13:  Key 6 is in a leaf node, this is Case 2, 22
  • 23.
    Example 2: Deletinga key in a B-Tree  Delete key 13:  Key 6 is in a leaf node, this is Case 2,  Case 2.a delete k and find its predecessor and place it in key’s position 23
  • 24.
    Example 2: Deletinga key in a B-Tree  Delete key 13:  Key 6 is in a leaf node, this is Case 2,  Case 2.a delete k and find its predecessor and place it in key’s position 24
  • 25.
    Time complexity:(insert &delete)  It takes O(h) for a B-Tree of height h in disk operation  CPU time takes O(th) = O (t 𝑙𝑜𝑔𝑡 n ) 25
  • 26.
    Usage of B-Tree Database: useful in searching  File system: in NTFS directory to make finding an entry faster 26 Unindexed and unsorted data => O(n) Key n B-Tree => O(log n)
  • 27.
    References 1 Cormen, T.,Leiserson, C., Rivest, R. and Stein, C. (2014). Introduction to algorithms. Cambridge, Massachusetts: The MIT Press. 2 Kaltenbrunner, A., Kellis, L. and Mart´, D. (n.d.). B-trees. [ebook] Available at: http://www.di.ufpb.br/lucidio/Btrees.pdf [Accessed 25 Dec. 2017]. 3 Koruga, P. and Bača, M. (n.d.). Analysis of B-tree data structure and its usage in computer forensics. [ebook] p.4. Available at: https://www.scribd.com/document/112071035/B-Tree-Application [Accessed 27 Dec. 2017]. 27
  • 28.