0% found this document useful (0 votes)
23 views

Traversal of Tree

This document discusses tree data structures and tree traversal methods. It defines a tree as a non-linear data structure used to represent hierarchical relationships. There are three common ways to traverse a tree: pre-order, in-order, and post-order. Pre-order traversal visits the node, then traverses the left subtree, then the right subtree. In-order traversal traverses the left subtree, visits the node, then the right subtree. Post-order traversal traverses the left subtree, then the right subtree, and finally visits the node. Algorithms and examples are provided for each traversal method on a sample tree.

Uploaded by

Guru Iyer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Traversal of Tree

This document discusses tree data structures and tree traversal methods. It defines a tree as a non-linear data structure used to represent hierarchical relationships. There are three common ways to traverse a tree: pre-order, in-order, and post-order. Pre-order traversal visits the node, then traverses the left subtree, then the right subtree. In-order traversal traverses the left subtree, visits the node, then the right subtree. Post-order traversal traverses the left subtree, then the right subtree, and finally visits the node. Algorithms and examples are provided for each traversal method on a sample tree.

Uploaded by

Guru Iyer
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tree

1) Tree is a non linear data structure 2) It is mainly used to represent data containing hierarchical relationship between elements 3) EG. records , family tree , table of contents

Diagram of a tree

TRAVERSAL of tree
The most common operation performed on a binary tree is to traverse in a binary tree or visit each node of the binary tree. There are 3 ways available to traverse a tree Traversing can be done in three ways:-1) Pre order 2) In order 3) Post order

Consider the following diagram


Level 0 Level 1 Level 2

Level 3

Fig 2

If we traverse the tree, we will visit to nodes in given order: A B C D E F G H


This process is applied to each node in tree

Algorithm for Pre order Traversing: [D L R]


1) Visit the node [D] 2) Traverse the left subtree [L] 3) Traverse the right subtree [R]

Solution for above example:----- fig

ABC ABDEC ABDECF ABDECFGH

Algorithm for In order traversal: [L D R]


1) Traverse left subtree [L] 2) Visit the node [D] 3) Traverse the right subtree [R]

Solution for above example:----- fig

BAC DBEAC DBEACF DBEACGFH

Algorithm for post order traversal: [L R D]


1) Traverse left subtree [L] 2) Traverse the right subtree [R] 3) Visit the node [D]

Solution for above example:----- fig

BCA DEBCA DEBFCA DEBGHFCA

Presentation byGAURAV HARIHARAN (IF4E/B)

You might also like