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

DSA L9

The document provides an overview of tree traversal techniques, including pre-order, in-order, post-order, and level-order methods for navigating tree data structures. It highlights the importance of these techniques in various applications such as expression evaluation, directory listings, and tree copying. Additionally, it includes algorithms and examples for each traversal type, emphasizing their distinct orders and use cases.

Uploaded by

seunadepoju64
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)
11 views

DSA L9

The document provides an overview of tree traversal techniques, including pre-order, in-order, post-order, and level-order methods for navigating tree data structures. It highlights the importance of these techniques in various applications such as expression evaluation, directory listings, and tree copying. Additionally, it includes algorithms and examples for each traversal type, emphasizing their distinct orders and use cases.

Uploaded by

seunadepoju64
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/ 30

1

Data Structures & Algorithms


Tree Data Structure

Tree Traversal

DR . MOHAMMED AL-HUBAISHI
Tree Traversal Understanding Different
Ways to Navigate Tree
Techniques Data Structures
3
Introduction

Definition: Tree traversal is the process of visiting (or updating/ printing)


each node in a tree data structure exactly once.

Importance: Tree traversal is a fundamental operation in working with tree


structures and is essential for various algorithms and applications.
Introduction Traversal

Tree Traversal: Process of visiting all nodes in a specific


order and help us to print or output the Tree in different ways
(techniques).
• Main traversal techniques:
1. Pre-order
2. In-order
3. Post-order
4. Level-order
Pre-order Traversal

Algorithm: Example Tree:


1. Visit root node A
2. Traverse left subtree /\
3. Traverse right subtree B C
/\

Implementation: D E

preorder(node):
Pre-order Output:
if node:
A→B→D→E→C
print(node.data)
preorder(node.left)
preorder(node.right)
In-order Traversal

Algorithm: Example Tree:


1. Traverse left subtree A
2. Visit root node /\
3. Traverse right subtree B C
/\
Implementation: D E
inorder(node):
if node: In-order Output:
inorder(node.left) D→B→E→A→C
print(node.data)
inorder(node.right)
Post-order and Level-order Traversal

Post-order Algorithm: Example Tree:


1. Traverse left subtree A
2. Traverse right subtree /\
3. Visit root node B C
/\
Post-order Output: D E
D→E→B→C→A
Comparison and Use Cases

Traversal Type Common Use Cases


• Pre-order • Copying/Duplicating trees
• In-order • Expression parsing in compilers
• Post-order • Binary Search Tree validation
9

Binary Tree Traversal Techniques

► Three recursive techniques for binary tree traversal


► In each technique, the left subtree is traversed recursively, the right
subtree is traversed recursively, and the root is visited
► What distinguishes the techniques from one another is the order of
those 3 tasks
10
Preoder, Inorder, Postorder
Preorder Traversal:
1. Visit the root
2. Traverse left subtree
► In Preorder, the root is visited before
(pre) the subtrees traversals 3. Traverse right subtree
► In Inorder, the root is visited Inorder Traversal:
in-between left and right subtree
traversal
1. Traverse left subtree
2. Visit the root
► In Preorder, the root is visited after
(pre) the subtrees traversals
3. Traverse right subtree
Postorder Traversal:
1. Traverse left subtree
2. Traverse right subtree
3. Visit the root
11
Illustrations for Traversals

► Assume: visiting a node


is printing its label 1
► Preorder: root L R
3 7
1 3 5 4 6 7 8 9 10 11 12
► Inorder: L root R 5 8 9
4 5 6 3 1 8 7 9 11 10 12
10
► Postorder: L R root 4 6
4 6 5 3 8 11 12 10 9 7 1
11 12
12
Tree Traversal Methods

Types of Traversals: Applications:


• Pre-order (Root-Left-Right) • Expression evaluation
• In-order (Left-Root-Right) • Directory listings
• Post-order (Left-Right-Root) • Copy operations
• Tree comparisons
13
Applications of Pre-order Traversal

- Expression evaluation
Explanation: Pre-order traversal is used to evaluate prefix
expressions (Polish notation). In this notation, operators appear
before their operands.
Example : Expression: + * 3 4 5 (Prefix notation)

- Pre-order traversal: `+ * 3 4 5`
- Evaluation: 3 * 4 = 12, then 12 + 5 = 17.
Applications of In-order Traversal

- Directory listings
Explanation: In-order traversal is used to list files and directories in a
hierarchical structure, such as a file system. It ensures that directories and files
are visited in a sorted or logical order.
Example: Directory Tree:
- In-order traversal: `File1 -> Folder1 -> Root -> Folder2 -> File2`
- Output: Files and folders are listed in a logical order.
Applications of Post-order Traversal

- Copy operations
Explanation: Post-order traversal is used to copy a tree structure. It
ensures that all child nodes are processed before the parent node,
which is essential for duplicating hierarchical data.
- Post-order traversal: `D -> E -> B -> C -> A`
- Copy Process: Start with the leaf nodes (`D` and `E`), then move
up to their parent (`B`), and so on, until the entire tree is copied.
16
C++ code

https://onlinegdb.com/zP0H_-IkM
17
test the code

https://onlinegdb.com/zP0H_-IkM
18
Illustrations for Traversals (Contd.)

► Assume: visiting a node


15
is printing its data
8 20
► Preorder: 15 8 2 6 3 7
11 10 12 14 20 27 22 30 2 11 27
► Inorder: 2 3 6 7 8 10 11
12 14 15 20 22 27 30 6 10 12 22 30
► Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15 3 7 14
19
Applications for traversals and BST

Expression Trees: Tree traversals are commonly used in evaluating


expressions represented by expression trees.

Binary Search Trees: Traversals are crucial for searching, inserting, and
deleting nodes in binary search trees.

Syntax Tree Construction: Tree traversal is used in parsing and


constructing syntax trees for programming languages.
20
Pre Order

Pre-order traversal, the nodes would be visited in this order:


X→A→B→C→+→X→D→E→F
21
In Order

the In-order traversal would be:


B→A→C→X→D→X→E→+→F
22
Post Order

the Post-order traversal would be:


B→C→A→D→E→X→F→+→X
23
preOrder
24
inOrder
25
postOrder
examples 26
27
example
28
Tree : Height

root
29
Book References
30

You might also like