fix: update citations and add periods
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
df46a3651d
commit
ac74ed1262
1 changed files with 18 additions and 22 deletions
|
@ -146,20 +146,18 @@ This chapter will briefly discuss the properties and fundamental ideas behind th
|
||||||
|
|
||||||
\section{Red-black Trees}
|
\section{Red-black Trees}
|
||||||
|
|
||||||
As mentioned previously, red-black trees are among the most popular implementations in standard libraries. As always, we have a binary search tree, and then each node is given \textit{red} or \textit{black} colour. A red-black tree is kept balanced by enforcing the following set of rules:
|
As mentioned previously, red-black trees are among the most popular implementations in standard libraries. As always, we have a binary search tree, and then each node is given \textit{red} or \textit{black} colour. A red-black tree is kept balanced by enforcing the following set of rules~\cite{rbtree}:
|
||||||
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item External nodes are black; internal nodes may be red or black
|
\item External nodes are black; internal nodes may be red or black.
|
||||||
\item For each internal node, all paths from it to external nodes contain the same number of black nodes
|
\item For each internal node, all paths from it to external nodes contain the same number of black nodes.
|
||||||
\item No path from an internal node to an external node contains two red nodes in a row
|
\item No path from an internal node to an external node contains two red nodes in a row.
|
||||||
\end{enumerate}~\cite{rbtree}
|
\end{enumerate}
|
||||||
|
|
||||||
Given this knowledge, we can safely deduce the following relation between the height of the red-black tree and nodes stored in it:
|
|
||||||
|
|
||||||
|
Given this knowledge, we can safely deduce the following relation between the height of the red-black tree and nodes stored in it~\cite{cormen2009introduction}:
|
||||||
\[
|
\[
|
||||||
\log_2{(n + 1)} \leq h \leq 2 \cdot \log_2{(n + 2)} - 2
|
\log_2{(n + 1)} \leq h \leq 2 \cdot \log_2{(n + 2)} - 2
|
||||||
\]\label{rb-height}~\cite{cormen2009introduction}
|
\]\label{rb-height}
|
||||||
|
|
||||||
where the lower bound is given by a perfect binary tree and upper bound by the minimal red-black tree.
|
where the lower bound is given by a perfect binary tree and upper bound by the minimal red-black tree.
|
||||||
|
|
||||||
There are also other variants of the red-black tree that are considered to be simpler for implementation, e.g. left-leaning red-black tree, as described by Sedgewick.
|
There are also other variants of the red-black tree that are considered to be simpler for implementation, e.g. left-leaning red-black tree, as described by Sedgewick.
|
||||||
|
@ -204,7 +202,7 @@ In comparison to nodes in binary search trees, nodes in rank-balanced trees cont
|
||||||
|
|
||||||
\section{Terminology Related to Rank-balanced Trees}
|
\section{Terminology Related to Rank-balanced Trees}
|
||||||
|
|
||||||
In the text and pseudocode we adopt these functions or properties:
|
In the text and pseudocode we adopt these functions or properties~\cite{wavl}:
|
||||||
\begin{itemize}
|
\begin{itemize}
|
||||||
\item function $r(x)$ or property $x.rank$ that returns rank of a node; in case of $r(x)$ there is a special case: $r(nil) = -1$
|
\item function $r(x)$ or property $x.rank$ that returns rank of a node; in case of $r(x)$ there is a special case: $r(nil) = -1$
|
||||||
\item function $parent(x)$ or property $x.parent$ returns parent of a node; analogically for the left and right children of a node
|
\item function $parent(x)$ or property $x.parent$ returns parent of a node; analogically for the left and right children of a node
|
||||||
|
@ -212,7 +210,7 @@ In the text and pseudocode we adopt these functions or properties:
|
||||||
\item $x$ is an \textit{i-child} if its rank-difference is $i$
|
\item $x$ is an \textit{i-child} if its rank-difference is $i$
|
||||||
\item $x$ is an $(i, j)$ node if its left and right children have $i$ and $j$ rank-differences respectively;
|
\item $x$ is an $(i, j)$ node if its left and right children have $i$ and $j$ rank-differences respectively;
|
||||||
ordering of the children does not matter
|
ordering of the children does not matter
|
||||||
\end{itemize}~\cite{wavl}
|
\end{itemize}
|
||||||
|
|
||||||
\section{Rules for the Other Trees}
|
\section{Rules for the Other Trees}
|
||||||
|
|
||||||
|
@ -220,7 +218,7 @@ As we have mentioned above, it is possible to implement different kinds of self-
|
||||||
|
|
||||||
\subsection{AVL Tree}\label{chap:avl-rule}
|
\subsection{AVL Tree}\label{chap:avl-rule}
|
||||||
|
|
||||||
\textbf{AVL Rule}: Every node is (1, 1) or (1, 2).
|
\textbf{AVL Rule}: Every node is (1, 1) or (1, 2).~\cite{wavl}
|
||||||
|
|
||||||
In case of the AVL trees rank represents height. Here we can notice a very smart way of using the \textit{(i, j) node} definition. If we go back to the definition and want to be explicit about the nodes that are allowed with the \textit{AVL Rule}, then we get (1, 1), (1, 2) \textbf{or} (2, 1) nodes. However it is possible to find implementations of the AVL tree that allow leaning \textbf{to only one side} as opposed to the original requirements given by Adelson-Velsky and Landis. Forbidding interchangeability of (i, j) with (j, i) nodes would still yield AVL trees that lean to one side.
|
In case of the AVL trees rank represents height. Here we can notice a very smart way of using the \textit{(i, j) node} definition. If we go back to the definition and want to be explicit about the nodes that are allowed with the \textit{AVL Rule}, then we get (1, 1), (1, 2) \textbf{or} (2, 1) nodes. However it is possible to find implementations of the AVL tree that allow leaning \textbf{to only one side} as opposed to the original requirements given by Adelson-Velsky and Landis. Forbidding interchangeability of (i, j) with (j, i) nodes would still yield AVL trees that lean to one side.
|
||||||
|
|
||||||
|
@ -275,7 +273,8 @@ Example of the AVL tree that uses ranks instead of signs or balance-factors can
|
||||||
|
|
||||||
\subsection{Red-black Tree}\label{chap:rb-rule}
|
\subsection{Red-black Tree}\label{chap:rb-rule}
|
||||||
|
|
||||||
\textbf{Red-Black Rule}: All rank differences are 0 or 1, and no parent of a 0-child is a 0-child. \\
|
\textbf{Red-Black Rule}: All rank differences are 0 or 1, and no parent of a 0-child is a 0-child.~\cite{wavl}
|
||||||
|
|
||||||
In case of red-black trees, rank represents number of black nodes on a path from the node to a leaf (excluding the node itself). Based on that we can discuss the \textit{Red-Black Rule} in detail:
|
In case of red-black trees, rank represents number of black nodes on a path from the node to a leaf (excluding the node itself). Based on that we can discuss the \textit{Red-Black Rule} in detail:
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item \textit{All rank differences are 0 or 1} inductively enforces monotonically increasing (at most by 1) count of black nodes from the leaves. \\
|
\item \textit{All rank differences are 0 or 1} inductively enforces monotonically increasing (at most by 1) count of black nodes from the leaves. \\
|
||||||
|
@ -351,8 +350,7 @@ To show that using rank is mostly an implementation detail, we will describe an
|
||||||
|
|
||||||
Implementation of the insertion is trivial, since it is described by \textit{Haeupler et al.} and is used in the WAVL tree. All we need to implement is the deletion from the AVL tree. We will start by short description of the deletion rebalance as given by \textit{Mareš and Valla} in \textit{Průvodce labyrintem algoritmů}.
|
Implementation of the insertion is trivial, since it is described by \textit{Haeupler et al.} and is used in the WAVL tree. All we need to implement is the deletion from the AVL tree. We will start by short description of the deletion rebalance as given by \textit{Mareš and Valla} in \textit{Průvodce labyrintem algoritmů}.
|
||||||
|
|
||||||
When propagating the error, we can encounter 3 cases (we explain them with respect to propagating deletion from the left subtree, propagation from right is mirrored and role of trits $+$ and $-$ swaps):
|
When propagating the error, we can encounter 3 cases (we explain them with respect to propagating deletion from the left subtree, propagation from right is mirrored and role of trits $+$ and $-$ swaps)~\cite{labyrint}:
|
||||||
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item \textit{Node was marked with $-$.} In this case, heights of left and right subtrees are equal now and node is marked with $0$, but propagation must be continued, since the height of the whole subtree has changed.\label{avl:rules:delete:1}
|
\item \textit{Node was marked with $-$.} In this case, heights of left and right subtrees are equal now and node is marked with $0$, but propagation must be continued, since the height of the whole subtree has changed.\label{avl:rules:delete:1}
|
||||||
\item \textit{Node was marked with $0$.} In this case, node is marked with $+$ and the height of the subrtree has not changed, therefore we can stop the propagation.\label{avl:rules:delete:2}
|
\item \textit{Node was marked with $0$.} In this case, node is marked with $+$ and the height of the subrtree has not changed, therefore we can stop the propagation.\label{avl:rules:delete:2}
|
||||||
|
@ -362,7 +360,7 @@ When propagating the error, we can encounter 3 cases (we explain them with respe
|
||||||
\item $y$ is marked with $0$, then we rotate by $x$ to the left. After the rotation, $x$ can be marked with $+$ and $y$ with $-$. Height of the subtree has not changed, so propagation can be stopped.\label{avl:rules:delete:3b}
|
\item $y$ is marked with $0$, then we rotate by $x$ to the left. After the rotation, $x$ can be marked with $+$ and $y$ with $-$. Height of the subtree has not changed, so propagation can be stopped.\label{avl:rules:delete:3b}
|
||||||
\item $y$ is marked with $-$. Let $z$ be the left son of $y$. We double rotate: first by $z$ to the right and then by $x$ to the left. After the double-rotation $x$ can be marked by either $0$ or $-$, $y$ by $0$ or $+$ and $z$ gets $0$. Height of the subtree has changed, therefore we must propagate further.\label{avl:rules:delete:3c}
|
\item $y$ is marked with $-$. Let $z$ be the left son of $y$. We double rotate: first by $z$ to the right and then by $x$ to the left. After the double-rotation $x$ can be marked by either $0$ or $-$, $y$ by $0$ or $+$ and $z$ gets $0$. Height of the subtree has changed, therefore we must propagate further.\label{avl:rules:delete:3c}
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
\end{enumerate}\label{avl:rules:delete}~\cite{labyrint}
|
\end{enumerate}\label{avl:rules:delete}
|
||||||
|
|
||||||
We have implemented the deletion rebalance by implementing following functions:
|
We have implemented the deletion rebalance by implementing following functions:
|
||||||
|
|
||||||
|
@ -470,9 +468,8 @@ Comparing the \textit{Weak AVL Rule} to the \textit{AVL Rule}, we can come to th
|
||||||
|
|
||||||
\section{Height Boundaries}
|
\section{Height Boundaries}
|
||||||
|
|
||||||
We have described in \autoref{chap:sb-bst} other common self-balanced binary search trees to be able to draw analogies and explain differences between them. Given the boundaries of height for red-black and AVL tree, we can safely assume that the AVL is more strict with regards to the self-balancing than the red-black tree. Let us show how does WAVL fit among them. \textit{Haeupler et al.} present following bounds:
|
We have described in \autoref{chap:sb-bst} other common self-balanced binary search trees to be able to draw analogies and explain differences between them. Given the boundaries of height for red-black and AVL tree, we can safely assume that the AVL is more strict with regards to the self-balancing than the red-black tree. Let us show how does WAVL fit among them. \textit{Haeupler et al.} present following bounds~\cite{wavl}:
|
||||||
|
\[ h \leq k \leq 2h \text{ and } k \leq 2 \log_2{n} \]
|
||||||
\[ h \leq k \leq 2h \text{ and } k \leq 2 \log_2{n} \]~\cite{wavl}
|
|
||||||
|
|
||||||
In those equations we can see $h$ and $n$ in the same context as we used it to lay boundaries for the AVL and red-black trees, but we can also see new variable $k$, which represents the rank of the tree.
|
In those equations we can see $h$ and $n$ in the same context as we used it to lay boundaries for the AVL and red-black trees, but we can also see new variable $k$, which represents the rank of the tree.
|
||||||
|
|
||||||
|
@ -544,8 +541,7 @@ In the \autoref{algorithm:wavl:insert} we have also utilized a helper function t
|
||||||
|
|
||||||
Rebalancing after insertion in the WAVL tree is equivalent to rebalancing after insertion in the AVL tree. We will start with a short description of the rebalancing within AVL to lay a foundation for analogies and differences compared to the implementation using ranks.
|
Rebalancing after insertion in the WAVL tree is equivalent to rebalancing after insertion in the AVL tree. We will start with a short description of the rebalancing within AVL to lay a foundation for analogies and differences compared to the implementation using ranks.
|
||||||
|
|
||||||
When propagating the error, we can encounter 3 cases (we explain them with respect to propagating insertion from the left subtree, propagation from right is mirrored and role of trits $+$ and $-$ swaps):
|
When propagating the error, we can encounter 3 cases (we explain them with respect to propagating insertion from the left subtree, propagation from right is mirrored and role of trits $+$ and $-$ swaps)~\cite{labyrint}:
|
||||||
|
|
||||||
\begin{enumerate}
|
\begin{enumerate}
|
||||||
\item \textit{Node was marked with $+$.} In this case, heights of left and right subtrees are equal now and node is marked with $0$ and propagation can be stopped.\label{avl:rules:insert:1}
|
\item \textit{Node was marked with $+$.} In this case, heights of left and right subtrees are equal now and node is marked with $0$ and propagation can be stopped.\label{avl:rules:insert:1}
|
||||||
\item \textit{Node was marked with $0$.} In this case, node is marked with $-$, but the height of the tree rooted at the node has changes, which means that we need to propagate the changes further.\label{avl:rules:insert:2}
|
\item \textit{Node was marked with $0$.} In this case, node is marked with $-$, but the height of the tree rooted at the node has changes, which means that we need to propagate the changes further.\label{avl:rules:insert:2}
|
||||||
|
@ -555,7 +551,7 @@ When propagating the error, we can encounter 3 cases (we explain them with respe
|
||||||
\item $x$ is marked with $+$, then we double rotate: first by $x$ to the left and then by $z$ to the right. Here we need to recalculate the balance-factors for $z$ and $x$, where $z$ gets $-$ or $0$ and $x$ gets $0$ or $+$. Node that was a right child to the $x$ before the double-rotation is now marked with $0$ and propagation can be stopped.\label{avl:rules:insert:3b}
|
\item $x$ is marked with $+$, then we double rotate: first by $x$ to the left and then by $z$ to the right. Here we need to recalculate the balance-factors for $z$ and $x$, where $z$ gets $-$ or $0$ and $x$ gets $0$ or $+$. Node that was a right child to the $x$ before the double-rotation is now marked with $0$ and propagation can be stopped.\label{avl:rules:insert:3b}
|
||||||
\item $x$ is marked with $0$. This case is trivial, since it cannot happen, because we never propagate the height change from a node that acquired sign $0$.
|
\item $x$ is marked with $0$. This case is trivial, since it cannot happen, because we never propagate the height change from a node that acquired sign $0$.
|
||||||
\end{enumerate}
|
\end{enumerate}
|
||||||
\end{enumerate}~\cite{labyrint}
|
\end{enumerate}
|
||||||
|
|
||||||
In the following explanation we have to consider that valid nodes in AVL tree implemented via ranks are (1, 1) and (1, 2) and by the time of evaluating rank-differences of parent, they are already affected by the rebalancing done from the inserted leaf.
|
In the following explanation we have to consider that valid nodes in AVL tree implemented via ranks are (1, 1) and (1, 2) and by the time of evaluating rank-differences of parent, they are already affected by the rebalancing done from the inserted leaf.
|
||||||
|
|
||||||
|
|
Reference in a new issue