0% found this document useful (0 votes)
57 views22 pages

Btree

The document summarizes the properties and operations of a B-tree, which is a multiway tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The key points are: 1. A B-tree of order m has internal nodes with between m/2 and m child nodes, and leaves at the same depth containing data values. 2. Insertions start by searching for the appropriate leaf node and either inserting into an empty slot or splitting the full node. 3. Deletions may require replacing the deleted value with another, redistributing values between nodes, or recombining nodes to avoid underflows.

Uploaded by

Shubham Sharma
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)
57 views22 pages

Btree

The document summarizes the properties and operations of a B-tree, which is a multiway tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The key points are: 1. A B-tree of order m has internal nodes with between m/2 and m child nodes, and leaves at the same depth containing data values. 2. Insertions start by searching for the appropriate leaf node and either inserting into an empty slot or splitting the full node. 3. Deletions may require replacing the deleted value with another, redistributing values between nodes, or recombining nodes to avoid underflows.

Uploaded by

Shubham Sharma
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/ 22

B-Tree Example

IS 320
B Tree
A B-tree of order m (the maximum number of children for
each node) is a tree which satisfies the following
properties:
1. Every node has at most m children.
2. Every node (except root and leaves) has at least m⁄2
children.
3. The root has at least two children if it is not a leaf node.
4. All leaves appear in the same level, and carry information.
5. A non-leaf node with k children contains k–1 keys.
k1 k2 k3 ... km-2 km-
1

T0 T1 T2 Tm-2 Tm-1

key < k1 k1 < key < k2 k2 < key < k3 km-2 < key < km-1 key > km-1

Multi-way tree
Example: A B-tree of order 4

Example: A B-tree of order 5

Note:
• The data references are not shown.
• The leaf references are to empty subtrees

B-Tree Examples
Operations
• B-Tree of order 4
– Each node has at most 4 pointers and 3 keys,
and at least 2 pointers and 1 key.
• Insert: 5, 3, 21, 9, 1, 13, 2, 7, 10, 12, 4, 8
• Delete: 2, 21, 10, 3, 4
All insertions start at a leaf node. To insert a new element search the tree to
find the leaf node where the new element should be added.
Insert the new element into that node with the following
steps:
• If the node contains fewer than the maximum legal number of
elements, then there is room for the new element. Insert the new
element in the node, keeping the node's elements ordered.
• Otherwise the node is full, so evenly split it into two nodes.
• A single median is chosen from among the leaf's elements and the
new element.
• Values less than the median are put in the new left node and
values greater than the median are put in the new right node,
with the median acting as a separation value.
• Insert the separation value in the node's parent, which may
cause it to be split, and so on. If the node has no parent (i.e., the
node was the root), create a new root above this node (increasing
the height of the tree)
Insert 5, 3, 21

*5* a

*3*5* a

* 3 * 5 * 21 * a
Insert 9
*9* a
b c
*3*5* * 21 *

Node a splits creating 2 children: b and c


Insert 1, 13
*9* a
b c
*1*3*5* * 13 * 21 *

Nodes b and c have room to insert more elements


Insert 2
*3*9* a

b d c
*1*2* *5* * 13 * 21 *

Node b has no more room, so it splits creating node d.


Insert 7, 10
*3*9* a

b d c
*1*2* *5*7* * 10 * 13 * 21 *

Nodes d and c have room to add more elements


Insert 12
* 3 * 9 * 13 * a

b d c e
*1*2* *5*7* * 10 * 12 * * 21 *

Nodes c must split into nodes c and e


Insert 4
a
* 3 * 9 * 13 *
b d c e
*1*2* *4*5*7* * 10 * 12 * * 21 *

Node d has room for another element


Insert 8
*9* a

f g
*3*7* * 13 *

b d h c e
*1*2* *4*5* *8* * 10 * 12 * * 21 *

Node d must split into 2 nodes. This causes node a to split


into 2 nodes and the tree grows a level.
Deletion from Leaf Node
• The necessary steps for deleting from a leaf
node are:
Step 1. Search for the value to delete.
Step 2. If the value is in a leaf node, it can
simply be deleted from the node
Step 3. If a node underflows, the tree has to
be rebalanced
Deletion from Internal Node
• Internal nodes act as keys for 2 subtrees. So
when deleting a key we need to find a
replacement key.
Step 1. Choose new key (largest element in left
subtree or smallest in right subtree)
Step 2. Remove it from leaf and replace the key
to be deleted it with the new key
Step 3. Since the new key was part of leaf, its
deletion might require rebalancing
REBALANCING a deficient leaf node

If the right sibling has more than the minimum number of elements
• Copy key (from the parent) to end of deficient node
• Replace key in parent with first element in right sibling.
• Tree is balanced
ELSE If the left sibling has more than the minimum number of elements
• Copy the key (from the parent) to the start of the deficient node
• Replace the key in the parent with the last element of the left sibling
• Tree is balanced
ELSE If both immediate siblings have only minimum number of elements
• Copy the key to the end of the left node
• Move all elements from the right node to the left node (right becomes
empty)
• Remove the key (from the parent) and remove its empty right child
– IF parent is ROOT and has no elements, FREE it and make the merged node as
ROOT
– IF parent is NOT ROOT and has less elements, REBALANCE it
Delete 2
*9* a

f g
*3*7* * 13 *

b d h c e
*1* *4*5* *8* * 10 * 12 * * 21 *

Node b can loose an element without underflow.


Delete 21 (case 2)
*9* a

f g
*3*7* * 12 *

b d h c e
*1* *4*5* *8* * 10 * * 13 *

Deleting 21 causes node e to underflow, so elements are


redistributed between nodes c, g, and e
Delete 10 (case 3)
*3*7*9* a

b d h e
*1* *4*5* *8* * 12 * 13 *

Deleting 10 causes node c to underflow. This causes the parent,


node g to recombine with nodes f and a. This causes the tree to
shrink one level.
Delete 3
*4*7*9* a

b d h e
*1* *5* *8* * 12 * 13 *

Because 3 is a pointer to nodes below it, deleting 3 requires


keys to be redistributed between nodes a and d.
Delete 4
*7*9* a

b h e
*1*5* *8* * 12 * 13 *

Deleting 4 requires a redistribution of the keys in the subtrees of 4;


however, nodes b and d do not have enough keys to redistribute
without causing an underflow. Thus, nodes b and d must be combined.

You might also like