Tree Traversal PPT
Tree Traversal PPT
By
Group # 03
Tree Introduction
• So far we have discussed mainly linear data structures,strings arrays,
• lists, stacks and queues
• Now we will discuss unknown linear data structure called tree
• Trees are mainly used to represent data hierarchical relationship
between elements for example records family tree and table of
content
• Consider a parent child relationship
Tree
• A tree is an abstract model of a hierarchical
structure that consists of
• nodes with a parent-child relationship.
• Tree is a sequence of nodes
• There is a starting node known as a root
node
• Every node other than the root has a
parent node.
• Nodes may have any number of children
some key terms:
• Must read
• RootNode at the top of the tree is called root
• Parent - An node except root node has one edge upward to a node called parent
• ChildNode below a given node connected by its edge downward is called its child
node
• Sibling-Child of same node are called siblings
• Leaf - Node which does not have any child node is called leaf node
• Sub tree-Sub tree represents descendants of a node.
• Levels-Level of a node represents the generation of a node. If root node is at level
0, then its next child node is at level 1, its grandchild is at level 2 and so on
• keys-Key represents a value of a node based on which a search operation is to be
carried out for a node
Basic Terminology of tree
note:
Basic Terminology of tree
• Definition:
Pre-order tree traversal is a depth-first traversal method for
trees where nodes are visited.
• In pre-order the tree is traversed in the following order:
1.Root
2.Left Subtree
3.Right Subtree
Where it is used?
• This traversal method ensures that the root
of each subtree is visited before its children,
making it particularly useful for operations
where the root node needs to be processed
before its subtrees.
Example
File Systems
• Directory Traversal:
File systems represented as trees use pre-order traversal to visit a
directory first before its subdirectories and files. This is useful for tasks like
creating directory listings or performing actions on folders before their
contents.
Algorithm:
A → B→ E → J → K → N → O → P → F → C → D → G → L → M → H → I
OUTPUT
In Order Traversal
In order Traversal:-
The in order traversal of a binary tree is a recursive
process. The in order traversal of a tree is:
•Traverse in in order the left sub-tree.
•Visit the root of the tree.
•Traverse in in order the right sub-tree.
1.Traverse left sub-tree of 1: 2
Definition:
Tree traversal refers to the process of visiting all the nodes in a tree
data structure in a specific order.
Post-Order Tree Traversal
Post-order traversal (left-right-node):
In this method, the root node is visited after visiting all the nodes
of left and right sub-trees. The left and right sub-trees are
processed before the root node.
Algorithm:
1- Start at the root node.