-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleteGenericTree.cpp
More file actions
234 lines (218 loc) · 3.92 KB
/
completeGenericTree.cpp
File metadata and controls
234 lines (218 loc) · 3.92 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//implemented by: Akhil Aggarwal
//contact : akhilagg123@gmail.com
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class treenode
{
public:
int data;
vector<treenode*> children;
treenode(int data)
{
this->data=data;
}
~treenode()
{
for(int i=0;i<this->children.size();i++)
{
delete children[i];
}
}
};
//function to take input
treenode* takeInput()
{
int rootdata;
cout<<"enter root data "<<endl;
cin>>rootdata;
treenode *root=new treenode(rootdata);
int n;
cout<<"enter no. of children of "<<root->data<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
treenode *child=takeInput();
root->children.push_back(child);
}
return root;
}
//function to take input level wise
treenode * takeInputLevelWise()
{
int rootdata;
cout<<"enter root data "<<endl;
cin>>rootdata;
treenode *root=new treenode(rootdata);
queue<treenode *>q;
q.push(root);
while(q.size()!=0)
{
treenode* front=q.front();
q.pop();
cout<<"enter no. of children of "<<front->data<<endl;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter value of "<<i<<"th child of "<<front->data<<endl;
int val;
cin>>val;
treenode *child=new treenode(val);
q.push(child);
front->children.push_back(child);
}
}
return root;
}
//function to print
void print(treenode *root)
{
if(root==NULL)
{
return ;
}
cout<<root->data<<":";
for(int i=0;i<root->children.size();i++)
{
cout<<root->children[i]->data<<",";
}
cout<<endl;
for(int i=0;i<root->children.size();i++)
{
print(root->children[i]);
}
}
//function to print levelwise
void printLevelWise(treenode *root)
{
if(root==NULL)
{
return ;
}
queue<treenode*> q;
q.push(root);
while(q.size()!=0)
{
treenode *front=q.front();
q.pop();
cout<<front->data<<": ";
for(int i=0;i<front->children.size();i++)
{
q.push(front->children[i]);
cout<<front->children[i]->data<<",";
}
cout<<endl;
}
}
//function to count no. of nodes
int count(treenode* root)
{
int ans=0;
if(root==NULL)
{
return 0;
}
for(int i=0;i<root->children.size();i++)
{
cout<<"ans called for "<<root->children[i]->data<<" "<<ans<<endl;
ans+=count(root->children[i]);
cout<<"ans called for "<<root->children[i]->data<<" "<<ans<<endl;
}
return ans+1;
}
//function to find nodes at depth k
void depth(treenode* root,int k)
{
if(root==NULL)
{
return ;
}
if(k==0)
{
cout<<root->data<<" ";
return;
}
for(int i=0;i<root->children.size();i++)
{
depth(root->children[i],k-1);
}
}
//function to count leafnodes
int leafnode(treenode *root)
{
int ans=0;
if(root==NULL)
{
return 0;
}
if(root->children.size()==0)
{
cout<<"leaf node: "<<root->data<<" ";
return 1;
}
for(int i=0;i<root->children.size();i++)
{
ans+=leafnode(root->children[i]);
}
return ans;
}
//program to find height of a tree
int height(treenode* root)
{
int ans=0;
if(root==NULL)
{
return 0;
}
for(int i=0;i<root->children.size();i++)
{
int max1=height(root->children[i]);
ans=max(ans,max1);
}
ans+=1;
return ans;
}
//preorder traversal
void preorder(treenode* root)
{
if(root==NULL)
{
return ;
}
cout<<root->data<<" ";
for(int i=0;i<root->children.size();i++)
{
preorder(root->children[i]);
}
}
//postorder traversal
void postorder(treenode* root)
{
if(root==NULL)
{
return ;
}
for(int i=0;i<root->children.size();i++)
{
postorder(root->children[i]);
}
cout<<root->data<<" ";;
}
// 10 3 20 30 40 2 50 60 2 70 80 1 90 1 100 0 0 0 0 0
int main()
{
treenode *root=takeInputLevelWise();
printLevelWise(root);
cout<<count(root)<<endl;
depth(root,2);
cout<<endl;
cout<<"total leaf node = "<<leafnode(root)<<endl;
preorder(root);
cout<<endl;
postorder(root);
cout<<endl;
cout<<height(root)<<endl;
delete root;
}