Tuesday, September 23, 2008

Binary Tree Traversals

One of the important features of Genetic Programming is the tree structure which is the primary method of representing an individual. Understanding binary tree traversals is therefore required. The 3 methods of traversing a binary tree are Preorder, Postorder and Inorder traversal.

Preorder Traversal:
begin
   if tree=null return;
   print(tree.root)
   preorder(tree.leftsubtree)
   preorder(tree.rightsubtree)
end

Postorder Traversal
begin
   if tree=null return
   postorder(tree.leftsubtree)
   postorder(tree.rightsubtree)
   print tree.root
end

Inorder Traversal
begin
   if tree=null return
   inorder(tree.leftsubtree)
   print(tree.root)
   inorder(tree.rightsubtree)
end

No comments:

Post a Comment