# binary tree post order , inorder and pre order traversal

• 01-26-2014, 04:26 PM
Heanre
binary tree post order , inorder and pre order traversal
our teacher already taught us this lesson a semester ago but my teacher now explained it differently, i went to youtube and they also got a different explanation..

anyone who can either teach or post a link to the accurate explanation of binary trees in java?

thanks
• 01-26-2014, 04:34 PM
superhaNds
Re: binary tree post order , inorder and pre order traversal
what exactly do you want to know? about the traversals?

the algorithm is simple

inorder: traverse the left subtree, visit node, traverse the right subtree
preorder: visit node, traverse the left subtree, traverse the right subtree
postorder: traverse the left subtree, traverse the right subtree, visit node

the traversals are implemented recursively and are very simple to implement in fact.
• 01-26-2014, 04:41 PM
jim829
Re: binary tree post order , inorder and pre order traversal
The three traversals you mention has to do with when you visit the root (i.e. take some action) in relationship to traversing the child nodes. Keep in mind that the child nodes may also be roots in their own right, so the traversal can be recursive.

Pre-order: root, left, right.
In-order: left, root, right.
Post-order: left, right, root.

Here is a link for an explanation. Tree traversal - Wikipedia, the free encyclopedia.
Note: if you populate a tree of numbers (or any other sortable item) such that the left node is less than the root and the right node is greater than the root, an in-order traversal will result in the items being displayed in ascending-sorted order.

Regards,
Jim