0% found this document useful (0 votes)
26 views13 pages

DS Group 2 New

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

DS Group 2 New

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

EASWARI ENGINEERING

(Autonomous)
COLLEGE
Ramapuram,Chennai-600 089
21st October 2024
GROUP PRESENTATION

Subject Name : DATA STRUCTURES AND ALGORITHMS


Subject Code: 231ITC301T
Second Year III Semester
2024-2025

TREE TRAVERSAL ALGORITHMS


PRESENTED BY
>MOUNIKA SHREE N G -310623205092
>KIRUTHIKA J -
310623205073
>MOHAMED YUSUFF -310623205089
> NAMITHA B -
310623205098
C Introduction
O Inorder Traversal
N
Preorder Traversal
T
Postorder Traversal
E
N Level order Traversal
T Application
S Conclusion
Introduction to Tree Traversal
Algorithms
Definition:
Tree traversal is the process of visiting each node in a tree data structure in a
systematic manner.
Importance:

 Efficient Data Access: Enables quick search, insertion, and deletion


operations.
 Hierarchical Representation: Essential for processing data structures like
file systems and organizational charts.
 Foundation for Algorithms: Supports various algorithms for balancing trees
and finding paths.
Overview of Traversals

Preorder

Depth-First Inorder

Traversal Postorder

Breadth-First Level Order


Inorder Traversal
Algorithm:
Traverse the left subtree,visit the
root,then traverse the right
subtree.
Use Cases:
Extracting sorted data from a
Binary Search Tree(BST).

Function
void inorderTraversal(struct
Node* root) {
if (root)
{ inorderTraversal(root-
>left);
// Traverse left subtree
printf("%d ", root->data);
// Visit node
inorderTraversal(root->right); //
Traverse right subtree
}}
Preorder Traversal
Algorithm:
Visit the root first , then traverse the
left and right subtrees.
Use Cases:
Creating a copy of the tree , prefix
expression evaluation.

Function:
void preorderTraversal(struct Node*
root) {
if (root) {
printf("%d ", root->data);
// Visit node
preorderTraversal(root->left);
// Traverse left subtree
preorderTraversal(root->right);
// Traverse right subtree
}}
Postorder Traversal
Algorithm:
Traverse the left subtree , then the
right subtree , and visit the root last.
Use Cases:
Deleting a tree , postfix expression
evaluation.

Function:

void postorderTraversal(struct Node*


root) {
if (root)
{ postorderTraversal(root->left);
// Traverse left subtree
postorderTraversal(root->right);
// Traverse right subtree
printf("%d ", root->data);
// Visit node
}}
Level Order Traversal
Description:
Visits nodes level by level from top
to bottom , left to right.
Algorithm:
Use a queue to keep track of nodes to
visit.
Use Cases:
Finding the shortest path in
unweighted trees , level-wise
printing.
Time and Space Complexity:

• O(n) for all traversal algorithms ,


where n is the number of nodes.
Time
Complexity

• Depth-First : O(h) for


Space recursion , where h is the

Complexit height of the tree.


• Breadth-First : O(w), where

y w is the maximum width of


the tree.
Real Life Applications:
1. Inorder Traversal:

Auto-complete and Search Suggestions: When you type into a search engine
(like Google) or a text field, the system often uses a binary search tree (BST)
or a similar structure to provide sorted suggestions. Inorder traversal is used to
fetch these suggestions in lexicographical order.

Music and Media Libraries: In music or media player applications, inorder


traversal can be used to list files (songs, videos) alphabetically or by date
added, allowing for quick access to ordered data.

2. Preorder Traversal

Web Crawlers : Search engines (e.g., Google) use web crawlers to traverse
websites. They often use preorder traversal to visit a website and its linked
pages, storing information before moving to the next. The "root" is the current
page, and "left/right" refers to links or other subpages.

Organizational Hierarchies: In Human Resources (HR) or organizational


software, preorder traversal is used to display organizational charts. It lists
supervisors before their subordinates, reflecting the structure of companies and
teams.
3. Postorder Traversal :

Backup and Restoration: Postorder traversal is used in applications that back


up file systems or databases, where you need to back up all subdirectories or
dependencies before backing up the root directory or main system file.

Dependency Resolution : In software package managers (e.g., npm, Maven),


postorder traversal helps resolve dependencies. The algorithm ensures that
dependencies are installed first before the main software, which is common in
installing software packages or libraries.

4. Level Order Traversal (Breadth-First Search):

Customer Service Systems (IVR): In Interactive Voice Response (IVR)


systems, such as the automated call menus when you call customer support,
level order traversal is used to navigate the hierarchy of menu options. It
ensures you’re presented with the first level options before being given
submenus.

Social Networks (Friends Suggestion): Social media platforms like Facebook


or LinkedIn use level order traversal (BFS) to suggest connections or friends.
They suggest people who are connected within 1 degree of separation before
suggesting those who are farther (2 degrees, 3 degrees, etc.)
Conclusion

Tree traversal algorithms play a crucial role in various real-world applications, from
data structures and file systems to AI decision-making and network routing. Whether
it's retrieving data in a sorted order, evaluating expressions, or finding optimal paths,
these algorithms provide efficient ways to navigate and process hierarchical data
structures, making them fundamental to many technologies we rely on daily.
THANK
YOU

You might also like