-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinaryTree.h
197 lines (148 loc) · 4.94 KB
/
binaryTree.h
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
//
// Created by wang mengbo on 2019-09-02.
//
#ifndef PROGRAM_BINARYTREE_H
#define PROGRAM_BINARYTREE_H
#include <vector>
#include <stack>
#include <iostream>
#include <limits>
#include <cmath>
#include "data.h"
#include "multiTree.h"
#include "params.h"
#include "utils.h"
namespace SuperTAD::binary {
struct TreeNode {
TreeNode *_left=NULL, *_right=NULL, *_parent=NULL;
double _se=0, _info=0, _D=0, _vol=0;
int _val[2] = {0, 0}, _idx;
TreeNode(int start, int end)
{
_val[0] = start;
_val[1] = end;
}
TreeNode(int start, int end, Data &data) : TreeNode(start, end)
{
this->setVol(data);
}
TreeNode &operator= (const TreeNode ©)
{
_val[0] = copy._val[0];
_val[1] = copy._val[1];
_se = copy._se;
_left = copy._left;
_right = copy._right;
_info = copy._info;
_parent = copy._parent;
_D = copy._D;
_vol = copy._vol;
_idx = copy._idx;
return *this;
}
void setIdx(int idx)
{
_idx = idx;
}
// calculate structure entropy as node consists of only leaves, given parent node and return
double getSEasLeaf(Data &data, TreeNode &pNode)
{
double se = data.getSE(_val[0], _val[1], pNode._val[0], pNode._val[1]);
se += (data.getVol(_val[0], _val[1]) * data._logVolTable[_val[0]][_val[1]-_val[0]]) / data._doubleEdgeSum;
if (_val[0] == 0)
se -= data._sumOfGtimesLogG[_val[1]] / data._doubleEdgeSum;
else
se -= (data._sumOfGtimesLogG[_val[1]] - data._sumOfGtimesLogG[_val[0]-1]) / data._doubleEdgeSum;
return se;
}
// calculate structure entropy, given parent node and return
double getSE(Data &data, TreeNode &pNode)
{
double se = data.getSE(_val[0], _val[1], pNode._val[0], pNode._val[1]);
if (_left)
se += _left->getSE(data, *this);
if (_right)
se += _right->getSE(data, *this);
return se;
}
// set volume
void setVol(Data &data)
{
_vol = data.getVol(_val[0], _val[1]);
}
bool operator==(const TreeNode &t) const
{
return _val[0] == t._val[0] && _val[1] == t._val[1];
}
};
inline std::ostream& operator<<(std::ostream &os, const TreeNode &node)
{
os << "idx=" << node._idx << ", ";
os << "self=(" << node._val[0] << ", " << node._val[1] << ")";
if (node._left != NULL) {
os << ", left=(" << node._left->_val[0] << ", " << node._left->_val[1] << ")";
}
else {
os << ", no left child";
}
if (node._right != NULL) {
os << ", right=(" << node._right->_val[0] << ", " << node._right->_val[1] << ")";
}
else {
os << ", no right child";
}
os << ", vol=" << node._vol;
return os;
}
class Tree {
private:
std::stack<TreeNode*> _t;
public:
Data *_data;
TreeNode *_root;
std::vector<TreeNode*> _nodeList;
Tree();
~Tree();
void setData(Data &d);
// BUG; deprecated; use insert instead
void add(int start, int end, int k);
void insert(TreeNode *newNode, TreeNode *parentNode);
TreeNode &root() { return *_root; }
// double getSE(TreeNode &child, TreeNode &parent);
};
static const int PruneMethod1 = 1;
static const int PruneMethod2 = 2;
class BasePruner {
public:
Data *_data;
double **_minHtable, _optimalSE;
int **_minIdxTable, _K, _mu, _optimalK;
binary::Tree *_tree;
multi::Tree _prunedTree;
BasePruner(Tree &tree);
virtual ~BasePruner();
virtual void execute() { fprintf(stderr, "execute() in BasePruner should not be called\n"); };
};
// legacy!!! prune deep binary tree
class Pruner1 : public BasePruner {
public:
Pruner1(Tree &tree, int k=10);
~Pruner1() override;
void execute() override;
double getH(TreeNode &node, int k);
void backTrace(TreeNode &node, int k);
};
// prune deep binary tree
// H(mu, k)=min_k1{H(mu_l, k1)+H(mu_r, k-k2)}
class Pruner2 : public BasePruner {
public:
Pruner2(Tree &tree);
~Pruner2() override;
// void init();
void execute() override;
// recursive approach for optimal H given k
double getH(TreeNode &node, int k);
void backTrace(TreeNode &node, int k);
};
}
#endif //PROGRAM_BINARYTREE_H