100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Class notes

Design analysis and algorithm

Rating
-
Sold
-
Pages
24
Uploaded on
04-03-2024
Written in
2023/2024

well defined study notes of Design analysis and algorithm based on Richard nepolitan and kumarss Foundation of Algorithm using C++ Pseudo codes Third edition

Institution
Course










Whoops! We can’t load your doc right now. Try again or contact support.

Connected book

Written for

Institution
Course

Document information

Uploaded on
March 4, 2024
Number of pages
24
Written in
2023/2024
Type
Class notes
Professor(s)
Anany levtin
Contains
Design analysis and algorithms

Subjects

Content preview

MODULE 5

Basic Search and Traversal Techniques

Definition 1

: Traversal of a binary tree involves examining every node in the tree.

Definition 2

Search involves visiting nodes in a graph in a systematic manner, and may or may

not result into a visit to all nodes.

•Different nodes of a graph may be visited, possibly more than once, during traversal or

search

•If search results into a visit to all the vertices, it is called traversal

Binary tree traversal: Preorder, Inorder, and Postorder

in order to illustrate few of the binary tree traversals, let us consider the below binary tree:




Preorder traversal

: To traverse a binary tree in Preorder, following operations are carried out

(i) Visit the root, (ii) Traverse the left subtree, and (iii) Traverse the right subtree.

Therefore, the Preorder traversal of the above tree will outputs:

7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10




1

, 1. Algorithm
2. Algorithm preorder(t)
3. //t is a binary tree. Each node of t has
4. //three fields: lchild, data, and rchild.
5. {if t!=0 then
6. {Vist(t);
7. preorder(t->lchild);
8. preorder(t->rchild);
9. }}


Inorder traversal

: To traverse a binary tree in Inorder, following operations are carriedout

(i) Traverse the left most subtree starting at the left external node,

(ii) Visit the root, and

(iii) Traverse the right subtree starting at the left external node.

Therefore, the Inorder traversal of the above tree will outputs:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

1. Algorithm:
2. Algorithm inorder(t)
3. //t is a binary tree. Each node of tHas
4. //three fields: lchild, data, and rchild.
5. {
6. if t!=0 then
7. {postorder(t->lchild);
8. Visit(t);
9. postorder(t->rchild);
10. }}




Postorder traversal

: To traverse a binary tree in Postorder, following operations arecarriedout

(i) Traverse all the left external nodes starting with the left most subtree which is then
followed

by bubbleup all the internal nodes,

(ii) Traverse the right subtree starting at the left external node which is then followed by
bubble

2

, up allthe internal nodes, and

(iii) Visit the root.

Therefore, the Postorder traversal of the above tree will outputs:

0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7

1. Algorithm:
2. Algorithm postorder(t)
3. //t is a binary tree. Each node of t has
4. //three fields: lchild, data,
5. and rchild.
6. {if t!=0 then
7. {postorder(t->lchild);
8. postorder(t->rchild);
9. Visit(t);
10. }
11. }


Graph Traversal

Graph traversal means visiting every vertex and edge exactly once in a well-defined order.
While using certain graph algorithms, you must ensure that each vertex of the graph is visited
exactly once. The order in which the vertices are visited are important and may depend upon
the algorithm or question that you are solving.

During a traversal, it is important that you track which vertices have been visited. The most
common way of tracking vertices is to mark them.

1.DepthFirst Traversal

• Depth first search (DFS) is an algorithm for traversing or searching a tree, tree
structure, or graph.
• One starts at the root (selecting some node as the root in the graph case) and explores
as far as possible along each branch before backtracking.
• Formally, DFS is an uninformed search that progresses by expanding the first child
node of the search tree that appears and thus going deeper and deeper until a goal
node is found, or until it hits a node that has no children.
• Then the search backtracks, returning to the most recent node it hasn't finished
exploring. In a non-recursive implementation, all freshly expanded nodes are
added to a stack for exploration.





3
$17.99
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
adithyanms

Get to know the seller

Seller avatar
adithyanms M G university
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
1 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions