feat: improve algorithms

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2022-05-14 23:05:06 +02:00
parent 3952d043ba
commit e824177cc1
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -114,6 +114,19 @@
\SetKwProg{Fn}{function}{ is}{end} \SetKwProg{Fn}{function}{ is}{end}
\SetKwProg{Proc}{procedure}{ is}{end} \SetKwProg{Proc}{procedure}{ is}{end}
\newcommand{\avlDeleteRebalance}{\hyperref[algorithm:avl:deleteRebalance]{\texttt{deleteRebalance}}}
\newcommand{\avlDeleteFixNode}{\hyperref[algorithm:avl:deleteFixNode]{\texttt{deleteFixNode}}}
\newcommand{\avlDeleteRotate}{\hyperref[algorithm:avl:deleteRotate]{\texttt{deleteRotate}}}
\newcommand{\findParentNode}{\hyperref[algorithm:findParentNode]{\texttt{findParentNode}}}
\newcommand{\wavlInsertRebalance}{\hyperref[algorithm:wavl:insertRebalance]{\texttt{insertRebalance}}}
\newcommand{\wavlFixZeroChild}{\hyperref[algorithm:wavl:fix0Child]{\texttt{fix0Child}}}
\newcommand{\wavlDeleteRebalance}{\hyperref[algorithm:wavl:deleteRebalance]{\texttt{deleteRebalance}}}
\newcommand{\wavlBottomUpDelete}{\hyperref[algorithm:wavl:bottomUpDelete]{\texttt{bottomUpDelete}}}
\newcommand{\wavlFixDelete}{\hyperref[algorithm:wavl:fixDelete]{\texttt{fixDelete}}}
\begin{document} \begin{document}
\chapter*{Introduction} \chapter*{Introduction}
@ -354,9 +367,9 @@ When propagating the error, we can encounter 3 cases (we explain them with respe
We have implemented the deletion rebalance by implementing following functions: We have implemented the deletion rebalance by implementing following functions:
\begin{enumerate} \begin{enumerate}
\item \texttt{deleteRebalance} that handles updating the current node and its parent and iteratively calls subroutine handling previously described \textit{one step of a rebalancing} \item \avlDeleteRebalance{} that handles updating the current node and its parent and iteratively calls subroutine handling previously described \textit{one step of a rebalancing}
\item \texttt{deleteFixNode} that handles one adjustment of rebalancing as described above \item \avlDeleteFixNode{} that handles one adjustment of rebalancing as described above
\item \texttt{deleteRotate} that handles rotation and updating of ranks, if necessary \item \avlDeleteRotate{} that handles rotation and updating of ranks, if necessary
\end{enumerate} \end{enumerate}
\begin{algorithm} \begin{algorithm}
@ -369,7 +382,7 @@ We have implemented the deletion rebalance by implementing following functions:
$(y, parent) \gets (parent, parent.parent)$\; $(y, parent) \gets (parent, parent.parent)$\;
} }
\BlankLine \BlankLine
\While{$y \neq nil \land \texttt{deleteFixNode}(T, y, parent)$}{ \While{$y \neq nil \land \avlDeleteFixNode(T, y, parent)$}{
$y \gets parent$\; $y \gets parent$\;
\eIf{$parent \neq nil$}{ \eIf{$parent \neq nil$}{
$parent \gets parent.parent$\; $parent \gets parent.parent$\;
@ -385,22 +398,22 @@ We have implemented the deletion rebalance by implementing following functions:
\begin{algorithm} \begin{algorithm}
\Proc{$\texttt{deleteFixNode}(T, x, parent)$}{ \Proc{$\texttt{deleteFixNode}(T, x, parent)$}{
\uIf(\tcp*[h]{Handles rule 1}){balance-factor of $x$ is $0$}{ \uIf(\tcp*[h]{Handles \hyperref[avl:rules:delete:1]{rule 1}}){balance-factor of $x$ is $0$}{
update rank of $x$\; update rank of $x$\;
\Return{$true$}\; \Return{$true$}\;
} }
\ElseIf(\tcp*[h]{Handles rule 2}){balance-factor of $x$ is $-1$ or $1$}{ \ElseIf(\tcp*[h]{Handles \hyperref[avl:rules:delete:2]{rule 2}}){balance-factor of $x$ is $-1$ or $1$}{
\Return{$false$}\; \Return{$false$}\;
} }
\BlankLine \BlankLine
$(l, r) \gets (x.left, x.right)$\; $(l, r) \gets (x.left, x.right)$\;
$(rotateL, rotateR) \gets (\texttt{rotateLeft}, \texttt{rotateRight})$\; $(rotateL, rotateR) \gets (\texttt{rotateLeft}, \texttt{rotateRight})$\;
\BlankLine \BlankLine
\tcp{Handles rule 3} \tcp{Handles \hyperref[avl:rules:delete:3]{rule 3}}
\eIf{balance-factor of $x$ is $2$}{ \eIf{balance-factor of $x$ is $2$}{
\Return{$\texttt{deleteRotate}(T, x, r, 1, rotateL, rotateR)$}\; \Return{$\avlDeleteRotate(T, x, r, 1, rotateL, rotateR)$}\;
}{ }{
\Return{$\texttt{deleteRotate}(T, x, l, -1, rotateR, rotateL)$}\; \Return{$\avlDeleteRotate(T, x, l, -1, rotateR, rotateL)$}\;
} }
} }
\caption{\texttt{deleteFixNode} algorithm for the AVL tree}\label{algorithm:avl:deleteFixNode} \caption{\texttt{deleteFixNode} algorithm for the AVL tree}\label{algorithm:avl:deleteFixNode}
@ -417,19 +430,19 @@ There are two operations that are not described using helper functions and they
\begin{algorithm} \begin{algorithm}
\Proc{$\texttt{deleteRotate}(T, x, y, leaning, rotateL, rotateR)$}{ \Proc{$\texttt{deleteRotate}(T, x, y, leaning, rotateL, rotateR)$}{
$factor \gets $ balance-factor of $y$\; $f \gets $ balance-factor of $y$\;
\BlankLine \BlankLine
\uIf(\tcp*[h]{Handles rule 3ab}){$factor = 0 \lor factor = leaning$}{ \uIf(\tcp*[h]{Handles rules \hyperref[avl:rules:delete:3a]{3a} \& \hyperref[avl:rules:delete:3b]{3b}}){$f = 0 \lor f = leaning$}{
$rotateL(T, x)$\; $rotateL(T, x)$\;
} }
\Else(\tcp*[h]{Handles rule 3c}){ \Else(\tcp*[h]{Handles \hyperref[avl:rules:delete:3c]{rule 3c}}){
$rotateR(T, y)$\; $rotateR(T, y)$\;
$rotateL(T, x)$\; $rotateL(T, x)$\;
} }
\BlankLine \BlankLine
update ranks of $x$, $y$ and new root of the subtree\; update ranks of $x$, $y$ and new root of the subtree\;
\BlankLine \BlankLine
\Return{$factor \neq 0$}\; \Return{$f \neq 0$}\;
} }
\caption{\texttt{deleteRotate} algorithm for the AVL tree}\label{algorithm:avl:deleteRotate} \caption{\texttt{deleteRotate} algorithm for the AVL tree}\label{algorithm:avl:deleteRotate}
\end{algorithm} \end{algorithm}
@ -489,7 +502,7 @@ Inserting values into WAVL tree is equivalent to inserting values into regular b
\Return\; \Return\;
} }
\BlankLine \BlankLine
$parent \gets \texttt{findParentNode}(key, T.root)$\; $parent \gets \findParentNode(key, T.root)$\;
\If{$parent = nil$}{ \If{$parent = nil$}{
\Return\; \Return\;
} }
@ -502,7 +515,7 @@ Inserting values into WAVL tree is equivalent to inserting values into regular b
$parent.right \gets insertedNode$\; $parent.right \gets insertedNode$\;
} }
\BlankLine \BlankLine
$\texttt{bottomUpInsertRebalance}(T, insertedNode)$\; $\wavlInsertRebalance(T, insertedNode)$\;
} }
\caption{Insert operation on binary search tree}\label{algorithm:wavl:insert} \caption{Insert operation on binary search tree}\label{algorithm:wavl:insert}
\end{algorithm} \end{algorithm}
@ -555,18 +568,21 @@ Once the node does not have a parent (is root node) or is not (0, 1) or (1, 0) n
Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tree insertion: Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tree insertion:
\begin{algorithm} \begin{algorithm}
\Proc{$\texttt{bottomUpInsertRebalance}(T, node)$}{ \Proc{$\texttt{insertRebalance}(T, node)$}{
\tcp{Handles \hyperref[avl:rules:insert:2]{rule 2}}
\While{$node.parent \neq nil \land (node.parent\, is\, (0, 1)\, or\, (1, 0))$}{ \While{$node.parent \neq nil \land (node.parent\, is\, (0, 1)\, or\, (1, 0))$}{
$\texttt{promote}(node.parent)$\; $\texttt{promote}(node.parent)$\;
$node \gets node.parent$\; $node \gets node.parent$\;
} }
\BlankLine \BlankLine
\tcp{Handles \hyperref[avl:rules:insert:1]{rule 1}}
\lIf{$node.parent = nil \lor node\, is\, not\, \text{0-child}$}{\Return} \lIf{$node.parent = nil \lor node\, is\, not\, \text{0-child}$}{\Return}
\BlankLine \BlankLine
\tcp{Handles \hyperref[avl:rules:insert:3]{rule 3}}
\eIf{$node = node.parent.left$}{ \eIf{$node = node.parent.left$}{
$\texttt{fix0Child}(T, node, node.right, \texttt{rotateLeft}, \texttt{rotateRight})$\; $\wavlFixZeroChild(T, node, node.right, \texttt{rotateLeft}, \texttt{rotateRight})$\;
}{ }{
$\texttt{fix0Child}(T, node, node.left, \texttt{rotateRight}, \texttt{rotateLeft})$\; $\wavlFixZeroChild(T, node, node.left, \texttt{rotateRight}, \texttt{rotateLeft})$\;
} }
\BlankLine \BlankLine
} }
@ -577,12 +593,12 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
\Proc{$\texttt{fix0Child}(T, x, y, rotateToLeft, rotateToRight)$}{ \Proc{$\texttt{fix0Child}(T, x, y, rotateToLeft, rotateToRight)$}{
$z \gets x.parent$\; $z \gets x.parent$\;
\BlankLine \BlankLine
\uIf{$y = nil \lor y\, is\, \text{2-child}$}{ \uIf(\tcp*[h]{Handles \hyperref[avl:rules:insert:3a]{rule 3a}}){$y = nil \lor y\, is\, \text{2-child}$}{
$newRoot \gets rotateToRight(T, z)$\; $rotateToRight(T, z)$\;
\BlankLine \BlankLine
$\texttt{demote}(z)$\; $\texttt{demote}(z)$\;
} }
\ElseIf{$y\, is\, \text{1-child}$}{ \ElseIf(\tcp*[h]{Handles \hyperref[avl:rules:insert:3b]{rule 3b}}){$y\, is\, \text{1-child}$}{
$rotateToLeft(T, x)$\; $rotateToLeft(T, x)$\;
$rotateToRight(T, z)$\; $rotateToRight(T, z)$\;
\BlankLine \BlankLine
@ -597,7 +613,7 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
\section{\texttt{delete}} \section{\texttt{delete}}
\begin{algorithm} \begin{algorithm}
\Proc{$\texttt{bottomUpDeleteRebalance}(T, y, parent)$}{ \Proc{$\texttt{deleteRebalance}(T, y, parent)$}{
\uIf{$y \text{ is (2, 2)}$}{ \uIf{$y \text{ is (2, 2)}$}{
$\texttt{demote}(y)$\; $\texttt{demote}(y)$\;
$parent \gets y.parent$\; $parent \gets y.parent$\;
@ -613,7 +629,7 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
$z \gets \text{3-child of } parent$\; $z \gets \text{3-child of } parent$\;
\If{$z \neq nil$}{ \If{$z \neq nil$}{
$\texttt{bottomUpDelete}(T, z, parent)$\; $\wavlBottomUpDelete(T, z, parent)$\;
} }
} }
\caption{Initial phase of algorithm for the rebalance after deletion from the WAVL tree}\label{algorithm:wavl:bottomUpDeleteRebalance} \caption{Initial phase of algorithm for the rebalance after deletion from the WAVL tree}\label{algorithm:wavl:bottomUpDeleteRebalance}
@ -656,9 +672,9 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
} }
$p \gets parent$\; $p \gets parent$\;
\eIf{$parent.left = x$}{ \eIf{$parent.left = x$}{
$\texttt{fixDelete}(T, x, p.right, p, false, \texttt{rotateLeft}, \texttt{rotateRight})$\; $\wavlFixDelete(T, x, p.right, p, false, \texttt{rotateLeft}, \texttt{rotateRight})$\;
}{ }{
$\texttt{fixDelete}(T, x, p.left, p, true, \texttt{rotateRight}, \texttt{rotateLeft})$\; $\wavlFixDelete(T, x, p.left, p, true, \texttt{rotateRight}, \texttt{rotateLeft})$\;
} }
} }
\caption{Propagation of the broken rank rule after deletion from the WAVL tree}\label{algorithm:wavl:bottomUpDelete} \caption{Propagation of the broken rank rule after deletion from the WAVL tree}\label{algorithm:wavl:bottomUpDelete}