-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_1.cpp
207 lines (172 loc) · 4.96 KB
/
tree_1.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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
template <typename T>
class TreeNode
{
public:
T data;
vector<TreeNode*> children;
TreeNode(T data){
this->data=data;
}
~TreeNode(){}
};
//basic printing of trees
void printTree(TreeNode<int>* root){
cout<<root->data<<endl;
for(int i=0;i<root->children.size();i++){
printTree(root->children[i]);
}
}
//printing trees with detail
void printTreeWithDetail(TreeNode<int>* root){
cout<<root->data<<":";
//now here ek loop chalake is node ke harek node ko print krenge
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++){
printTreeWithDetail(root->children[i]);
}
}
//preorder printing
void preorder(TreeNode<int>* root){
if(root==NULL)
return;
cout<<root->data<<" ";
for(int i=0;i<root->children.size();i++){
preorder(root->children[i]);
}
}
//TODO: levelorder printing
/*void levelorder(TreeNode<int>* root){
if(root==NULL)
return;
for(int i=0;i<root->children.size();i++){
cout<<root->data<<" ";
levelorder(children[i]);
}
}*/
//TODO: postorder printing
void postorder(TreeNode<int>* root){
if(root==NULL)
return;
for(int i=0;i<root->children.size();i++){
preorder(root->children[i]);
cout<<root->data<<" ";
}
}
//taking input for trees recursively
TreeNode<int>* takeInput()
{
int rootData;
cout<<"enter data:"<<endl;
cin>>rootData;
TreeNode<int>* root = new TreeNode<int>(rootData);
//now taking input about childrens
int n;
cout<<"enter no. of children of"<<rootData<<endl;
cin>>n;
for(int i=0;i<n;i++){
TreeNode<int>* child = takeInput();
root->children.push_back(child);
}
return root;
}
//counting total nodes in a tree
int countNodes(TreeNode<int>* root){
int ans=1;
for(int i=0;i<root->children.size();i++){
ans=ans+countNodes(root->children[i]);
}
return ans;
}
//printing nodes at level k
void printAtLevelK(TreeNode<int>* root, int k)
{
if(k==0){
cout<<root->data<<endl;
return;
}
for(int i=0; i<root->children.size();i++){
printAtLevelK(root->children[i],k-1);
}
}
//taking input level wise
TreeNode<int>* takeInputLevelWise(){
int rootData;
cout<<"Enter root data: "<<endl;
cin>>rootData;
//now root node banake usme rootData ka value daal denge
TreeNode<int>* root = new TreeNode<int>(rootData);
//now ek queue bana lenge
queue<TreeNode<int>*> pendingNodes;
//aur root ko queue me push kr denge
pendingNodes.push(root);
//now jab tk queue ka size empty na ho tab ke loop chalayenge
//aur ek ek node nikalenge unke child node ka data enter krwayenge
//aur unke child nodes ke data ko queue me hi push krte jayenge
//kuki unke bhi to child nodes ka data enter krna hoga agr honge to
while(pendingNodes.size()!=0){
//front element ko nikal liye queue se ab iske children enter krwayenge
TreeNode<int>* front = pendingNodes.front();
pendingNodes.pop();
cout<<"Enter number of childrens of:"<<front->data<<endl;
int numChild;
cin>>numChild;
//loop chalake jitne numchild hain utne entry krwa lenge
for(int i=0;i<numChild;i++){
int childData;
cout<<"Enter "<<i<<"th child of "<<front->data<<endl;
cin>>childData;
//vector me push back krwayege child ko
TreeNode<int>* child = new TreeNode<int>(childData);
front->children.push_back(child);
pendingNodes.push(child);
}
}
return root;
}
//delete tree
//is function ko direct tree class me implement kr skte hain
void deleteTree(TreeNode<int>* root)
{
for(int i=0;i<root->children.size();i++){
deleteTree(root->children[i]);
}
delete root;
}
//1 3 2 3 4 2 5 6 2 7 8 0 0 0 0 1 9 0
int main()
{
cout<<"-------------Program started-------------"<<endl;
/* TreeNode<int>* root = new TreeNode<int>(10);
TreeNode<int>* node1 = new TreeNode<int>(20);
TreeNode<int>* node2 = new TreeNode<int>(30);
//abhi bs three node create kiye hain linked nhi hain abhi aapas me
root->children.push_back(node1);
root->children.push_back(node2);
//root ke children vector me node1 and node2 ko push kr diye isse ye connect ho gye
*/
//taking input normal
//TreeNode<int>* root = takeInput();
//taking input levelwise
TreeNode<int>* root = takeInputLevelWise();
//printTree(root);
//printing the tree
//printing tree with detail
//printTreeWithDetail(root);
//preorder printing
//preorder(root);
//levelorder printing
//levelorder(root);
//postorder printing
postorder(root);
//counting total nodes
int totalNodes = countNodes(root);
cout<<"Total nodes are: "<<totalNodes<<endl;
return 0;
}