[Data structure] B tree

B tree (B-tree, B-tree)

B-tree is a balanced multi-way search tree, Mostly used for the realization of file system and database;

If you look closely at the B-tree, what are the bright features?

  • 1 个节点Can store more than 2 elements, can have more than 2 child nodes
  • Have some properties of binary search tree
  • Balanced, all subtrees of each node have the same height
  • Relatively short

Insert picture description here
Insert picture description here
Insert picture description here

Properties of m-order B-tree

Insert picture description here
How many levels of B-trees are generally used in database implementation?

  • 200 ~ 300

B-tree vs. binary search tree

B-tree and binary search trees , are logically equivalent;

Combine multiple generations of nodes to get a super node

  • 2 generations (current node and left and right child nodes) merged super node, with a maximum of 4 child nodes (at least 4-order B-tree)
  • 3 generations (the current node and the grandchild nodes of the left and right child nodes) merged super node, with a maximum of 8 child nodes (at least 8-order B-tree)
  • Super nodes merged in generation n, up to 2 n 2^n2n child nodes (at least2 n 2^n2n- order B tree)

m-order B-tree, at most log 2 m log_2mlog2M- generation merger;
Insert picture description here

search for

Similar to binary search tree search:
Insert picture description here

  1. First search for elements from small to large (from left to right) inside the node
  2. If it hits, the search ends
  3. If it is not hit, go to the corresponding child node to search for the element, and then repeat step 1

Add-Overflow

4th order B tree

The newly added element must be added to the leaf node :
Insert picture description here
insert 55:
Insert picture description here
insert 95:
Insert picture description here
insert 98 again? (Assuming this is a 4th order B-tree)

  • The number of elements of the leaf node in the bottom right corner will exceed the limit
  • This phenomenon can be called:Overflow

Add-Overflow resolution (assuming 5th order)

Insert picture description here
The number of elements of the overflow node must be equal to m;

Suppose the position of the middle element of the overflow node is k

  1. Merge the element at position k upward with the parent node
  2. Put [0, k − 1] [0, k-1][0,k1 ] and[k + 1, m − 1] [k + 1, m-1][k+1,m1 ] The element at position is split into 2 child nodes
  3. The number of elements of these two child nodes must not be lower than the minimum limit ( ┌ m / 2 ┐ − 1 ┌ m/2 ┐ − 1m / 2 1 ) [m divided by 2 and round up and minus 1]

After a split is completed, it may cause the parent node to overflow, still follow the above method to solve

  • In the most extreme case, it may split all the way to the root node
    Insert picture description here

Add demo [overflow]

Suppose this is a 4th-order B-tree , add 98, 52, 54 in sequence
Insert picture description here

delete

Delete-leaf node

If the element to be deleted is in the leaf node , just delete it directly;

Example: delete 30
Insert picture description here

Delete-non-leaf node

If you need to delete elements in non-leaf nodes in

  1. Find the predecessor or successor element first, and overwrite the value of the element to be deleted
  2. Then delete the predecessor or successor element

The predecessor or successor element of a non-leaf node must be in the leaf node;

  • So the predecessor or successor element deleted here is the situation mentioned at the beginning: the deleted element is in the leaf node
  • The actual deletion of elements occurs in the leaf nodes;

Example: delete 60
Insert picture description here

Delete-underflow

Insert picture description here
Delete 22? (Assuming this is a 5th-order B-tree)

  • After one element is deleted from the leaf node, the number of elements may be lower than the minimum limit ( ≥ ┌ m / 2 ┐ − 1 ≥ ┌ m/2 ┐ − 1m/21
  • This phenomenon is called:下溢(underflow)

Delete-Underflow resolution

Insert picture description here
Insert picture description here

The number of elements of the underflow node must be equal to ┌ m / 2 ┐ − 2 ┌ m/2 ┐ − 2m/22

If the next sibling node of the underflow node is at least ┌ m / 2 ┐ ┌ m/2 ┐m / 2 elements, one element can be borrowed from it

  • Insert the element b of the parent node into position 0 (minimum position) of the underflow node
  • Replace the element b of the parent node with the element a of the sibling node (the largest element)
  • This kind of operation is actually: rotate
    Insert picture description here

If the next sibling node of the underflow node, only ┌ m / 2 ┐ − 1 ┌ m/2 ┐ − 1m / 2 1 element

  • Move the element b of the parent node down to merge with the left and right child nodes
  • The number of combined node elements is equal to ┌ m / 2 ┐ + ┌ m / 2 ┐ − 2 ┌ m/2 ┐ + ┌ m/2 ┐ − 2m/2+m / 2 2 , not more than m − 1
  • This operation may cause the parent node to underflow, still follow the above method to solve the problem, the underflow phenomenon may continue to propagate upward
    Insert picture description here

Delete demo [underflow]

Assuming this is a 5th-order B-tree, deleting 22 will cause 20, 30 to underflow
Insert picture description here

4th order B tree

Insert picture description here
The properties of the 4-order B-tree:

  • The number of elements x that all nodes can store :1 ≤ x ≤ 3
  • The number of child nodes y of all non-leaf nodes :2 ≤ and ≤ 4

Guess you like

Origin blog.csdn.net/qq_32727095/article/details/112541327