feat(bst): add b-tree

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-18 11:34:17 +02:00
parent 5531b4187a
commit 7cb34090ce
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -53,6 +53,23 @@ Regarding the implementation of AVL trees, we can see them implemented in the st
\section {B-tree}
\textit{To keep or not to keep…}
B-tree is a self-balancing tree as described by \textit{Bayer and McCreight}~\cite{btree}. We will list the rules given by \textit{Cormen et al.}~\cite{cormen2009introduction}:
\begin{enumerate}
\item Every node has attributes holding current number of keys it stores $n$, $n$ keys stored in non-decreasing order and optionally boolean value determining whether node is leaf or not.
\item Each internal node holds $n + 1$ pointers to children. Leaf nodes have no children.
\item Keys in internal nodes separate the ranges of keys that can be stored in the subtrees. If $k_i$ is any key stored in the subtree with root $x.c_i$, then
\[ k_1 \leq x.key_1 \leq k_2 \leq x.key_2 \leq \dots \leq x.key_n \leq k_{n + 1} \]
\item All leaves are in the same depth.
\item Each B-tree has a fixed integer $t \geq 2$ called \textit{minimum degree}, which gives bounds to number of keys that can be stored in a node. If $k$ is number of keys, then
\[ t - 1 \leq k \leq 2t - 1 \]
Minimal number of keys stored in a node does not apply to the root node.
\end{enumerate}
Used in Rust.
We have chosen the rules from \textit{Introduction to Algorithms}~\cite{cormen2009introduction}, because the terminology is more familiar and they are more compact than the rules given in the original paper by \textit{Bayer and McCreight}~\cite{btree}, where they introduce B-trees for organization of files in filesystems as the title suggests.
Based on the original paper~\cite{btree} and \textit{Introduction to Algorithms}~\cite{cormen2009introduction}, we have deduced following height boundaries:
\[ 1 + \log_{t}{ \frac{(t - 1)(n - 1)}{2} + 1} \leq h \leq \log_t{\frac{n + 1}{2}} \]
%% TODO: ↑ Check this thing once again ↑
Even though original paper has presented B-tree to be used in filesystems or databases, there are also cases when B-tree is used to represent sets, for example in Rust~\footnote{\url{https://github.com/rust-lang/rust/blob/master/library/alloc/src/collections/btree/map.rs} as of commit \texttt{9805437}; $t = 6$}.