I want to write the following algorithm in LaTeX.
The code I used is as follows:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}
%\usepackage{algpseudocode}
%\algtext*{EndIf}
\begin{document}
\begin{algorithm}
\caption{Left-rotate $(T,x)$}
\begin{algorithmic}
\STATE $y \gets \mathrm{right}[x]$
\STATE $ \mathrm{right}[x] \gets \mathrm{left}[y]$
\IF {$ \mathrm{left}[y] \neq \mathrm{NIL} $}
\STATE $ p[\mathrm{left}[y]] \gets x $
\ENDIF
\IF {$ p[x]=\mathrm{NIL} $}
\STATE $ \mathrm{root}[T] \gets y $
\ELSIF {$ x=\mathrm{left}[p[x]] $}
\STATE $ \mathrm{left}[p[x]] \gets y $
\ELSE
\STATE $ \mathrm{right}[p[x]] \gets y $
\ENDIF
\STATE $ \mathrm{left}[y] \gets x $
\STATE $ p[x] \gets x $
\end{algorithmic}
\end{algorithm}
\end{document}
I have already tried all the solutions on this platform but unfortunately, none worked. The output generated is the following:
I do not want the \ENDIF, it would be nice if then can be on the line below the one with \IF, and the nesting should be according to the first image. Please help me out.


