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

Tree Traversal PPT

The document provides an introduction to tree data structures, explaining their hierarchical nature and key terminology such as root, parent, child, and leaf nodes. It details three types of tree traversal methods: pre-order, in-order, and post-order, including their definitions, algorithms, and examples. Additionally, it discusses how trees can be represented using arrays.

Uploaded by

areebahfiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Tree Traversal PPT

The document provides an introduction to tree data structures, explaining their hierarchical nature and key terminology such as root, parent, child, and leaf nodes. It details three types of tree traversal methods: pre-order, in-order, and post-order, including their definitions, algorithms, and examples. Additionally, it discusses how trees can be represented using arrays.

Uploaded by

areebahfiaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Tree Traversal

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

1.*Successor*: The next node after a given node.


2. *Predecessor*: The node before a given node.
3. *Ancestor*: A node higher up in the tree (parent, grandparent, etc.).
4. *Sibling*: Nodes that share the same parent.
5. *Terminal (Leaf) Node*: A node with no children.
6. *Non-terminal (Internal) Node*: A node with at least one child.
7. *Edge*: A link between two nodes.
8. *Branch*: A path from one node to another.
9. *Path*: A sequence of nodes connected by edges.
10. *Degree*: The number of children a node has.
11. *Level*: How far a node is from the root (starting from 0).
Tree Traversal
Pre-Order Tree Traversal
Pre-Oder Tree Traversal

• 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:

1. Start from the root node.


2. Base Case: If the node is NULL, return (end recursion for this
branch).
3. Visit the current node (process its value or perform the desired action).
4. Recursively traverse the left subtree by applying pre-order traversal
5. Recursively traverse the right subtree by applying pre-order traversal.
6. End.
Dry Run:
Steps for Pre-Order Tree Traversal

• In pre-order traversal, the sequence is:


Root → Left Subtree → Right Subtree
• Steps:
1. Start at A: Visit A.
2. Traverse the first child of A (B):
• Visit B
• Traverse the first child of B (E):
• Visit E
• Traverse the first child of E (J):
• Visit J [it has no child]
• Traverse the first child of E (K):
• Visit K
• Traverse the children of K.
• Visit N,O,P (in order)
• Traverse the second child of B (F):
• Visit F [it has no child]
3. Traverse the first child of A (C):
• Visit C [C has no child]
4. Traverse the third child of A (D):
• Visit D
• Traverse the first child of D (G):
• Visit G
• Traverse the children of G
• Visit L, M (in order) [they both have no child]
• Traverse the first child of D (H):
• Visit H [it has no child]
• Traverse the first child of D (I):
• Visit I [it has no child]
The Pre-Order is:

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

Example:- 2.Traverse left sub-tree of 2: 4


3.Traverse left sub-tree of 4: NULL
4 has no child ,visit 4
4. Go back to node 2 and visit it (2)
5. Now traverse the right sub-tree of 2 (5).
6. For node 5, go to its left sub-tree (8)
.7. Node 8 has no left sub-tree. So, visit 8
8. Go back to node 5 and visit it (5).
9. Node 5 has no right sub-tree. Go back to
the root (1) and visit 1.
10. Now go to the right sub-tree of 1 (3).
11. For node 3, go to its left sub-tree (6).
12. For node 6, go to its left sub-tree (9).
13. Node 9 has no left sub-tree. So, visit 9.
14. Go back to node 6 and visit it (6).
15. Node 6 has no right sub-tree. Go back to
node 3 and visit it (3).
16. Now go to the right sub-tree of 3 (7).
17. For node 7, go to its right sub-tree (10).
18. Node 10 has no left sub-tree. So, visit 10.
19. Go back to node 7 and visit it (7).
Final In order Traversal Output:4, 2, 8, 5, 1, 9,
6, 3, 7,
Algorithmn:-
Step 1: Start
Step 2: Process the left sub-tree
element.
Step 3: Process the root node.
Step 4: Process the right sub-tree
element.
Step 5: End
Code:-
void inOrder (Node* root)
{
if (root! = null)
{
inOrder (root->left);
cout << root->data << " ";
inOrder (root->right);
}
}
Post order tree traversal
Tree Traversal:

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):

Post-order traversal is one of the depth-first search (DFS)


methods used for traversing or visiting all nodes in a
binary tree. The traversal order is:
1- Left sub-tree
2- Right sub-tree
3- Root 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.

2- Base Case: If the node is NULL, return.

3- Traverse the left sub-tree by recursively applying the


post-order traversal.

4- Traverse the right sub-tree by recursively applying the


post-order traversal.

5- Visit the root node


❑ Steps for Post-Order Tree Traversal:
1. Traverse left sub-tree of A: B
2. Traverse left sub-tree of B: D
D has no children, visit D.
3. Traverse right sub-tree of B: E
E has no children, visit E.
4. Visit B.
5. Traverse right sub-tree of A: C
6. Traverse left sub-tree of C: None
7. Traverse right sub-tree of C: F
F has no children, visit F.
Output:
8. Visit C.
D, E, B, F, C, A
9. Visit A.
Code for Post-Order:

Tree Representation Using Array:

• The tree is represented as an array where:


• Root node is at index 0.
• Left child of a node at index i is at 2 * i + 1.
• Right child of a node at index i is at 2 * i + 2.
• -1 indicates no node at that position.

• Example Tree for {1, 2, 3, 4, 5, -1, 6}


How 2(i)*1 for left and 2(i)*2 for right sub-tree works?
Thank You

You might also like