-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree.h
226 lines (207 loc) · 6.24 KB
/
AVLTree.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: AVLTree.h
* Author: Chengheng Li Chen
*
* Created on 6 de matempo de 2021, 20:37
*/
#ifndef AVLTREE_H
#define AVLTREE_H
#include "math.h"
#include "BinarySearchTree.h"
#include "BinaryTreeNode.h"
template <class K, class V>
class AVLTree : public BinarySearchTree<K, V> {
public:
AVLTree();
AVLTree(const AVLTree& orig);
//virtual ~AVLTree();
virtual BinaryTreeNode<K, V>* add(const K& k, const V& value);
private:
int node_height(BinaryTreeNode<K, V>* newNode) const;
void insertar(BinaryTreeNode<K, V>*& newNode, BinaryTreeNode<K, V>* n);
void comprovar(BinaryTreeNode<K, V>* node, K key);
void rightRotate(BinaryTreeNode<K, V>* n);
void leftRotate(BinaryTreeNode<K, V>* n);
};
/**
* Constructor AVLTree
*/
template<class K, class V>
AVLTree<K, V>::AVLTree() {
}
/**
* Constructor copia AVLTree
* @param orig original
*/
template<class K, class V>
AVLTree<K, V>::AVLTree(const AVLTree& orig) {
}
/**
* Funcion que comprueba si hace falta rotar
*
* Cost O(1)
*
* @param node nodo a comprovar
* @param key key del nodo nuevo
*/
template<class K, class V>
void AVLTree<K, V>::comprovar(BinaryTreeNode<K, V>* node, K key) {
//
if ((node_height(node->getRight()) - node_height(node->getLeft()) > 1)) {
if (key > node->getRight()->getKey()) {
leftRotate(node);
} else {
rightRotate(node->getRight());
leftRotate(node);
}
} else
if (node_height(node->getLeft()) - node_height(node->getRight()) > 1) {
if (key < node->getLeft()->getKey()) {
rightRotate(node);
} else {
leftRotate(node->getLeft());
rightRotate(node);
}
}
node->setHeight(1 + max(node_height(node->getRight()), node_height(node->getLeft())));
}
/**
* Rotacio cap a la dreta, solament fa conexions y assignacions de cost O(1)
*
* Cost O(1)
*
* @param n node a rotar
*/
template<class K, class V>
void AVLTree<K, V>::rightRotate(BinaryTreeNode<K, V>* n) {
//Hace todas las conexiones de una rotación hacia la derecha
BinaryTreeNode<K, V>* temp = n->getLeft();
n->setLeft(temp->getRight());
if (temp->getRight() != nullptr) {
temp->getRight()->setPare(n);
}
temp->setPare(n->getPare());
if (n->getPare() == nullptr) {
this->p_root = temp;
} else if (n == n->getPare()->getRight()) {
n->getPare()->setRight(temp);
} else {
n->getPare()->setLeft(temp);
}
temp->setRight(n);
n->setPare(temp);
// update the balance factor
n->setHeight(1 + max(node_height(n->getRight()), node_height(n->getLeft())));
temp->setHeight(1 + max(node_height(temp->getRight()), node_height(temp->getLeft())));
}
/**
* Rotacio cap a l'esquerra, solament fa conexions y assignacions de cost O(1)
*
* Cost O(1)
*
* @param n node a rotar
*/
template<class K, class V>
void AVLTree<K, V>::leftRotate(BinaryTreeNode<K, V>* n) {
//Hace todas las conexiones de una rotación hacia la izquierda.
BinaryTreeNode<K, V>* temp = n->getRight();
n->setRight(temp->getLeft());
if (temp->getLeft() != nullptr) {
temp->getLeft()->setPare(n);
}
temp->setPare(n->getPare());
if (n->getPare() == nullptr) {
this->p_root = temp;
} else if (n == n->getPare()->getLeft()) {
n->getPare()->setLeft(temp);
} else {
n->getPare()->setRight(temp);
}
temp->setLeft(n);
n->setPare(temp);
// update the balance factor
n->setHeight(1 + max(node_height(n->getRight()), node_height(n->getLeft())));
temp->setHeight(1 + max(node_height(temp->getRight()), node_height(temp->getLeft())));
}
/**
*
* Añade un nodo al árbol
*
* O(log(n))
*
* @param k key del nodo
* @param value valor del nodo
*/
template<class K, class V>
BinaryTreeNode<K, V>* AVLTree<K, V>::add(const K& k, const V& value) {
//Si esta vacío ponemos el root
if (this->isEmpty()) {
BinaryTreeNode<K, V> *newNode = new BinaryTreeNode<K, V>(k);
newNode->addValue(value);
this->p_root = newNode;
return this->p_root;
}
//Si no esta vacío llamamos a la función auxiliar
else {
BinaryTreeNode<K, V> *newNode = new BinaryTreeNode<K, V>(k);
newNode->addValue(value);
insertar(newNode, this->p_root);
return newNode;
}
}
/**
* Función auxiliar recursivo para insertar un nodo
*
* @param newNode nodo a insertar
* @param n nodo para la recursión
*/
template<class K, class V>
void AVLTree<K, V>::insertar(BinaryTreeNode<K, V>*& newNode, BinaryTreeNode<K, V>* n) {
//Si el key es mayor a nos vamos a la derecha
if (newNode->getKey() > n->getKey()) {
if (n->hasRight()) {
insertar(newNode, n->getRight());
} else {
//Si no hay derecha añadimos el nodo en ese sitio
newNode->setPare(n);
n->setRight(newNode);
}
//Si tiene el mismo key se añade el valor al vector del key
} else if (newNode->getKey() == n->getKey()) {
n->addValue(newNode->getValues()[0]);
//Se elimina el nodo
delete newNode;
//Se asigna el valor para el return
newNode = n;
//Si el key es menor nos vamos a la izquierda
} else {
if (n->hasLeft()) {
insertar(newNode, n->getLeft());
} else {
//Si no hay izquierda añadimos nodo
newNode->setPare(n);
n->setLeft(newNode);
}
}
//comprueba si hace falta balancear
comprovar(n, newNode->getKey());
}
/**
* Métode auxiliar per a calcular l'alçada/ factor de balanç.
*
* @param newNode
* @return factor de balance del nodo
*/
template<class K, class V>
int AVLTree<K, V>::node_height(BinaryTreeNode<K, V>* newNode) const {
if (newNode == nullptr)
return -1;
else
return newNode->getHeight();
}
#endif /* AVLTREE_H */