You might, for instance, want to add all the values in the tree or find the largest one. For all these operations, you will need to visit each node of the tree.
Linear data structures like arrays, stacks , queues , and linked list have only one way to read the data. But a hierarchical data structure like a tree can be traversed in different ways.
Although this process is somewhat easy, it doesn't respect the hierarchy of the tree, only the depth of the nodes. The struct node pointed to by left and right might have other left and right children so we should think of them as sub-trees instead of sub-nodes. Remember that our goal is to visit each node, so we need to visit all the nodes in the subtree, visit the root node and visit all the nodes in the right subtree as well. We traverse the left subtree first.
We also need to remember to visit the root node and the right subtree when this tree is done. Since the node "5" doesn't have any subtrees, we print it directly. As an example of a tree to traverse, we will represent this book as a tree. The book is the root of the tree, and each chapter is a child of the root. Each section within a chapter is a child of the chapter, and each subsection is a child of its section, and so on. The diagram below shows a limited version of a book with only two chapters.
Note that the traversal algorithm works for trees with any number of children, but we will stick with binary trees for now. Representing a book as a tree. Suppose that you wanted to read this book from front to back. The preorder traversal gives you exactly that ordering. Starting at the root of the tree the Book node we will follow the preorder traversal instructions. Pseudocode for a recursive inorder traversal is a minor variation of the pseudocode for the recursive preorder traversal:.
Inorder traversal can also be performed using a non-recursive or iterative algorithm, in a fashion very similar to the iterative preorder traversal. In a postorder traversal of a binary tree, we traverse both subtrees of a node, then "visit" the node. Usually we traverse the node's left subtree first and then traverse the node's right subtree. Alternatively, we can perform a postorder traversal from right-to-left instead of left-to-right. Once again, this is done by traversing the node's right subtree before we traverse its left subtree.
Note that the left-to-right postorder traversal and the right-to-left postorder traversal are not mirror images of each other. However, we can now see that the left-to-right postorder traversal is a mirror image of the right-to-left preorder traversal, while the right-to-left postorder traversal is a mirror image of the left-to-right preorder traversal. Once again, pseudocode for a recursive inorder traversal is just a minor variation of the pseudocode for the recursive preorder and inorder traversals:.
Postorder traversal can also be performed using a non-recursive or iterative algorithm. Next Inorder Tree Traversal without Recursion. Recommended Articles. Check if given inorder and preorder traversals are valid for any Binary Tree without building the tree. Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative.
Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Articles. Most visited in Tree.
We use cookies to ensure you have the best browsing experience on our website.
0コメント