A NON-RECURSIVE IMPLEMENTATION OF AN IN-ORDER TRAVERSAL. This example illustrates that sometimes a non-recursive method of solving a problem can be quite complex. (* p is a pointer to the tree to be traversed *) PROCEDURE Inorder(p: NodePtr) ; VAR S: stack ; (* declare stack variable S *) ok, finished :boolean ; BEGIN (* procedure Inorder *) Stacks.Create(S,ok) ; (* initialize the stack *) WHILE p <> nil (* as long as p points somewhere *) DO BEGIN (* outer while loop *) (* as long as you can go left from where p points. *) WHILE p^.left <> nil DO BEGIN (* inner while loop *) Push(S,p) ; (* push p onto the stack *) p := p^.left ; (* advance p to the left *) end (* inner while loop *) finished := false ; REPEAT Process(p) ; (* Print what's in the node, or whatever. *) IF p^.right = nil ; (* if there is no node to the right *) THEN BEGIN IF NOT Stacks.Empty(S) (* if the stack is not empty *) THEN Pop(S,p) (* pop a pointer off the stack *) ELSE BEGIN finished := true ; (* signal to stop repeat loop *) p := nil ; (* signal to stop outer while loop *) end end ELSE BEGIN (* work on the right subtree *) p := p^.right ; (* point at the right subtree *) finished := true ; (* signal to stop repeat loop *) END UNTIL finished ; END (* outer while loop *) Stack.Terminate(S) ; (* deallocate the stack *) END (* procedure Inorder *)