-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS_without_recursion.cpp
More file actions
101 lines (75 loc) · 2.76 KB
/
DFS_without_recursion.cpp
File metadata and controls
101 lines (75 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Trying to implement DFS without using recursion !
YES, its not practical, is very rigid and inefficient but for the sake of understanding and exploring its good !
*/
#include<iostream>
typedef struct Node{
int data;
struct Node* left;
struct Node* right;
struct Node* parent;
bool visited; //needed for DFS without recursion or need to use external data structure to track visited nodes.
Node() {}
Node(int val) :
data(val),
left(nullptr),
right(nullptr),
parent(nullptr),
visited(false) {}
}node;
void traverse(node* root){
//trying a DFS traversal
node* current = root;
while(current != nullptr){
//visit a node if it is not visited
if(!current->visited){
std::cout << current->data << std::endl;
current->visited = true; //set visited to true
}
//if there exists a left node and its not visited
if(current->left && !current->left->visited){
current = current->left; //move current to that node (move down by 1 level)
}
//if there is no left node left we gotta visit the right ones, branch down right by one node.
else if(current->right && !current->right->visited){
current = current->right;
}
//backtrack ! If one of the levels are all visited we backtrack my moving current up by one level.
else{
current = current->parent;
}
//current becomes null pointer when it backtracks to the root node and enters the else block because all the child nodes are already visited.
//current node's parent is set to be nullptr by node structure therefore it becomes nullptr.
}
}
int main(){
node* root = new node(10);
root->left = new node(8);
root->right = new node(15);
//setting the parent pointers
root->left->parent = root;
root->right->parent = root;
//done this to avoid `->` chaining for better readability at the cost of 2 extra pointers in memory
node* leftSubTree = root->left;
node* rightSubTree = root->right;
//left sub tree
leftSubTree->left = new node(3);
leftSubTree->right = new node(5);
leftSubTree->left->parent = leftSubTree; //setting parent for left subtree
leftSubTree->right->parent = leftSubTree;
//right sub tree
rightSubTree->left = new node(12);
rightSubTree->right = new node(14);
rightSubTree->left->parent = rightSubTree; //setting parent for right subtree
rightSubTree->right->parent = rightSubTree;
traverse(root);
//free memory
delete leftSubTree->left;
delete leftSubTree->right;
delete rightSubTree->left;
delete rightSubTree->right;
delete leftSubTree;
delete rightSubTree;
delete root;
return 0;
}