0% found this document useful (0 votes)
9 views45 pages

B Tree Final

B-Trees, developed in 1972, are a type of search tree that allows nodes to contain multiple keys and children, making them suitable for large databases and efficient data retrieval. Key operations include searching, insertion, and deletion, each with specific algorithms to maintain the tree's properties. While B-Trees offer advantages like guaranteed logarithmic time complexity and efficient storage, they can be disk-dependent and slower compared to other data structures.

Uploaded by

ksrao532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views45 pages

B Tree Final

B-Trees, developed in 1972, are a type of search tree that allows nodes to contain multiple keys and children, making them suitable for large databases and efficient data retrieval. Key operations include searching, insertion, and deletion, each with specific algorithms to maintain the tree's properties. While B-Trees offer advantages like guaranteed logarithmic time complexity and efficient storage, they can be disk-dependent and slower compared to other data structures.

Uploaded by

ksrao532
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

B-Tree

Introduction
o B-Tree was developed in the year 1972 by Bayer and
McCreight with the name Height Balanced m-way Search
Tree.
o Later it was named as B-Tree.

o In search trees like binary search tree, AVL Tree, etc., every
node contains only one value (key) and a maximum of two
children.
o But there is a special type of search tree called B-Tree in
which a node contains more than one value (key) and more
than two children.

2
B-Tree :
B-Tree properties
Example for 4 way B-Tree
Operations on B-Tree
o The search operation is performed as follows...
o Step 1 - Read the search element from the user.
o Step 2 - Compare the search element with first key value of root
node in the tree.
o Step 3 - If both are matched, then display "Given node is found!!!"
and terminate the function
o Step 4 - If both are not matched, then check whether search
element is smaller or larger than that key value.
o Step 5 - If search element is smaller, then continue the search
process in left subtree.
o Step 6 - If search element is larger, then compare the search
element with next key value in the same node and repeat steps 3, 4,
5 and 6 until we find the exact match or until the search element is
compared with last key value in the leaf node.
o Step 7 - If the last key value in the leaf node is also not matched
then display "Element is not found" and terminate the function.

8
Search in a B-Tree
Insertion in a B-Tree
o In a B-Tree, a new element must be added only at the leaf node.
That means, the new key value is always attached to the leaf node
only. The insertion operation is performed as follows...
o Step 1 - Check whether tree is Empty.
o Step 2 - If tree is Empty, then create a new node with new key
value and insert it into the tree as a root node.
o Step 3 - If tree is Not Empty, then find the suitable leaf node to
which the new key value is added using Binary Search Tree logic.
o Step 4 - If that leaf node has empty position, add the new key value
to that leaf node in ascending order of key value within the node.
o Step 5 - If that leaf node is already full, split that leaf node by
sending middle value to its parent node. Repeat the same until the
sending value is fixed into a node.
o Step 6 - If the splitting is performed at root node, then the middle
value becomes new root node for the tree and the height of the tree
is increased by one.
10
Example

Construct a B-Tree of Order 3 :


7, 8, 9, 10, 11, 16, 21,18.
Since the maximum degree of a node in the tree is 3;
therefore,
the maximum number of keys per node will be 3 - 1 = 2.

11
1. We will start by inserting data element 7 in the empty tree.

2. We will insert the next data element, i.e., 8, into the tree. Since 8 is greater than 7, it will be
inserted to the right of 7 in the same node.

3. Similarly, we will insert another data element, 9, into the tree on the same to the right of 8.
However, since the maximum number of keys per node can only be 2, the node will split,
pushing the median key 8 upward, making 7 the key of the left child node and 9 the key of the
right child node.

4. We will insert the next data element, i.e., 10, into the tree. Since 10 is greater than 9, it will
be inserted as a key on the right of the node containing 9 as a key.
5. We will now insert another data element, 11, into the tree. Since 11 is greater than 10, it
should be inserted to the right of 10. However, as we know, the maximum number of keys per
node cannot be more than 2; therefore, 10 being the median, will be pushed to the root node
right to 8, splitting 9 and 11 into two separate nodes.

6. We will now insert data element 16 into the tree. Since 16 is greater than 11, it will be
inserted as a key on the right of the node consisting of 11 as a key.

7. The next data element that we will insert into the tree is 21. Element 21 should be
inserted to the right of 16; however, it will exceed the maximum number of keys per node
limit. Therefore, a split will occur, pushing the median key 16 upward and splitting the left
and right keys into separate nodes. But this will again violate the maximum number of keys
per node limit; therefore, a split will once again push the median key 10 upward a root node
and make 8 and 11 its children.
8. At last, we will insert data element 18 into the tree. Since 18 is greater than 16 but less
than 21, it will be inserted as the left key in the node consisting of 21.

9. Hence the resulted B Tree will be as shown below:


Insertion in a B-Tree

1. Find the minimum elements in a internal and leaf nodes


m/2 to m number of children or m/2-1 to m-1 number of data items

minimum is 1 data item and maximum 2 items


Insertion in a B-Tree
Insertion in a B-Tree
Insertion in a B-Tree
Insertion in a B-Tree
Ex:- Construct a B-Tree of order 5

1. Insert the following items into a 5 way tree or 5 ary tree or order
5 tree

{ 45, 10,56, 75, 33,37, 68, 54, 60, 48, 25, 66, 70, 28, 30, 65 }

Find the minimum elements in a internal and leaf nodes


m/2 to m number of children or m/2-1 to m-1 number of data items
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Ex:- Construct a B-Tree of order 5
Deletion
There are 2 cases:
oLeaf node:

28
o Internal node:

29
Example

30
o Deletion of 32:
After deleting 32, that node can have min.
number of keys. So, simply the key 32 can
be deleted. Then,

31
o Deletion of 31:
It can barrow a key from its left node ie the
largest value of left node.

32
o Deletion of 30:
If a key cannot barrow from its left or right,
then this node will be merged with left
node or right node, along with its
intervening parent.
Then there are 2 possible cases,
 [25, 28]
 [33, 35]

33
34
o Deletion of 33:
Internal node 33 is replaced with its
inorder predecessor or inorder successor.

For inorder predecessor, take maximum value


of its left node
For inorder suceessor, take minimum value of
its left node

35
36
o Deletion of 50:
If it is not possible to take inorder
predecessor or inorder successor, then
merge the leaf nodes.

37
o Deletion of 40:
 Here 40 is the root node. So, take inorder

predecessor or inorder successor.


 But, here it cannot take inorder predecessor, since
there is only one key ie 35.
 It can take inorder successor, since there are 2 keys
in the node [45, 55]

38
39
Deletion in a B-Tree

1. Find the minimum elements in a internal and leaf nodes


m/2 to m number of children or m/2-1 to m-1 number of keys

minimum is 2 keys and maximum 4 keys


Deletion in a B-Tree
Deletion in a B-Tree
43
B-Tree applications:
oLarge databases employ it to access information stored on discs.
oUsing the B-Tree, finding data in a data set can be done in a great
deal less time.
oMultilevel indexing is possible with the indexing feature.
oThe B-tree method is also used by the majority of servers.
oIn CAD systems, B-Trees are used to catalogue and search
geometric data.
oOther applications of B-Trees include encryption, computer networks,
and natural language processing.
oSince accessing values stored in a large database that is stored on a
disc takes a long time, B trees are used to index the data and provide
quick access to the actual data stored on the disks.
oIn the worst case, it takes O(n) running time to search a database with
n key values that is not sorted or indexed. However, if we use B Tree to
index this database, it will be searched in O (log n) time in worst case.

44
Advantages of B-Trees:
oB-Trees are well suited for big data sets and real-time
applications because they have a guaranteed time complexity of
O (log n) for fundamental operations like insertion, deletion, and
searching.
oB-Trees can balance themselves.
oHigh throughput and concurrency.
oEfficient use of the storage space.

Disadvantages of B-Trees:
oThey are dependent on disk-based data structures and can use
a lot of disc space.
oNot always the greatest option.
oComparatively slow to other data structures.

45

You might also like