-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSA_Binary_Tree.cpp
217 lines (186 loc) · 5.74 KB
/
DSA_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// author: jaydattpatel
/*
CREATE BINARY TREE
(1,A)
_______________|________________
| |
(2,B) (3,C)
________|________ ________|________
| | | |
(4,D) (5,E) (6,F) (7,G)
___|___ ___|___ ___|___ ___|___
| | | | | | | |
(8,H) (9,I) (10,J) (11,K) (12,L) (13,M) (14,N) (15,O)
Inorder is: [4, D] [2, B] [5, E] [1, A] [6, F] [3, C] [7, G]
Preorder is: [1, A] [2, B] [4, D] [5, E] [3, C] [6, F] [7, G]
Postorder is: [4, D] [5, E] [2, B] [6, F] [7, G] [3, C] [1, A]
BFS levelorder is: [1, A] [2, B] [3, C] [4, D] [5, E] [6, F] [7, G]
Height of Tree : 3
DFS levelorder is: [4, D] [5, E] [6, F] [7, G] [2, B] [3, C] [1, A]
Print value of level:
Level-1: [1, A]
Level-2: [2, B] [3, C]
Level-3: [4, D] [5, E] [6, F] [7, G]
*/
#include<iostream>
#include<deque>
using namespace std;
class BTree
{
public:
BTree *left;
BTree *right;
int data;
string ss;
BTree() //constructor
{
left = NULL;
right = NULL;
cout<<"\nNew constructed\n";
}
~BTree() //deconstructor
{
cout<<"\nFree: ["<<this->data<<", "<<this->ss<<"] ";
delete(this->left);
delete(this->right);
}
BTree *create(BTree *root);
void postorder(BTree *root);
void inorder(BTree *root);
void preorder(BTree *root);
void BFS_levelOrder(BTree *root);
int height(BTree* root);
void DFS_depthOrder(BTree *root);
void printCurrentLevel(BTree* root, int level);
};
BTree* BTree::create(BTree *root)
{
if(root==NULL)
{
root = new BTree;
root->right=NULL;
root->left=NULL;
cout<<"Enter the value :";
cin>>root->data;
cout<<"Enter the string:";
cin>>root->ss;
}
char c;
cout<<"Left node of ["<<root->data<<", "<<root->ss<<"] (y/n) ? : ";
cin>>c;
if(c=='y'||c=='Y')
root->left=create(root->left);
cout<<"Right node of ["<<root->data<<", "<<root->ss<<"] (y/n) ? : ";
cin>>c;
if(c=='y'||c=='Y')
root->right=create(root->right);
cout<<"\n\n";
return(root);
}
void BTree::inorder(BTree *root)
{
if(root==NULL)
return;
inorder(root->left);
cout<<" ["<<root->data<<", "<<root->ss<<"] ";
inorder(root->right);
}
void BTree::preorder(BTree *root)
{
if(root==NULL)
return;
cout<<" ["<<root->data<<", "<<root->ss<<"] ";
preorder(root->left);
preorder(root->right);
}
void BTree::postorder(BTree *root)
{
if(root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<" ["<<root->data<<", "<<root->ss<<"] ";
}
//BFS : Breadth First Search(Level order search) in Binary Tree
void BTree::BFS_levelOrder(BTree *root)
{
deque< BTree* > deq;
deq.push_back(root);
while(!deq.empty())
{
cout<<" ["<<deq[0]->data<<", "<<deq[0]->ss<<"] ";
if(deq[0]->left != NULL)
deq.push_back(deq[0]->left);
if(deq[0]->right != NULL)
deq.push_back(deq[0]->right);
deq.pop_front();
}
}
int BTree::height(BTree* root)
{
if (root == NULL)
return 0;
else
{
// compute the height of each subtree
int left_height = height(root->left);
int right_height = height(root->right);
// use the larger one
if (left_height > right_height)
return (left_height + 1);
else
return (right_height + 1);
}
}
//DFS : Depth First Search in Binary Tree
void BTree::DFS_depthOrder(BTree *root)
{
int hh = root->height(root);
for(int i=hh ; i>=0; i--)
root->printCurrentLevel(root,i);
}
void BTree::printCurrentLevel(BTree* root, int level)
{
if (root == NULL)
return;
if (level == 1)
cout<<" ["<<root->data<<", "<<root->ss<<"] ";
else if (level > 1)
{
printCurrentLevel(root->left, level - 1);
printCurrentLevel(root->right, level - 1);
}
}
int main()
{
BTree *root = NULL;
char c;
while(1)
{
root = root->create(root);
cout<<"Do you want to continue(y/n):";
cin>>c;
if(c=='n'||c=='N')
break;
}
cout<<"\nInorder is:";
root->inorder(root);
cout<<"\nPreorder is:";
root->preorder(root);
cout<<"\nPostorder is:";
root->postorder(root);
cout<<"\nBFS levelorder is:";
root->BFS_levelOrder(root);
int hh = root->height(root);
cout<<"\nHeight of Tree : "<<hh;
cout<<"\nDFS levelorder is:";
root->DFS_depthOrder(root);
cout<<"\nPrint value of level:";
for(int i=1; i<=hh; i++)
{
cout<<"\n\tLevel-"<<i<<":";
root->printCurrentLevel(root,i);
}
delete(root);
return 0;
}