Programs that utilize tree structures need to process nodes in a tree (represented as circles in below diagram). Nodes contain information about an object. For now, let's assume each node contains a letter.
Pre-Order Traversal is a type of Tree Traversal algorithm. Pre-order refers to when the root is processed previous to its two subtrees.
Given a non-empty tree,
The order would go
A,B,D,E,G,C,F
Here is an example of Preorder in C++
Steps to Preorder Traversal
Given a binary tree PY:template
The same example in Haskell might look like
data Tree a = ET | Node(a, Tree a, Tree a)Compare: Inorder traversal, Post-order traversalpreorder :: Tree a -> [a] preorder ET = [] preorder (Node (x, left,right)) = x : (preorder left) ++ (preorder right)