xxxxxxxxxx
<> Preorder Root -> Left -> Right
<> Inorder Left -> Root -> Right
<> Postorder Left -> Right -> Root
xxxxxxxxxx
//*******this is a C program for implementation and searching in A BT*******
#include<stdlib.h>
#include <stdio.h>
struct BinaryTree{
int data;
struct BinaryTree*right;
struct BinaryTree*left;
};
struct BinaryTree*createnode(int val){
struct BinaryTree*root=(struct BinaryTree*)malloc(sizeof(struct BinaryTree));
root->data=val;
root->left=NULL;
root->right=NULL;
}
void inorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {inorder(root->left);
printf("%d->",root->data);
inorder(root->right);
}
}
void preorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else {
printf("%d->",root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(struct BinaryTree*root){
if(root==NULL){
return;
}
else{
postorder(root->left);
postorder(root->right);
printf("%d->",root->data);
}
}
int main()
{
printf("Lets grow the tree\n");
struct BinaryTree*root=createnode(1);
root->left=createnode(2);
root->right=createnode(3);
root->left->left=createnode(4);
root->left->right=createnode(5);
printf("tree has grown up\n");
printf("Inorder traversal ");
inorder(root);printf("NULL");
printf("\npreorder traversal ");
preorder(root);printf("NULL");
printf("\nPostorder traversal");
postorder(root);printf("NULL");
return 0 ;
}
xxxxxxxxxx
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
xxxxxxxxxx
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node * left, * right;
};
vector < int > preOrderTrav(node * curr) {
vector < int > preOrder;
if (curr == NULL)
return preOrder;
stack < node * > s;
s.push(curr);
while (!s.empty()) {
node * topNode = s.top();
preOrder.push_back(topNode -> data);
s.pop();
if (topNode -> right != NULL)
s.push(topNode -> right);
if (topNode -> left != NULL)
s.push(topNode -> left);
}
return preOrder;
}
struct node * newNode(int data) {
struct node * node = (struct node * ) malloc(sizeof(struct node));
node -> data = data;
node -> left = NULL;
node -> right = NULL;
return (node);
}
int main() {
struct node * root = newNode(1);
root -> left = newNode(2);
root -> right = newNode(3);
root -> left -> left = newNode(4);
root -> left -> right = newNode(5);
root -> left -> right -> left = newNode(8);
root -> right -> left = newNode(6);
root -> right -> right = newNode(7);
root -> right -> right -> left = newNode(9);
root -> right -> right -> right = newNode(10);
vector < int > preOrder;
preOrder = preOrderTrav(root);
cout << "The preOrder Traversal is : ";
for (int i = 0; i < preOrder.size(); i++) {
cout << preOrder[i] << " ";
}
return 0;
}
xxxxxxxxxx
1. Right child of 1 exists.
Push 3 to stack. Push 1 to stack. Move to left child.
Stack: 3, 1
2. Right child of 2 exists.
Push 5 to stack. Push 2 to stack. Move to left child.
Stack: 3, 1, 5, 2
3. Right child of 4 doesn't exist. '
Push 4 to stack. Move to left child.
Stack: 3, 1, 5, 2, 4
4. Current node is NULL.
Pop 4 from stack. Right child of 4 doesn't exist.
Print 4. Set current node to NULL.
Stack: 3, 1, 5, 2
5. Current node is NULL.
Pop 2 from stack. Since right child of 2 equals stack top element,
pop 5 from stack. Now push 2 to stack.
Move current node to right child of 2 i.e. 5
Stack: 3, 1, 2
6. Right child of 5 doesn't exist. Push 5 to stack. Move to left child.
Stack: 3, 1, 2, 5
7. Current node is NULL. Pop 5 from stack. Right child of 5 doesn't exist.
Print 5. Set current node to NULL.
Stack: 3, 1, 2
8. Current node is NULL. Pop 2 from stack.
Right child of 2 is not equal to stack top element.
Print 2. Set current node to NULL.
Stack: 3, 1
9. Current node is NULL. Pop 1 from stack.
Since right child of 1 equals stack top element, pop 3 from stack.
Now push 1 to stack. Move current node to right child of 1 i.e. 3
Stack: 1
10. Repeat the same as above steps and Print 6, 7 and 3.
Pop 1 and Print 1.
xxxxxxxxxx
Inorder Traversal using Stack:
As we already know, recursion can also be implemented using stack. Here also we can use a stack to perform inorder traversal of a Binary Tree. Below is the algorithm for traversing a binary tree using stack.
Create an empty stack (say S).
Initialize the current node as root.
Push the current node to S and set current = current->left until current is NULL
If current is NULL and the stack is not empty then:
Pop the top item from the stack.
Print the popped item and set current = popped_item->right
Go to step 3.
xxxxxxxxxx
Following is a simple stack based iterative process to print Preorder traversal.
Create an empty stack nodeStack and push root node to stack.
Do the following while nodeStack is not empty.
Pop an item from the stack and print it.
Push right child of a popped item to stack
Push left child of a popped item to stack
The right child is pushed before the left child to make sure that the left subtree is processed first.
xxxxxxxxxx
<> Preorder Root -> Left -> Right
<> Inorder Left -> Root -> Right
<> Postorder Left -> Right -> Root
xxxxxxxxxx
// Tree traversal in C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
// Preorder traversal
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
cout << node->data << "->";
preorderTraversal(node->left);
preorderTraversal(node->right);
}
// Postorder traversal
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
}
// Inorder traversal
void inorderTraversal(struct Node* node) {
if (node == NULL)
return;
inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
int main() {
struct Node* root = new Node(1);
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6);
cout << "Inorder traversal ";
inorderTraversal(root);
cout << "\nPreorder traversal ";
preorderTraversal(root);
cout << "\nPostorder traversal ";
postorderTraversal(root);