110 likes | 224 Views
B + Trees. Dale-Marie Wilson, Ph.D. B + Trees. Search Tree Used to guide search for a record, given the value of one of its fields Two types of Nodes Internal Nodes contain Key values and node pointers Leaf Nodes contain Key, Record-Pointer pairs. B + Trees.
E N D
B+ Trees Dale-Marie Wilson, Ph.D.
B+ Trees • Search Tree • Used to guide search for a record, given the value of one of its fields • Two types of Nodes • Internal Nodes contain Key values and node pointers • Leaf Nodes contain Key, Record-Pointer pairs
B+ Trees • The structure of internal nodes in a B+ tree of order p: • Each internal node is of the form <P1, K1, P2, K2, ..., Pq-1, Kq-1, Pq > , where q <= p , each Pi is a tree pointer • Within each internal node, K1 < K2 < ... < Kq-1 • For all values of X in the subtree pointed at by Pi , we have Ki-1 < X < Ki for 1 < i < q , X < Ki for i=q, and Ki-1 < X for i=q • Each internal node has at most p tree pointers • Each internal node, except the root, has at least (p/2) tree pointers. The root node has at least two tree pointers if it is an internal node. • An internal node with q pointers, q <= p, has q-1 search field values.
B+ Trees • The structure of leaf nodes in a B+ tree of order p: • Each leaf node is of the form < <K1,Pr1>, <K2,Pr2>, ..., <Kq-1,Prq-1>, Pnext > , where q <= p , each Pri is a data pointer that points to a record or block of records • Within each internal node, K1 < K2 < ... < Kq-1 • Each leaf node, has at least (p/2) values • All leaf nodes are at the same level • The Pnext pointer points to the next leaf node in the tree • This give efficient sequential access to data
B+ Trees • Insertion example for B+ Tree: • When you insert into a leaf node that is full, you split and pass the middle value up to the parent • When you insert into a full root, the root splits and a new root is created with the middle value from the child nodes • Otherwise, values are inserted into openings at the lowest level
B+ Trees proc insert (nodepointer, entry, newchildentry) if *nodepointer is a non-leaf node, say N, find i such that Ki <= entry's key value < K i+1; insert (Pi, entry, newchildentry); if newchildentry is null, return; else, if N has space, put *newchildentry on it, set newchildentry to null, return; else, split N: first d key values and d + 1 nodepointers stay, last d keys and d + 1 pointers move to new node, N2: newchildentry = &(<smallest key value on N2, pointer to N2>); if N is the root, create new node with (pointer to N, *newchildentry); make the tree's root-node pointer point to the new node; return; if *nodepointer is a leaf node, say L, if L has space, put entry on it, set new childentry to null and return; else, split L: first d entries stay, rest move to brand new node L2; newchildentry = &(<smallest key value on L2, pointer to L2>); set sibling pointers in L and L2; return; endproc
In-class Example: • Write the B+ Tree (p = 4) for the following values that are inserted into the B Tree in the order below. 17, 2, 98, 23, 27, 34, 0, 44, 68, 19, 72, 10, 5, 55, 3