-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-code-decode.hpp
310 lines (245 loc) · 7.51 KB
/
tree-code-decode.hpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <math.h>
#include <deque>
template <typename T>
struct tNode
{
T val;
struct tNode * left;
struct tNode * right;
tNode():left(NULL),right(NULL){}
tNode(const T& v):left(NULL),right(NULL),val(v)
{}
};
//tree
namespace tree{
#define maxl(a,b)\
({ __typeof__ (a) a_ = (a);\
__typeof__ (b) b_ = (b);\
a_ > b_ ? a_ : b_;})
#define minl(a,b)\
({ __typeof__ (a) a_ = (a);\
__typeof__ (b) b_ = (b);\
a_ < b_ ? a_ : b_;})
template<typename T>
std::size_t binaryTreeMaxHeigh(tNode<T> *root)
{
if(root == NULL) return 0;
//Postorder Traversal
std::size_t lef = binaryTreeMaxHeigh(root->left);
std::size_t rig = binaryTreeMaxHeigh(root->right);
return 1 + std::max( lef , rig);
//return 1 + std::max( binaryTreeMaxHeigh(root->left) , binaryTreeMaxHeigh(root->right) );
}
template<typename T>
void binaryTreePher(tNode<T> * root,std::vector<std::vector<std::string>>& res,std::size_t row,std::size_t lef,std::size_t rig)
{
if(root == NULL) return ;
std::size_t mid = lef + (rig - lef) /2;
res[row][mid] = std::to_string(root->val);
binaryTreePher(root->left,res,row+1,lef,mid-1);
binaryTreePher(root->right,res,row+1,mid+1,rig);
}
template<typename T>
void binaryTreePrint(tNode<T> * root)
{
std::size_t row = binaryTreeMaxHeigh(root);
std::size_t colum = ::pow(2,row) - 1;
std::vector<std::vector<std::string> > res(row, std::vector<std::string>(colum," ") );
binaryTreePher(root,res,0,0,colum-1);
for(const auto& ii: res)
{
for(const auto& jj: ii)
{
printf("%s",jj.c_str());
}
printf("\n");
}
}
template<typename T>
void binaryTreePrintPreorder(tNode<T> * root)
{
if(root == NULL) return ;
printf("%d ",root->val);
binaryTreePrintPreorder(root->left);
binaryTreePrintPreorder(root->right);
}
}
// a binary tree Encode and Decode
namespace EncodeDecode{
template<typename T>
class BinaryTreeED
{
private:
tNode<T> * createNode(T val)
{
tNode<T> * nod = (tNode<T>*)calloc(1,sizeof(tNode<T>));
nod->val = val;
return nod;
}
void freeNode(tNode<T> *nod)
{
if(nod) free(nod);
nod = NULL;
//nod->left = NULL;
//nod->right = NULL;
}
/*
# --> is a NULL pointer
, --> is separator of 2 poiners
*/
void EncodePreorder(tNode<T>* root,std::string& res)
{
if(root == NULL)
{
res.append("#,");
return;
}
res.append(std::to_string(root->val)).append(",");
EncodePreorder(root->left,res);
EncodePreorder(root->right,res);
}
tNode<T> * DecodePreorder(std::list<std::string>& list)
{
if(list.empty()) return NULL;
std::string valss = std::move(list.front());list.pop_front();
if(valss.compare("#") == 0 ) return NULL;
tNode<T>* node = createNode(atoi(valss.c_str()));
node->left = DecodePreorder(list);
node->right = DecodePreorder(list);
return node;
}
/*
curlev : current level of binary three
*/
void encodeLevelDFS(std::vector<std::string>& res,int curlev,tNode<T> *root)
{
if(res.size() <= curlev) //current level of this tree is greater than vector(res) 's size
res.push_back( std::string("") );
if(root == NULL)
{
res[curlev].append(std::string("#,"));
return ;
}
res[curlev].append( std::to_string(root->val) + "," );
encodeLevelDFS(res, curlev + 1 ,root->left); //++curlev or curlev++ are all ERROR
encodeLevelDFS(res, curlev + 1 ,root->right);
}
void encodeLevelHelp(tNode<T> * root,std::string& rs)
{
if(root == NULL) return ;
std::deque<tNode<T>*> que;
que.push_back(root);
while(!que.empty())
{
for(std::size_t i=que.size(); i>0; i--)
{
tNode<T>* tp = que.front();que.pop_front();
if (tp == NULL)
{
rs.append("#,");
}else
{
rs.append(std::to_string(tp->val)).append(",");
que.push_back(tp->left);
que.push_back(tp->right);
}
}//end for
}
}//end function
void encodeLevel(tNode<T> *root,std::string& res_ss)
{
std::vector<std::string> level_evc;
encodeLevelDFS(level_evc,0,root);
for(const auto& m_le : level_evc)
{
res_ss.append(m_le);
}
}
tNode<T> * decodeLevel(std::list<std::string>& list_)
{
if(list_.size() <= 0) return NULL;
std::string valss = std::move(list_.front());list_.pop_front();
if(valss.compare("#") == 0) return NULL;
//store tree level traversal struct
tNode<T>* root = createNode(std::atoi(valss.c_str()));
std::deque<tNode<T>*> q_; q_.push_back(root);
while( !list_.empty() )
{
tNode<T>* parent = q_.front();q_.pop_front();
std::string valTmp = std::move(list_.front());list_.pop_front();
if(valTmp.compare("#") != 0)
{
parent->left = createNode(atoi(valTmp.c_str()));
q_.push_back(parent->left);
}
valTmp = std::move(list_.front());list_.pop_front();
if(valTmp.compare("#") != 0)
{
parent->right = createNode(atoi(valTmp.c_str()));
q_.push_back(parent->right);
}
}
return root;
}
public:
/*
# --> is a NULL pointer
, --> is separator of 2 poiners
*/
std::string Encode(tNode<T>* root)
{
std::string res;
//EncodePreorder(root,res);
//encodeLevel(root,res);
encodeLevelHelp(root,res);
return res; //RVO?
}
tNode<T> * Decode(const std::string& ress)
{
std::list<std::string> clist_;
std::size_t slen = ress.length();
for(std::size_t i=0,j=0; i<slen && j<slen; j++)
{
if(ress.at(j) == ',')
{
clist_.push_back(ress.substr(i,j-i));
i = j+1;
}
}
//return DecodePreorder(clist_);
return decodeLevel(clist_);
}//end Decode
BinaryTreeED()
{}
~BinaryTreeED()
{}
}; //end class
}//end namespace
#if 0
int main(int argc,char * argv[])
{
EncodeDecode::BinaryTreeED<int> ed;
//tNode<int> * root = ed.Decode("-3,1,15,#,#,-10,#,#,#,");
tNode<int> * root = ed.Decode("-3,1,#,15,-10,#,#,#,#,");
printf("-3,1,#,15,-10,#,#,#,#,\n");
// tNode<int> * root = (tNode<int>*)malloc(sizeof(tNode<int>));root->val = -3;// new tNode<int>(-3);
// tNode<int> * r1 = (tNode<int>*)malloc(sizeof(tNode<int>)); r1->val = 1;
// tNode<int> * r15 = (tNode<int>*)malloc(sizeof(tNode<int>)); r15->val = 15;
// tNode<int> * r10 = (tNode<int>*)malloc(sizeof(tNode<int>)); r10->val = -10;
// r15->left = r10;
// root->left = r1; root->right = r15;
std::string ssd = ed.Encode(root);
printf("after %s\n",ssd.c_str());
//tree::binaryTreePrintPreorder(root);
//printf("\n");
//EncodeDecode::tNode<int> * root = ed.Decode("-3,1,15,#,#,1029,#,#,#,");
tree::binaryTreePrint(root);
return EXIT_SUCCESS;
}
#endif