# Trees in C++ Final part

Submitted by moazkhan on Tuesday, August 26, 2014 - 04:19.

# Trees in C++ Final part

In this tutorial, you will learn 1. Different types of traversal of BST 2. Concept and implementation of inorder traversal 3. Concept and implementation of preorder traversal 4. Concept and implementation of postorder traversal**What are different types of traversals of BST?**In trees, there are three types of traversals 1. In-order traversal 2. Post-order traversal 3. Pre-order traversal

**What is in-order traversal?**In inorder traversal, first the left child is accessed then root and then right child at last. Note, that when we access left child, it may act as a root to another tree and we before accessing it, we must access its left child first. To explain the traversal further, I will use a picture to help me in explaining. The inorder traversal of this tree would be 0,1,2,3,4,5,6,7,8,9,10. The most important thing about inorder traversal is that if we call inorder traversal on a Binary Search Tree then the result would be in ascending order. The C++ implementation is given below.

- void Inorder(node * root)
- {
- if (root!=NULL) {
- Inorder(root->left);
- cout<<root->info;
- Inorder(root->right);
- }
- }

**What is preorder traversal?**In preorder traversal, we first visit the root and then the left sub tree and at last the right sub tree. For the tree given in part of in-order, the result of pre order traversal would be 7,1,0,3,2,5,4,6,9,8,10 The code of preorder is given below

- void Preorder(node * root)
- {
- if (root!=NULL) {
- cout<<root->info;
- Preorder(root->left);
- Preorder(root->right); }
- }

**What is postorder traversal?**In post order traversal, we first access the left sub tree and then the right sub tree and then the root. While accessing any of the sub tree, the same steps are carried that first its left sub tree is accessed then the right one and then the root.

- void Postorder(node * root)
- {
- if (root!=NULL) {
- Postorder(root->left);
- Postorder(root->right);
- cout<<root->info;}
- }

## Book traversal links for Trees in C++ Final part

## Add new comment

- 135 views