Language: EN FI

Exercises > Preorder with stack

These exercises demonstrate the content of the ByTheMark service. These are meant for private individuals for learning data structures and algorithms. If you want to try out more content, you can register to the ByTheMark Personal free of charge.

Traverse the following binary tree in preorder. Show the contents of the traversal's stack as the algorithm progresses.

PreOrderTreeTraversal(root)
1  S.push(root)
2  while (S.notEmpty()) do
3    next = S.pop()
4    visit(next)
5    if (next->right != NULL)
       S.push(next->right)
6    if (next->left != NULL)
       S.push(next->left)

  Last modified Tue Jun 28 21:01:56 EEST 2011