0% found this document useful (0 votes)
22 views18 pages

DAA - Tree Traversal

The document discusses basic search and traversing techniques for binary trees and graphs. It describes tree traversal algorithms like preorder, inorder, and postorder traversal. These traversal algorithms traverse the tree by visiting each node exactly once according to different sets of rules. For example, preorder traversal visits the root node first, then traverses the left subtree, and finally the right subtree. The document provides examples of applying these traversal algorithms to sample binary trees. It also discusses converting between different traversal orders and the time complexity of tree traversal techniques.
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)
22 views18 pages

DAA - Tree Traversal

The document discusses basic search and traversing techniques for binary trees and graphs. It describes tree traversal algorithms like preorder, inorder, and postorder traversal. These traversal algorithms traverse the tree by visiting each node exactly once according to different sets of rules. For example, preorder traversal visits the root node first, then traverses the left subtree, and finally the right subtree. The document provides examples of applying these traversal algorithms to sample binary trees. It also discusses converting between different traversal orders and the time complexity of tree traversal techniques.
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/ 18

DAA

Basic search and traversing techniques


Unit-4 Basic search and traversing techniques

● Techniques for Binary trees, Techniques for Graphs, connected


components and spanning trees, Bi-connected components, and depth
first search.
● Back Tracking: The General Method, Eight Queens problem, Sum of
subsets, Graph coloring, Hamiltonian cycle.
Tree
● In a binary tree, each node in
a tree can have utmost two
child nodes.
Tree Traversal
● Tree Traversal refers to the process of traversing or visiting each node in
a tree exactly once. Traversing is also known as iterating over the data
structure.

● Preorder traversal (Root → Left → Right)


● Inorder traversal (Left → Root → Right)
● Postorder traversal (Left → Right → Root)
Preorder Traversal
● Until all nodes of the tree are not visited
● Step 1: Visit the root node
● Step 2: Traverse the left subtree recursively, call Preorder (left subtree)
● Step 3: Traverse the right subtree recursively, call Preorder (right subtree)
Preorder Traversal

● A→B→D→E→C→F→G ● A, B, D, G, H, L, E, C, F, I, J, and K
Postorder Traversal
● Until all nodes of the tree are not visited
● Step 1: Traverse the left subtree recursively, call Postorder (left subtree)
● Step 2: Traverse the right subtree recursively, call Postorder (right sub tree)
● Step 3: Visit the root node.
Postorder Traversal

● D→E→B→F→G→C→A ● G, L, H, D, E, B, I, K, J, F, C, and A
Inorder Traversal
● Until all nodes of the tree are not visited
● Step 1 - Traverse the left subtree recursively, call Inorder (left sub tree)
● Step 2 - Visit the root node.
● Step 3 - Traverse the right subtree recursively, call Inorder (right sub tree)
Inorder Traversal

● D→B→E→A→F→C→G ● G, D, H, L, B, E, A, C, I, F, K, and J
Example

● Preorder traversal: 27 14 10 19 35 31 42 ● Preorder traversal: 1 2 4 5 3


● Inorder traversal: 10 14 19 27 31 35 42 ● Inorder traversal: 4 2 5 1 3
● Post order traversal: 10 19 14 31 42 35 27 ● Post order traversal: 4 5 2 3 1
Example

● Pre: A - B - D - I - J - F - C - G - K - H ● Pre: A, B, D, C, E, F, G, H, and I


● In: I - D - J - B - F - A - G - K - C - H ● In: B, D, A, E, H, G, I, F, and C
● Post: I - J - D - F - B - K - G - H - C - A ● Post: D, B, H, I, G, F, E, C, and A
Example
Complexity of Tree traversal techniques

● The time complexity of tree traversal techniques discussed above is O(n),


where 'n' is the size of binary tree.
Traversal Conversion (Binary Tree)
● Pre Order: A B D E C F G
● In Order: D B E A F C G
● Post: D E B F G C A

● Post Order: D B G E F C A
● In Order: D B A E G C F
● Pre Order: A B D C E G F
Traversal Conversion (BST)
● Pre Order: 12 8 6 2 7 9 10 16 15 19 17 20
● In Order: Sorted Data
● Post: 2 7 6 10 9 8 15 17 20 19 16 12

● Post Order: 10 9 23 22 27 25 15 50 95 60 40 29
● In Order: Sorted Data
● Pre Order: 29 15 9 10 25 22 23 27 40 60 50 95
References

● Ellis Horowitz, S Sahni, Fundamentals of Computer algorithms

● Thomas H Corman, Charles E Leiserson, Introduction to Algorithms

● Anany Levitin, Introduction to The Design and Analysis of Algorithms


From
Gurpreet Singh Chhabra

You might also like