forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Traversals of trees.cpp
161 lines (127 loc) · 3.05 KB
/
Traversals of trees.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<bits/stdc++.h>
#define ll long long
#define MAX 1000003
#define V vector<int>
#define pii pair<int,int>
#define VP vector< pii >
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define rep(i,a,b) for(int (i) = (a); (i) < (b); (i)++)
#define all(v) (v).begin(),(v).end()
#define S(x) scanf("%d",&(x))
#define S2(x,y) scanf("%d%d",&(x),&(y))
#define SL(x) scanf("%lld",&(x))
#define SL2(x) scanf("%lld%lld",&(x),&(y))
#define test() ll t;cin>>t;while(t--)
#define P(x) printf("%d\n",(x))
#define FF first
#define SS second
using namespace std;
struct node {
int data;
struct node *left;
struct node *right;
};
struct node *newNode(int data) {
struct node *treeNode = new node;
treeNode->data = data;
treeNode->left = NULL;
treeNode->right = NULL;
return treeNode;
}
int heightTree(struct node *root) {
if(root == NULL) return 0;
int lDepth = heightTree(root->left);
int rDepth = heightTree(root->right);
if(lDepth > rDepth) return lDepth+1;
else return rDepth+1;
}
void inOrder(struct node *root) {
if(root == NULL) return;
inOrder(root->left);
cout<< root->data;
inOrder(root->right);
}
void preOrder(struct node *root) {
if(root == NULL) return;
cout<<root->data;
preOrder(root->left);
preOrder(root->right);
}
void postOrder(struct node *root) {
if(root == NULL) return;
postOrder(root->left);
postOrder(root->right);
cout<<root->data;
}
void printGivenLevel(struct node *root,int i){
if(root == NULL) return;
if(i == 1) cout<< root->data;
printGivenLevel(root->left, i-1);
printGivenLevel(root->right, i-1);
}
void levelOrder(struct node *root) {
int h = heightTree(root);
for(int i=1;i<h;i++) {
printGivenLevel(root, i);
}
}
void levelOrderUsingQueue(struct node *root) {
if(root == NULL) return;
queue<struct node *> q;
q.push(root);
while(!q.empty()) {
struct node *temp = q.front();
cout<< temp->data<<" ";
if(temp->left!=NULL) q.push(temp->left);
if(temp->right!=NULL) q.push(temp->right);
q.pop();
}
}
int main(){
/*
1
/ \
2 3
/ \
4 5
/
6
*/
// struct node *root = newNode(1);
// root->left = newNode(2);
// root->right = newNode(3);
// root->left->left = newNode(4);
// root->left->right = newNode(6);
// root->right->right = newNode(5);
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->right->left = newNode(6);
cout<< "In-order: ";
inOrder(root);
cout<<endl;
cout<< "Pre-order: ";
preOrder(root);
cout<<endl;
cout<< "Post-order: ";
postOrder(root);
cout<<endl;
cout<< "Level-Order (BFS): In O(n^2) ";
levelOrder(root);
cout<<endl;
cout<< "Level-Order (BFS): Using queue in O(n) ";
levelOrder(root);
cout<<endl;
return 0;
}
/*
Code by :
Rishabh Patel
Integrated Post Graduation (IPG)
Indian Institute of Information Technology and Management, Gwalior (ABV-IIITM Gwalior)
In order to understand recursion, one must first understand recursion.
*/