📚 Drozdek (Ch. 7)
A multiway tree of order \(m\) is a generalized tree in which each node may have up to \(m\) children (each of which may also be trees).
A multiway search tree of order \(m\) is a multiway tree in which:
ceil(m/2)
and \(m\).ceil(m/2)
and \(m\).
Example of a B-tree of order 5:
B-Tree
Drozdek Figure 7.7
BTNode* BTree::search(keyType k, BTNode* subroot=root) const {
int i = 0;
BTNode* ans = nullptr;
if(subroot){
while(i < subroot->key_count && subroot->keys[i] < k){
++i; // Do nothing, except increment i
}
if(i >= subroot->key_count || subroot->keys[i] > k){
ans = search(k, subroot->pointers[i]);
}
else{
ans = subroot;
}
}
return ans;
}
Drozdek Figure 7.7
B-Tree
Drozdek Figure 7.8
B-Tree
Drozdek Figure 7.8
B-Tree
Drozdek Figure 7.8
B-Tree
Drozdek Figure 7.9
B-Tree
Drozdek Figure 7.9
B-Tree
Drozdek Figure 7.9
B* trees work like B-tree, except that nodes must remain 2/3 full at all times. (Average utilization becomes 81%.)
B+ trees try to optimize traversals with respect to secondary storage by only storing data at the leaves, and using internal nodes as an “index”.
R-trees represent spatial (2-D or 3-D) data.
If you restrict a B tree so that the order is small, it can be used for efficient search trees in memory.
Due to the overhead of the unused storage in each node, you might want to eliminate it and use a strict binary tree.
We can do both.
Red-black trees (or vertical-horizontal trees) do this by maintaining two kinds of links (designated by a flag).
One kind links to children, the other links to other node elements within the same logical B-tree node.
See Drozdek 337-352.
Comparision of 2-3-4 tree vs. Red-Black tree vs. Horizontal-Vertical tree
2-3-4 vs. Red-Black Trees
Drozdek Figure 7.23
Comparision of 2-3-4 tree vs. Red-Black tree vs. Horizontal-Vertical tree
Complete 2-3-4 vs. Red-Black Trees
Drozdek Figure 7.23
Pronounced “try”, but comes from the word “retrieval”. 🤷♂️
Each node contains only a part of the key being searched.
Often, the keys are “words” (sequences of letters); each node contains a single letter.
Nodes are either leaf nodes (containing a word or the suffix or a word), or non-leaf nodes (with an array of pointers to sub-tries).
Very fast for things like spell checkers, etc.
Trie height depends on the length of common prefixes.
See Drozdek 364-372
This one has words containing the letters A, E, I, R, P.
A Trie
Notice that there can be a lot of unused pointers in the nodes…
Drozdek Figure 7.38
Same as the previous trie, but implemented as a h-v tree to reduce the unused storage locations.
Drozdek Figure 7.40
Multiway Trees (m-Trees)
CS 50x2 Accelerated Programming Series