\begin{algorithm}
        \caption{Binary Tree Traversal Nonrecursive with Constant Space}
        \begin{algorithmic}
        \REQUIRE a binary tree $T$
        \ENSURE print all keys appearing in $T$
        \STATE $prev = NIL$
        \STATE $x = T.root$
        \WHILE{$x \ne NIL$}
            \IF{$prev == x.p$} \COMMENT{ $T.root.p$ is $NIL$}
                \PRINT $x.key$
                \STATE $prev = x$
                \IF{$x.left \ne NIL$}
                    \STATE $x = x.left$
                \ELSEIF{$x.right \ne NIL$}
                    \STATE $x = x.right$
                \ELSE
                    \STATE $x = x.p$
                \ENDIF
            \ELSEIF{$prev == x.left$ \AND $x.right \ne NIL$}
                \STATE $prev = x$
                \STATE $x = x.right$
            \ELSE
                \STATE $prev = x$
                \STATE $x = x.p$
            \ENDIF
        \ENDWHILE
        \end{algorithmic}
        \end{algorithm}