0% found this document useful (0 votes)
2 views

B tree

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

B tree

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

B Tree

B Tree is a specialized m-way tree that can be widely used for disk access.
An m-way tree that self-balances itself is called a "B-tree." Due to their balanced structure,
such trees are frequently used to manage and organise enormous databases and facilitate
searches.

Definition of B-tree

B-tree in DBMS is an m-way tree which self-balances itself. Due to their balanced structure,
such trees are frequently used to manage and organise enormous databases and facilitate
searches. In a B-tree, each node can have a maximum of n child nodes. B-tree is an example
of multilevel indexing. Leaf nodes and internal nodes will both have record references. B-
Tree is called Balanced stored trees as all the leaf nodes are at same levels.

The image below depicts a B tree of order 4

Any B Tree property, such as the minimum number of children a node can have, may be
violated while performing specific actions on the B Tree. The tree may divide or unite to
preserve B Tree’s characteristics.
B-Tree Time Complexity
S Complexit
Algorithm
No y

1 Search 0 (log n)

2 Insert 0 (log n)

3 Delete 0 (log n)

Properties of B-tree

Following are some of the properties of B-tree:

 A non-leaf node's number of keys is one less than the number of its children.
 The number of keys in the root ranges from one to (m-1) maximum. Therefore, root
has a minimum of two and a maximum of m children.
 The keys range from min([m/2]-1) to max(m-1) for all nodes (non-leaf nodes) besides
the root. Thus, they can have between m and [m/2] children.
 The level of each leaf node is the same.
 The keys should be arranged in sorted manner.

Need of B-tree

 For having optimized searching, we cannot increase a tree's height. Therefore, we


want the tree to be as short as possible in height.
 Use of B-tree in DBMS, which has more branches and hence shorter height, is the
solution to this problem. Access time decreases as branching and depth grow.
 Hence, use of B-tree is needed for storing data as searching and accessing time is
decreased.
 The cost of accessing the disc is high when searching tables. Therefore, minimising
disc access is our goal.
 So, to decrease time and cost, we use B-tree for storing data as it makes the Index
Fast.

B-Tree Operations
1. Searching
Similar to binary search trees, B trees also allow for searching. A binary tree search is similar
to a B-Tree search. The algorithm uses recursion and is comparable. The search is optimized
at each level, so it is in a different branch if the key value isn’t in the parent’s range. These
numbers are called pull-back or separation values because they restrict the search. It will
return NULL if we approach a leaf node but can’t find the key.
For instance, see the above B Tree for item 50. The procedure will go something like this:
 Compare root node 78 with item 50. Go to the sub-tree on its left because 49 78.
 Since 40>50>56, “Match found,” go back.
 50>45, move towards the right side.
 Now, compare 50
 Return as the match is found.
The height of the B tree affects how you search for that tree. Any item in a B tree can be
searched using the O(log n) time search algorithm.
2. Insertion
At the level of the leaf node, insertions are made. The following algorithm must be used to
add a new item to the B Tree.
 Find the proper leaf node where the node could be placed by traversing the B Tree.
 Insert the item in increasing order, whereas if the leaf node contains fewer keys than
m-1.
 Otherwise, take the following action if such a leaf node has m-1 keys.
 Add the new element to the elements’ increasing order.
 At the median, divide the node into two further nodes.
 The median element should be raised to its parent node.
 If the parent node receives additional m-1 keys, split it using the same procedures.

Example: Insert the node 8 into the B Tree of order 5 shown in the following image.

8 will be inserted to the right of 5, therefore insert 8.

The node, now contain 5 keys which is greater than (5 -1 = 4) keys. Therefore, split the node
from the median i.e. 8 and push it up to its parent node shown as follows.

3. Deletion

Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a
leaf node or an internal node. Following algorithm needs to be followed in order to delete a
node from a B tree.

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from the
node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the element
from right or left sibling.
o If the left sibling contains more than m/2 elements then push its largest
element up to its parent and move the intervening element down to the node
where the key is deleted.
o If the right sibling contains more than m/2 elements then push its smallest
element up to the parent and move intervening element down to the node
where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf node
by joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the parent
too.

If the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node
hence, the process will be similar as the node is being deleted from the leaf node.

Example

Delete the node 53 from the B Tree of order 5 shown in the following figure.

53 is present in the right child of element 49. Delete it.

Now, 57 is the only element which is left in the node, the minimum number of elements that
must be present in a B tree of order 5, is 2. it is less than that, the elements in its left and right
sub-tree are also not sufficient therefore, merge it with the left sibling and intervening
element of parent i.e. 49.
The final B tree is shown as follows.

Application of B tree

 Since accessing values held in a vast database saved on a disc takes a long time, B
trees index all data and allow quick access to the information stored on the discs.
 Large databases employ it to access information stored on discs.
 Using the B-Tree, data in a dataset could be found in less time.
 Multilevel indexing is possible with the indexing feature.
 The majority of servers also use the B-tree method.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy