-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS_of_Binary_Tree.cpp
122 lines (106 loc) · 2.88 KB
/
DFS_of_Binary_Tree.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <bits/stdc++.h>
using namespace std;
struct Node //structure of the binary tree
{
int info; //data part
struct Node *left, *right; //left and right node which will contain address of left and right subtree
};
//function to create tree
struct Node* create()
{
int data;
Node *tree;
tree = new Node;
cout << "\nEnter data to be inserted or type -1 : ";
cin>>data;
//condition for termination
if(data == -1)
return 0;
tree->info = data;
cout << "Enter left child of " << data;
tree->left = create();
cout << "Enter right child of " << data;
tree->right=create();
return tree;
};
/*
In the DFS traversal, we start with a node and traverse its adjacent
child node and continue this process until we are done with all the nodes.
As trees do not contain cycles,we will surely traverse all the nodes.
We have tree ways of doing it:
1.Pre-order way: First print the data, goto left child and then goto right child.
1.In-order way: First goto left child, print the data and then goto right child.
1.Post-order way: First goto left child, then goto right child and then print the data.
*/
void DFS_PreOrder(Node* root)
{
if(root == NULL)
return;
cout << root->info << " ";
DFS_PreOrder(root->left);
DFS_PreOrder(root->right);
}
void DFS_PostOrder(Node* root)
{
if(root == NULL)
return;
DFS_PostOrder(root->left);
DFS_PostOrder(root->right);
cout << root->info << " ";
}
void DFS_InOrder(Node* root)
{
if(root == NULL)
return;
DFS_InOrder(root->left);
cout << root->info << " ";
DFS_InOrder(root->right);
}
//Driver Program
int main()
{
Node *root = NULL;
root = create();
cout << "\nDFS Traversal(Pre-Order): ";
DFS_PreOrder(root);
cout << "\nDFS Traversal(Post-Order): ";
DFS_PostOrder(root);
cout << "\nDFS Traversal(In-Order): ";
DFS_InOrder(root);
return 0;
}
/*
Time Complexity : O(n)
Space Complexity: O(1)
Sample Input/Output:
Enter data to be inserted or type -1 : 10
Enter left child of 10
Enter data to be inserted or type -1 : 20
Enter left child of 20
Enter data to be inserted or type -1 : 30
Enter left child of 30
Enter data to be inserted or type -1 : -1
Enter right child of 30
Enter data to be inserted or type -1 : -1
Enter right child of 20
Enter data to be inserted or type -1 : -1
Enter right child of 10
Enter data to be inserted or type -1 : 40
Enter left child of 40
Enter data to be inserted or type -1 : -1
Enter right child of 40
Enter data to be inserted or type -1 : 50
Enter left child of 50
Enter data to be inserted or type -1 : -1
Enter right child of 50
Enter data to be inserted or type -1 : -1
DFS Traversal(Pre-Order): 10 20 30 40 50
DFS Traversal(Post-Order): 30 20 50 40 10
DFS Traversal(In-Order): 30 20 10 40 50
Tree Formed :
10
/\
20 40
/ \
30 50
*/