Algorithm 6 Binary Tree Traversal Nonrecursive with Constant Space

Require: a binary tree TT

Ensure: print all keys appearing in TT

1:prev=NILprev = NIL

2:x=T.rootx = T.root

3:while xNILx \ne NIL do

4:if prev==x.pprev == x.p then // T.root.pT.root.p is NILNIL

5:print x.keyx.key

6:prev=xprev = x

7:if x.leftNILx.left \ne NIL then

8:x=x.leftx = x.left

9:else if x.rightNILx.right \ne NIL then

10:x=x.rightx = x.right

11:else

12:x=x.px = x.p

13:end if

14:else if prev==x.leftprev == x.left and x.rightNILx.right \ne NIL then

15:prev=xprev = x

16:x=x.rightx = x.right

17:else

18:prev=xprev = x

19:x=x.px = x.p

20:end if

21:end while