-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsumtree.cpp
More file actions
executable file
·171 lines (141 loc) · 4.21 KB
/
Copy pathsubsumtree.cpp
File metadata and controls
executable file
·171 lines (141 loc) · 4.21 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
/*
* subsumstree.cpp
*
* Created on: 2013-11-10
* Author: Rong Xiao
*/
#include "subsumtree.hpp"
#include <forward_list>
#include <cstdlib>
#include <iostream>
#include <ctime>
using std::cin;
using std::cout;
using std::endl;
using std::forward_list;
void SubsumTree::buildSubsumTree(const vector<int>& vec) {
for (size_t i = 0; i<vec.size(); ++i) {
treeNodes.push_back(new BackTreeNode(vec[i], i));
}
forward_list<BackTreeNode*> buildingLayer;
forward_list<BackTreeNode*>::iterator it = buildingLayer.before_begin();
size_t n = treeNodes.size();
for (size_t i =0; true; ) {
if (i==n) {
break;
}
else if (i+1<n) {
it = buildingLayer.insert_after(it, new BackTreeNode(treeNodes[i], treeNodes[i+1]));
i += 2;
} else {
it = buildingLayer.insert_after(it, treeNodes[i]);
i++;
}
}
size_t height = 1;
/*
cout << "Building layer: | ";
for (forward_list<BackTreeNode*>::iterator tmp = buildingLayer.begin(); tmp != buildingLayer.end(); tmp++) {
cout << " -> " << (*tmp)->val;
}
cout << endl;
*/
while (true) {
forward_list<BackTreeNode*>::iterator cur = buildingLayer.before_begin();
forward_list<BackTreeNode*>::iterator it = buildingLayer.begin();
if (++it == buildingLayer.end()) {
break;
} else {
it = buildingLayer.begin();
}
while(it!=buildingLayer.end() && ++it!=buildingLayer.end()) {
forward_list<BackTreeNode*>::iterator it1 = cur;
it1++;
cur=buildingLayer.insert_after(cur, new BackTreeNode(*it1, *it));
it = buildingLayer.erase_after(cur);
it = buildingLayer.erase_after(cur);
}
/*
cout << "Building layer: | ";
for (forward_list<BackTreeNode*>::iterator tmp = buildingLayer.begin(); tmp != buildingLayer.end(); tmp++) {
cout << " -> " << (*tmp)->val;
}
cout << endl;
*/
height ++ ;
}
cout << "Tree heigth is " << height << endl;
}
SubsumTree::SubsumTree(const vector<int>& vec) : treeNodes() {
buildSubsumTree(vec);
/*
for (size_t i = 0; i<treeNodes.size(); ++i) {
cout <<' '<< reinterpret_cast<size_t>(treeNodes[i]->parent);
}
cout << endl;
*/
}
void SubsumTree::deleteSubsumTree() {
for (size_t i = 0; i<treeNodes.size(); ++i) {
BackTreeNode* t = treeNodes[i];
while (t) {
BackTreeNode* tmp = t->parent;
if (tmp && t->lstart == tmp->lstart) {
delete t;
t = tmp;
} else {
delete t;
t = 0;
}
}
}
}
SubsumTree::~SubsumTree() {
deleteSubsumTree();
}
int SubsumTree::query(size_t i, size_t j) {
j--;
BackTreeNode* t = treeNodes[i];
int diff = 0;
while (t->rend<j) { // t &&
BackTreeNode* tmp = t->parent;
if (t->rend == tmp->rend) {
diff += (tmp->val - t->val);
}
t = tmp;
}
t = treeNodes[j];
while (t->lstart>i) { // t &&
BackTreeNode* tmp = t->parent;
if (t->lstart == tmp->lstart) {
diff += (tmp->val - t->val);
}
t = tmp;
}
return t->val - diff;
}
int main(int argc, char** argv) {
size_t n = static_cast<size_t>(atoi(argv[1]));
vector<int> vec(n);
// cout << "Please input your " << m << " by " << n << " matrix row by row" << endl;
srand (time(NULL));
for (size_t i = 0; i < n; ++i) {
vec[i] = rand() % 11 - 6;
// cout << ' ' << vec[i];
}
// cout << endl;
SubsumTree subsums(vec);
while (true) {
size_t i;
size_t j;
cout << "Input the start and end index to query " << endl;
cin >> i;
cin >> j;
if (i>=j || j>vec.size()) { // i<0 no need to check
break;
} else {
cout << "Sub sum is " << subsums.query(i,j) << endl;
}
}
return 0;
}