Traversing in the binary tree in data structure
Tree traversal is the process of visiting each node in the tree exactly once.
Three traversal techniques:-
1.preorder traversal
2.inorder traversal
3.postorder traversal
Preorder traversal
To traverse a binary tree in preorder , following operations are carried out:-
1.visit the root.
2.traverse the left subtree of root.
3.traverse the right subtree of root.
Algorithm:-
Algorithm preorder(t)
{
If t!=0 then
{
Visit(t);
Preorder(t->child)
Preorder (t->rchild)
}
}
Preorder traversal- 7,1,0,3,2,5,4,6,9,8,10
Inorder traversal
To traverse a binary tree in inorder traversal , following operation are carried out:-
1.traverse the left most subtree.
2.visit the root.
3.trverse the right most subtree.
Algorithm
Algorithm inorder (t)
{
If t!=0 then
{
Inorder (t->lchild);
Visit(t);
Inorder(t->rchild);
}
}
Example:-
Inorder traversal- 0,1,2,3,4,5,6,7,8,9,10
Postorder traversal
To traverse a binary tree in postorder traversal , following operation are carried out.
1.traverse the left subtree of root.
2.traverse the right subtree of root.
3.visit the root .
Algorithm:-
Algorithm postorder (t)
{
If t!=0 then
{
Postorder(t->lchild);
Postorder (t->rchild);
Visit(t);
}
}
Example-
Postorder- 0,2,4,6,5,3,1,8,10,9,7
Thank you viewers
This is published by soumy sinha
1.preorder traversal
2.inorder traversal
3.postorder traversal
Preorder traversal
To traverse a binary tree in preorder , following operations are carried out:-
1.visit the root.
2.traverse the left subtree of root.
3.traverse the right subtree of root.
Algorithm:-
Algorithm preorder(t)
{
If t!=0 then
{
Visit(t);
Preorder(t->child)
Preorder (t->rchild)
}
}
Inorder traversal
To traverse a binary tree in inorder traversal , following operation are carried out:-
1.traverse the left most subtree.
2.visit the root.
3.trverse the right most subtree.
Algorithm
Algorithm inorder (t)
{
If t!=0 then
{
Inorder (t->lchild);
Visit(t);
Inorder(t->rchild);
}
}
Example:-
Inorder traversal- 0,1,2,3,4,5,6,7,8,9,10
Postorder traversal
To traverse a binary tree in postorder traversal , following operation are carried out.
1.traverse the left subtree of root.
2.traverse the right subtree of root.
3.visit the root .
Algorithm:-
Algorithm postorder (t)
{
If t!=0 then
{
Postorder(t->lchild);
Postorder (t->rchild);
Visit(t);
}
}
Example-
Postorder- 0,2,4,6,5,3,1,8,10,9,7
Thank you viewers
This is published by soumy sinha
Comments
Post a Comment