NIST

preorder traversal

(algorithm)

Definition: Process all nodes of a tree by processing the root, then recursively processing all subtrees.

Also known as prefix traversal.

Generalization (I am a kind of ...)
tree traversal, depth-first search.

See also in-order traversal, postorder traversal, level-order traversal.

Note: For instance, if the "processing" is just printing, a tree is printed as "root (first subtree) (second subtree) ... (last subtree)." Here is pseudocode for a binary tree:

 preorder(tree)
begin
if tree is null, return;

print(tree.root);
preorder(tree.left_subtree);
preorder(tree.right_subtree);
end

Author: PEB

Implementation

Mukundan's animation (Java).
Go to the Dictionary of Algorithms and Data Structures home page.

If you have suggestions, corrections, or comments, please get in touch with Paul E. Black.

Entry modified 14 August 2008.
HTML page formatted Tue Dec 6 16:16:32 2011.

Cite this as:
Paul E. Black, "preorder traversal", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed., U.S. National Institute of Standards and Technology. 14 August 2008. (accessed TODAY) Available from: http://www.nist.gov/dads/HTML/preorderTraversal.html

to NIST home page