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{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}
\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:
\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 \texttt{deleteFixNode} that handles one adjustment of rebalancing as described above
\item \texttt{deleteRotate} that handles rotation and updating of ranks, if necessary
\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 \avlDeleteFixNode{} that handles one adjustment of rebalancing as described above
\item \avlDeleteRotate{} that handles rotation and updating of ranks, if necessary
\end{enumerate}
\begin{algorithm}
@ -369,7 +382,7 @@ We have implemented the deletion rebalance by implementing following functions:
$(y, parent) \gets (parent, parent.parent)$\;
}
\BlankLine
\While{$y \neq nil \land \texttt{deleteFixNode}(T, y, parent)$}{
\While{$y \neq nil \land \avlDeleteFixNode(T, y, parent)$}{
$y \gets parent$\;
\eIf{$parent \neq nil$}{
$parent \gets parent.parent$\;
@ -385,22 +398,22 @@ We have implemented the deletion rebalance by implementing following functions:
\begin{algorithm}
\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$\;
\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$}\;
}
\BlankLine
$(l, r) \gets (x.left, x.right)$\;
$(rotateL, rotateR) \gets (\texttt{rotateLeft}, \texttt{rotateRight})$\;
\BlankLine
\tcp{Handles rule 3}
\tcp{Handles \hyperref[avl:rules:delete:3]{rule 3}}
\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}
@ -417,19 +430,19 @@ There are two operations that are not described using helper functions and they
\begin{algorithm}
\Proc{$\texttt{deleteRotate}(T, x, y, leaning, rotateL, rotateR)$}{
$factor \gets $ balance-factor of $y$\;
$f \gets $ balance-factor of $y$\;
\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)$\;
}
\Else(\tcp*[h]{Handles rule 3c}){
\Else(\tcp*[h]{Handles \hyperref[avl:rules:delete:3c]{rule 3c}}){
$rotateR(T, y)$\;
$rotateL(T, x)$\;
}
\BlankLine
update ranks of $x$, $y$ and new root of the subtree\;
\BlankLine
\Return{$factor \neq 0$}\;
\Return{$f \neq 0$}\;
}
\caption{\texttt{deleteRotate} algorithm for the AVL tree}\label{algorithm:avl:deleteRotate}
\end{algorithm}
@ -489,7 +502,7 @@ Inserting values into WAVL tree is equivalent to inserting values into regular b
\Return\;
}
\BlankLine
$parent \gets \texttt{findParentNode}(key, T.root)$\;
$parent \gets \findParentNode(key, T.root)$\;
\If{$parent = nil$}{
\Return\;
}
@ -502,7 +515,7 @@ Inserting values into WAVL tree is equivalent to inserting values into regular b
$parent.right \gets insertedNode$\;
}
\BlankLine
$\texttt{bottomUpInsertRebalance}(T, insertedNode)$\;
$\wavlInsertRebalance(T, insertedNode)$\;
}
\caption{Insert operation on binary search tree}\label{algorithm:wavl:insert}
\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:
\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))$}{
$\texttt{promote}(node.parent)$\;
$node \gets node.parent$\;
}
\BlankLine
\tcp{Handles \hyperref[avl:rules:insert:1]{rule 1}}
\lIf{$node.parent = nil \lor node\, is\, not\, \text{0-child}$}{\Return}
\BlankLine
\tcp{Handles \hyperref[avl:rules:insert:3]{rule 3}}
\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
}
@ -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)$}{
$z \gets x.parent$\;
\BlankLine
\uIf{$y = nil \lor y\, is\, \text{2-child}$}{
$newRoot \gets rotateToRight(T, z)$\;
\uIf(\tcp*[h]{Handles \hyperref[avl:rules:insert:3a]{rule 3a}}){$y = nil \lor y\, is\, \text{2-child}$}{
$rotateToRight(T, z)$\;
\BlankLine
$\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)$\;
$rotateToRight(T, z)$\;
\BlankLine
@ -597,7 +613,7 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
\section{\texttt{delete}}
\begin{algorithm}
\Proc{$\texttt{bottomUpDeleteRebalance}(T, y, parent)$}{
\Proc{$\texttt{deleteRebalance}(T, y, parent)$}{
\uIf{$y \text{ is (2, 2)}$}{
$\texttt{demote}(y)$\;
$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$\;
\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}
@ -656,9 +672,9 @@ Bottom-up rebalancing after the insertion into WAVL tree is identical to AVL tre
}
$p \gets parent$\;
\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}