-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
221 lines (190 loc) · 6.23 KB
/
Copy pathnode.h
File metadata and controls
221 lines (190 loc) · 6.23 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
#ifndef NODE_H_
#define NODE_H_
#include <memory>
template <class K, class D>
class Node{
private :
K key;
std::shared_ptr<D> data;
std::shared_ptr<Node> left;
std::shared_ptr<Node> right;
std::shared_ptr<Node> papa;
int height;
int traffic;
int nodeCount;
int self_traffic;
public:
Node():key(),data(nullptr),left(nullptr),right(nullptr),papa(nullptr),height(1),traffic(0),nodeCount(1),self_traffic(0){};
Node(K key, std::shared_ptr<D> data,std::shared_ptr<Node> papa);
Node(K key, std::shared_ptr<D> data,std::shared_ptr<Node> papa, int self_traffic);
~Node() = default;
Node(const Node&)= delete;
std::shared_ptr<Node> operator=(const Node&)= delete;
void calcHeight();
void setLeft(const std::shared_ptr<Node<K,D>>& left);
void setRight(const std::shared_ptr<Node<K,D>>& right);
int getHeight() const;
const K& getKey() const;
std::shared_ptr<D> getData();
void setData(const D& data);
void setData(const std::shared_ptr<D>& data);
std::shared_ptr<Node> getLeft();
std::shared_ptr<Node> getRight();
std::shared_ptr<Node> getPapa();
void setPapa(std::shared_ptr<Node> papa);
bool isLeaf();
bool isRoot();
// for list use.
std::shared_ptr<Node> getNext();
std::shared_ptr<Node> getPrev();
void setPrev(const std::shared_ptr<Node>& prev);
void setNext(const std::shared_ptr<Node>& next);
Node(K key, std::shared_ptr<D> data,std::shared_ptr<Node> prev,std::shared_ptr<Node> next);
Node(K key, std::shared_ptr<D> data);
int getNodeCount();
void setNodeCount(int nodeCount);
int getTraffic();
void setSelfTraffic(int traffic);
void calcRank();
int getSelfTraffic();
void setKey(const K& key);
};
static int max(int a, int b){
if (a>b) return a;
return b;
}
template <class K, class D>
Node<K,D>::Node(K key, std::shared_ptr<D> data_ptr, std::shared_ptr<Node<K,D>> papa):key(key),data(data_ptr),papa(papa),height(1),traffic(0),nodeCount(1){}
template <class K, class D>
Node<K,D>::Node(K key, std::shared_ptr<D> data_ptr,std::shared_ptr<Node> papa, int traffic):key(key),data(data_ptr),
papa(papa),height(1),traffic(traffic),nodeCount(1), self_traffic(traffic){}
/*
template <class K, class D>
Node<K,D>::~Node(){
delete data;
}
*/
template <class K, class D>
void Node<K,D>::calcHeight(){
if(this->left== nullptr && this->right== nullptr)this->height = 1; //leaf
else if(this->left== nullptr) this->height = this->right->height +1; // no left son
else if(this->right== nullptr) this->height = this->left->height +1; // no right son
else this->height= max(this->left->height,this->right->height) + 1; // max between sons
}
template <class K, class D>
void Node<K,D>::setLeft(const std::shared_ptr<Node<K,D>>& left){
this->left = left;
}
template <class K, class D>
void Node<K,D>::setRight(const std::shared_ptr<Node<K,D>>& right){
this->right = right;
}
template <class K, class D>
int Node<K,D>::getHeight() const{
return this->height;
}
template <class K, class D>
const K& Node<K,D>::getKey() const{
return this->key;
}
template <class K, class D>
std::shared_ptr<D> Node<K,D>::getData(){
return this->data;
}
template <class K, class D>
void Node<K,D>::setData(const D& data){
delete this->data;
this->data = new D(data);
}
template <class K, class D>
void Node<K,D>::setData(const std::shared_ptr<D>& data){
this->data = data;
}
template <class K, class D>
std::shared_ptr<Node<K,D>> Node<K,D>::getLeft(){
return this->left;
}
template <class K, class D>
std::shared_ptr<Node<K,D>> Node<K,D>::getRight(){
return this->right;
}
template <class K, class D>
std::shared_ptr<Node<K,D>> Node<K,D>::getPapa(){
return this->papa;
}
template <class K, class D>
void Node<K,D>::setPapa(std::shared_ptr<Node<K,D>> papa){
this->papa= papa;
}
template <class K, class D>
bool Node<K,D>::isLeaf() {
return (this->getRight() == nullptr && this->getLeft() == nullptr);
}
template <class K, class D>
bool Node<K,D>::isRoot(){
return this->getPapa() == nullptr;
}
template <class K, class D>
std::shared_ptr<Node<K,D>> Node<K,D>::getNext(){
return this->getRight();
}
template <class K, class D>
void Node<K,D>::setNext(const std::shared_ptr<Node<K,D>>& next){
this->setRight(next);
}
template <class K, class D>
std::shared_ptr<Node<K,D>> Node<K,D>::getPrev(){
return this->getLeft();
}
template <class K, class D>
void Node<K,D>::setPrev(const std::shared_ptr<Node<K, D>>& prev){
this->setLeft(prev);
}
template <class K, class D>
Node<K,D>::Node(K key, std::shared_ptr<D> data_ptr,std::shared_ptr<Node> prev,std::shared_ptr<Node> next):key(key),data(data_ptr),left(prev),right(next),height(1){}
template <class K, class D>
Node<K,D>::Node(K key, std::shared_ptr<D> data_ptr):key(key),data(data_ptr),left(nullptr),right(nullptr),height(1){}
template <class K, class D>
int Node<K,D>::getNodeCount(){
return this->nodeCount;
}
template <class K, class D>
void Node<K,D>::setNodeCount(int nodeCount){
this->nodeCount=nodeCount;
}
template <class K, class D>
int Node<K,D>::getTraffic(){
return this->traffic;
}
template <class K, class D>
void Node<K,D>::setSelfTraffic(int traffic){
this->self_traffic=traffic;
}
template <class K, class D>
void Node<K,D>::calcRank(){
if(this->getLeft()!= nullptr && this->getRight()!= nullptr){
this->traffic = this->getLeft()->getTraffic() + this->getRight()->getTraffic() + this->self_traffic;
this->nodeCount = this->getLeft()->getNodeCount() + this->getRight()->getNodeCount() + 1;
}
else if(this->getLeft()!= nullptr && this->getRight()== nullptr){
this->traffic = this->getLeft()->getTraffic() + this->self_traffic;
this->nodeCount = this->getLeft()->getNodeCount() + 1;
}
else if(this->getLeft()== nullptr && this->getRight()!= nullptr){
this->traffic = this->getRight()->getTraffic() + this->self_traffic;
this->nodeCount = this->getRight()->getNodeCount() + 1;
}
else{
this->traffic = this->self_traffic;
this->nodeCount = 1;
}
}
template <class K, class D>
int Node<K,D>::getSelfTraffic(){
return this->self_traffic;
}
template <class K, class D>
void Node<K,D>::setKey(const K& key){
this->key = key;
}
#endif