Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Program to print the DFS traversal step-wise using C++
In this tutorial, we will be discussing a program to print the steps of the traversal using Depth First Search in a given binary tree.
This would include every step that occurs in the depth-first search including the backtracking procedure as well.
During DFS, we will be traversing each node and simultaneously storing the parent node and the edge used. During the traversal, if the adjacent edge has been visited, then the exact node can be printed as a step in the depth-first search.
Example
#includeusing namespace std; const int N = 1000; vector adj[N]; //printing the steps in DFS traversal void dfs_steps(int u, int node, bool visited[], vector path_used, int parent, int it){ int c = 0; for (int i = 0; i > path_used; for (int i = 0; i Output
0 1 5 1 6 7 8 7 6 1 0 2 4 2 9 3 10
Advertisements
