-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.h
64 lines (57 loc) · 1.97 KB
/
Node.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
#ifndef EX1_NODE_H
#define EX1_NODE_H
#include <cmath>
template <typename Key, typename Data>
class Node {
private:
const Key key;
Data data;
Node<Key, Data>* left = nullptr;
Node<Key, Data>* right = nullptr;
Node<Key, Data>* father = nullptr;
public:
int height, balancingParameter;
explicit Node<Key, Data>(Key const &k, Data const &val) : key(k), data(val), left(nullptr),
right(nullptr), father(nullptr), height(0), balancingParameter(0) {};
~Node() = default;
Data &getData() { return this->data; }
const Key &getKey() const { return this->key; }
Node *getLeft() const { return this->left; }
Node *getRight() const { return this->right; }
void setData(Data &nodeData){
this->data=nodeData;
}
void setLeft(Node* const leftNode){
this->left = leftNode;
if(leftNode != nullptr){
leftNode->setFather(this);
}
this->calculateHeightAndBalance();
}
void setRight(Node* const rightNode){
this->right = rightNode;
if(rightNode != nullptr){
rightNode->setFather(this);
}
this->calculateHeightAndBalance();
}
void clearNode(){
this->left = nullptr;
this->right = nullptr;
this->father = nullptr;
}
void setFather(Node<Key, Data>* const prev){ this->father = prev;}
void calculateHeightAndBalance(){
if(this->getLeft() == nullptr && this->getRight() == nullptr) {
this->balancingParameter = 0;
this->height = 0;
return;
}
int heightLeft = this->getLeft() == nullptr? 0:this->getLeft()->height;
int heightRight = this->getRight() == nullptr? 0:this->getRight()->height;
this->height = fmax((double) heightRight, (double) heightLeft)+1;
this->balancingParameter = heightLeft - heightRight;
}
Node<Key, Data> *getFather() const { return this->father; }
};
#endif //EX1_NODE_H