Docsity

B-Trees and Related Data Structures - Lecture Notes | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
B–Trees and Related Data Structures
REMEMBER: Trees that are balanced (have minimum height for a given
node count) are more quickly searched.
Balanced trees serve as efficient indices into databases and other collections
of indexed records.
The B–Tree and its derivative data structures allow for very efficient indexing.
The B–Tree comprises a data structure and associated insertion and deletion algorithms.
One suspects that deletion would be done by ā€œLazy Deletionā€, marking as deleted.
The B–Tree data structure contains two types of nodes: index nodes and data nodes.
Index nodes are also called ā€œinterior nodesā€.
Unlike a binary tree, the interior nodes of a B–Tree do not contain data values.
These interior nodes contain copies of the key values from some of the data nodes.
These key values are used by the index structure in efficient location of the data nodes.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download B-Trees and Related Data Structures - Lecture Notes | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

B–Trees and Related Data Structures

REMEMBER: Trees that are balanced (have minimum height for a given node count) are more quickly searched. Balanced trees serve as efficient indices into databases and other collections of indexed records. The B–Tree and its derivative data structures allow for very efficient indexing. The B–Tree comprises a data structure and associated insertion and deletion algorithms. One suspects that deletion would be done by ā€œLazy Deletionā€, marking as deleted. The B–Tree data structure contains two types of nodes: index nodes and data nodes. Index nodes are also called ā€œinterior nodesā€. Unlike a binary tree, the interior nodes of a B–Tree do not contain data values. These interior nodes contain copies of the key values from some of the data nodes. These key values are used by the index structure in efficient location of the data nodes.

Index Nodes in a B–Tree

In a B–Tree of Order M, index nodes have M pointers and (M – 1) keys Example: An Index Node (Interior Node) for B–Tree of Order 4 P 0 points to records with key values K < K 1. For 1 ļ‚£ J ļ‚£ (M – 2), here 1 ), here 1 ļ‚£ J ļ‚£ 2), here 1 PJ points to records with key values KJ ļ‚£ K < KJ+1. PM-1, here P 3 , points to records with key values K  K 3. Unless the index node is the root node of the B–Tree, there will be additional constraints placed on the records pointed to by P 0 and PM–1. All B–Tree variants of Order 4, such as a B+ Tree, would have the same structure for interior nodes.

Balance Requirements for a B–Tree

All variants of B–Trees, including the B+ Tree, follow the same balance constraints. These constraints are enforced by the algorithm to insert new keys into the tree. A B–Tree of order M  2), here 1 satisfies the following requirements.

  1. The root is either a leaf (thus an isolated node) or it has between 2), here 1 and M child nodes. 2), here 1. The tree is perfectly balanced; all of its leaf nodes are at the same level.
  2. Each node, except for the root node and the leaf nodes, has between M / 2), here 1  and M child nodes (hence M / 2), here 1  – 1 and M – 1 key values). The last constraint can be rewritten as a requirement that at least M / 2), here 1  of the pointers in the node not be the null pointer. Here P 0  NULL, and P 1  NULL. If P2), here 1 = NULL, then P 3 = NULL. If P2), here 1  NULL, then P 3 may or may not be NULL.

Leaf Nodes on a B–Tree

Leaf Nodes may contain data, but usually contain pointers to data records (as indicated here). The data are most commonly stored on a disk. These pointers may actually be implemented as addresses on the disk used to store the data files. Note the major structural difference between the index nodes and leaf nodes in a B–Tree of order M. The maximum counts are as follows. Index nodes: (M – 1) keys and M pointers Leaf nodes: (M – 1) keys and (M – 1) pointers, one for each key.

B–Trees Compared to Other Balanced Binary Trees

Binary Tree Data stored in interior nodes B–Tree (also B+ Tree) Interior nodes (index nodes) store only indices. Any given index value may also be a data value, but is not required to be. Leaf nodes store either the data or pointers to the data.

Example: Insertion into a B–Tree of Order Four

Leaf Node Interior Node 3 keys 3 keys 3 pointers 4 pointers Insert ā€œAā€, ā€œBā€, ā€œCā€, ā€œDā€, ā€œEā€, and ā€œFā€ (in that order) into the B–Tree Insert ā€œAā€. This creates the B–Tree. At this point, the tree is just a single node.

Insertion Example (Part 3)

Insert ā€œEā€ Insert ā€œFā€ – This causes another split.