0% found this document useful (0 votes)
14 views20 pages

DS-Tree Traversal BSC CS III Sem

Data structure

Uploaded by

ajithkumar07252
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)
14 views20 pages

DS-Tree Traversal BSC CS III Sem

Data structure

Uploaded by

ajithkumar07252
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/ 20

Tree Traversal

Contents
• Tree Traversal
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
• Applications of Binary Trees
Tree Traversal
• Tree Traversal refers to the process of visiting
each node in a tree data structure exactly
once.
Depth First Traversal
Following three traversal techniques fall under
Depth First Traversal-
• Preorder Traversal
• Inorder Traversal
• Postorder Traversal
1. Preorder Traversal
Algorithm-
• Visit the root
• Traverse the left sub tree i.e. call Preorder (left
sub tree)
• Traverse the right sub tree i.e. call Preorder
(right sub tree)
• Root → Left → Right
Example
• Applications-
• Preorder traversal is used to get prefix
expression of an expression tree.
• Preorder traversal is used to create a copy of
the tree.
2. Inorder Traversal
• Algorithm-
• Traverse the left sub tree i.e. call Inorder (left
sub tree)
• Visit the root
• Traverse the right sub tree i.e. call Inorder
(right sub tree)
• Left → Root → Right
Example-
• Application-
• Inorder traversal is used to get infix expression
of an expression tree.
3. Postorder Traversal-
• Algorithm-
• Traverse the left sub tree i.e. call Postorder (left
sub tree)
• Traverse the right sub tree i.e. call Postorder
(right sub tree)
• Visit the root

• Left → Right → Root


Example-
• Applications-
• Postorder traversal is used to get postfix
expression of an expression tree.
• Postorder traversal is used to delete the tree.
• This is because it deletes the children first and
then it deletes the parent.
Breadth First Traversal-

• Breadth First Traversal of a tree prints all the


nodes of a tree level by level.
• Breadth First Traversal is also called as Level
Order Traversal.
Example
• Application-
• Level order traversal is used to print the data
in the same order as stored in the array
representation of a complete binary tree.
Binary Tree Properties-
• Property-01:
• Minimum number of nodes in a binary tree
of height H= H + 1
• Property-02:
• Maximum number of nodes in a binary tree
of height H= 2H+1 – 1
• Property-03:
• Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1
• Property-04:
• Maximum number of nodes at any level ‘L’ in
a binary tree= 2L
Applications of Binary Tree
• Binary Tree is used to as the basic data structure in Microsoft Excel
and spreadsheets in usual.
• Binary Tree is used to implement indexing of Segmented Database.
• Splay Tree (Binary Tree variant) is used in implemented efficient
cache is hardware and software systems.
• Binary Space Partition Trees are used in Computer Graphics, Back
face Culling, Collision detection, Ray Tracing and algorithms in
rendering game graphics.
• Syntax Tree (Binary Tree with nodes as operations) are used to
compute arithmetic expressions in compilers like GCC, AOCL and
others.
• Binary Heap (Binary Tree variant of Heap) is used to implement
Priority Queue efficiently which in turn is used in Heap Sort
Algorithm.
THANKYOU

You might also like