Sunteți pe pagina 1din 7

B Tree

 B-Tree is a self-balanced search tree in which every node contains multiple keys
and has more than two children.
 Here, number of keys in a node and number of children for a node depends on the
order of B-Tree. Every B-Tree has an order.
 B-Tree was developed in the year 1972 by Bayer and McCreight with the
name Height Balanced m-way Search Tree.

B-Tree of Order m has the following properties...

a. Every node in a B-Tree contains at most m children.


b. Every node in a B-Tree except the root node and the leaf node contain at least m/2
children.
c. The root nodes must have at least 2 nodes.
d. All leaf nodes must be at the same level.

Inserting

Insertions are done at the leaf node level. The following algorithm needs to be followed in order
to insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can
be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
i. Insert the new element in the increasing order of elements.
ii. Split the node into the two nodes at the median.
iii. Push the median element upto its parent node.
iv. If the parent node also contain m-1 number of keys, then split it too by
following the same steps.
Ex: Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
Deletion in B tree

B tree is a self-balancing search tree (the tree adjusts itself so that all the leaves are at the
same depth) and contains multiple nodes which keep data in sorted order. Each node has 2 or
more children and consists of multiple keys.

Deletion is much more complicated than insertion. We consider 3 cases when deleting the key k
from a B tree which is given.

 Case 1
If k (Key) is in the node x which is a leaf and x.n(Number of keys) >= t (child/Order).
Here you can straightaway delete k from x. Ex:-Let’s delete D from the B tree

 Case 2
If k is in the node x which is a leaf and x.n == (t-1)
Case 2a:
Find the immediate sibling y of x, the extreme key m of y, the parent p of x and the
parent key l of k
If y.n >= t:
Move l into x
Move m into p
Delete k from x
Ex:- Delete F
Case 2b:

Find the immediate sibling y of x, the extreme key m of y, the parent p of x and the
parent key l of k
If y.n == t-1:
Merge x and y
Move down l to the new node as the median key
Delete k from the new node

Ex:- Delete B

Step 3 :B tree after deleting B Stet 2

 Case 3
If k is in the node x and x is an internal node (not a leaf)
Case 3a:
Find the child node y that precedes k (the node which is on the left side of k)
If y.n >= t:
Find the key k’ in y which is the predecessor of k
Delete k’ recursively. (Here k’ can be another internal node as well. So we have to
delete it recursively in the same way)
Replace k with k’ in x
Ex :- Delete M

Case 3b:
Find the child node y that precedes k
If y.n < t (or y.n == (t-1)):
Find the child node z that follows k (the node which is on the right side of k)
If z.n >= t:
Find k’’ in z which is the successor of k
Delete k’’ recursively
Replace k with k’’ in x

Ex:- Delete G

Step 1: Delete J and replace G with J B tree after Deleting G

 Case 3c:
Find the child node y that precedes k and the child node z that follows k
If y.n == (t-1) AND z.n == (t-1):
Merge k and z to y
Free memory of node z
Recursively delete k from y

Ex:- Delete C

Step 1 : Merging
Step 2: Delete C B tree after deleting C

B+ Tree
 B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.
 In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in
B+ tree, records (data) can only be stored on the leaf nodes while internal nodes can only store
the key values.
 The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the
search queries more efficient.
 B+ Tree are used to store the large amount of data which can not be stored in the main memory.
Due to the fact that, size of main memory is always limited, the internal nodes (keys to access
records) of the B+ tree are stored in the main memory whereas, leaf nodes are stored in the
secondary memory.
 The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is shown in the
following figure.

Advantages of B+ Tree
1. Records can be fetched in equal number of disk accesses.
2. Height of the tree remains balanced and less as compare to B tree.
3. We can access the data stored in a B+ tree sequentially as well as directly.
4. Keys are used for indexing.
5. Faster search queries as the data is stored only on the leaf nodes.
B Tree VS B+ Tree
SN B Tree B+ Tree

1 Search keys can not be repeatedly stored. Redundant search keys can be present.

2 Data can be stored in leaf nodes as well as Data can only be stored on the leaf
internal nodes nodes.

3 Searching for some data is a slower process Searching is comparatively faster as data
since data can be found on internal nodes as can only be found on the leaf nodes.
well as on the leaf nodes.

4 Deletion of internal nodes are so Deletion will never be a complexed


complicated and time consuming. process since element will always be
deleted from the leaf nodes.

5 Leaf nodes can not be linked together. Leaf nodes are linked together to make
the search operations more efficient.

S-ar putea să vă placă și