|
| 1 | +/******************************************************************************* |
| 2 | + * ALGORITHM IMPLEMENTAIONS |
| 3 | + * |
| 4 | + * /\ | _ _ ._ o _|_ |_ ._ _ _ |
| 5 | + * /--\ | (_| (_) | | |_ | | | | | _> |
| 6 | + * _| |
| 7 | + * |
| 8 | + * Adelson-Velskii and Landis' (AVL) tree |
| 9 | + * |
| 10 | + * Features, being N the number of elements in the tree: |
| 11 | + * 1. Guaranteed search time is O(log(N)). |
| 12 | + * 2. Dynamically updated/balanced tree structure O(N) storage. |
| 13 | + * 3. Exportable to GraphViz format for easy visualization and verification |
| 14 | + * |
| 15 | + * http://en.wikipedia.org/wiki/AVL_tree |
| 16 | + * |
| 17 | + ******************************************************************************/ |
| 18 | + |
| 19 | +#ifndef __AVL_H__ |
| 20 | +#define __AVL_H__ |
| 21 | + |
| 22 | +#include <iostream> |
| 23 | +#include <cmath> |
| 24 | +#include <stack> |
| 25 | + |
| 26 | +namespace alg { |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +class AVL { |
| 30 | + |
| 31 | + public: |
| 32 | + |
| 33 | + AVL() : tree(0), numNodes(0) {} |
| 34 | + |
| 35 | + T root () const { return tree->value; } |
| 36 | + unsigned height() const { return Node::getHeight(tree); } |
| 37 | + unsigned size() const { return numNodes; } |
| 38 | + bool isEmpty() const { return numNodes == 0; } |
| 39 | + |
| 40 | + bool contains(const T &x) const { |
| 41 | + if (!isEmpty()) { |
| 42 | + return tree->contains(x); |
| 43 | + } else return false; |
| 44 | + } |
| 45 | + |
| 46 | + void insert(const T &x) { |
| 47 | + if (isEmpty()) tree = new Node(x); |
| 48 | + else tree = tree->insert(x); |
| 49 | + numNodes++; |
| 50 | + } |
| 51 | + |
| 52 | + void erase(const T &x) { |
| 53 | + if (!isEmpty()) { |
| 54 | + bool found = false; |
| 55 | + tree = tree->erase(x, found); |
| 56 | + if (found) numNodes--; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void toGraphViz(std::ostream &stream, std::string name) const { |
| 61 | + if (!isEmpty()) { |
| 62 | + stream << "digraph " << name << " {" << std::endl; |
| 63 | + tree->toGraphViz(stream); |
| 64 | + stream << "}" << std::endl; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public: |
| 69 | + |
| 70 | + struct Node { |
| 71 | + Node *left, *right; |
| 72 | + T value; |
| 73 | + unsigned height; |
| 74 | + |
| 75 | + Node(const T &x) : left(0), right(0), value(x), height(1) {} |
| 76 | + |
| 77 | + bool contains(const T &x) const { |
| 78 | + if (value == x) return true; |
| 79 | + else if (x < value && left != 0) return left->contains(x); |
| 80 | + else if (right != 0) return right->contains(x); |
| 81 | + else return false; |
| 82 | + } |
| 83 | + |
| 84 | + Node *insert(const T &x) { |
| 85 | + if (x <= value) { |
| 86 | + if (left == 0) left = new Node(x); |
| 87 | + else left = left->insert(x); |
| 88 | + } |
| 89 | + else { |
| 90 | + if (right == 0) right = new Node(x); |
| 91 | + else right = right->insert(x); |
| 92 | + } |
| 93 | + |
| 94 | + return update(); |
| 95 | + } |
| 96 | + |
| 97 | + Node *erase(const T &x, bool &found) { |
| 98 | + if (value == x) { |
| 99 | + found = true; |
| 100 | + if (left == 0 && right == 0) { |
| 101 | + delete this; |
| 102 | + return 0; |
| 103 | + } else if (left == 0) { |
| 104 | + Node *aux = right; |
| 105 | + *this = *right; |
| 106 | + delete aux; |
| 107 | + } else if (right == 0) { |
| 108 | + Node *aux = left; |
| 109 | + *this = *left; |
| 110 | + delete aux; |
| 111 | + } else { |
| 112 | + // Tracing path to rightmost leaf of the left subtree |
| 113 | + std::stack<Node*> trace; |
| 114 | + |
| 115 | + Node *current = left; |
| 116 | + while (current != 0) { |
| 117 | + trace.push(current); |
| 118 | + current = current->right; |
| 119 | + } |
| 120 | + |
| 121 | + current = trace.top(); |
| 122 | + value = current->value; |
| 123 | + Node *lsubtree = current->left; |
| 124 | + delete current; |
| 125 | + trace.pop(); |
| 126 | + |
| 127 | + if (trace.empty()) { left = lsubtree; } |
| 128 | + else { |
| 129 | + trace.top()->right = lsubtree; |
| 130 | + trace.pop(); |
| 131 | + while (!trace.empty()) { |
| 132 | + current = trace.top(); |
| 133 | + current->right = current->right->update(); |
| 134 | + trace.pop(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + return update(); |
| 139 | + } |
| 140 | + else if (x < value) { |
| 141 | + if (left != 0) { |
| 142 | + left = left->erase(x, found); |
| 143 | + return update(); |
| 144 | + } else return this; |
| 145 | + } |
| 146 | + else { |
| 147 | + if (right != 0) { |
| 148 | + right = right->erase(x, found); |
| 149 | + return update(); |
| 150 | + } else return this; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + Node *update() { |
| 155 | + updateHeight(); |
| 156 | + |
| 157 | + if (getBF(this) >= 2) { |
| 158 | + if (getBF(left) <= -1) LR(); |
| 159 | + return LL(); |
| 160 | + } else if (getBF(this) <= -2) { |
| 161 | + if (getBF(right) >= 1) RL(); |
| 162 | + return RR(); |
| 163 | + } else return this; |
| 164 | + } |
| 165 | + |
| 166 | + void updateHeight() { height = std::max(getHeight(left), getHeight(right)) + 1; } |
| 167 | + |
| 168 | + void LR() { |
| 169 | + Node *lrcopy = left->right; |
| 170 | + left->right = lrcopy->left; |
| 171 | + lrcopy->left = left; |
| 172 | + left = lrcopy; |
| 173 | + left->left->updateHeight(); |
| 174 | + left->updateHeight(); |
| 175 | + updateHeight(); |
| 176 | + } |
| 177 | + |
| 178 | + void RL() { |
| 179 | + Node *rlcopy = right->left; |
| 180 | + right->left = rlcopy->right; |
| 181 | + rlcopy->right = right; |
| 182 | + right = rlcopy; |
| 183 | + right->right->updateHeight(); |
| 184 | + right->updateHeight(); |
| 185 | + updateHeight(); |
| 186 | + } |
| 187 | + |
| 188 | + Node *LL() { |
| 189 | + Node *lcopy = left; |
| 190 | + left = left->right; |
| 191 | + lcopy->right = this; |
| 192 | + lcopy->left->updateHeight(); |
| 193 | + lcopy->right->updateHeight(); |
| 194 | + lcopy->updateHeight(); |
| 195 | + return lcopy; |
| 196 | + } |
| 197 | + |
| 198 | + Node *RR() { |
| 199 | + Node *rcopy = right; |
| 200 | + right = right->left; |
| 201 | + rcopy->left = this; |
| 202 | + rcopy->left->updateHeight(); |
| 203 | + rcopy->right->updateHeight(); |
| 204 | + rcopy->updateHeight(); |
| 205 | + return rcopy; |
| 206 | + } |
| 207 | + |
| 208 | + static int getBF(const Node *t) { |
| 209 | + return getHeight(t->left) - getHeight(t->right); |
| 210 | + } |
| 211 | + |
| 212 | + static int getHeight(const Node *t) { |
| 213 | + return t == 0 ? 0 : t->height; |
| 214 | + } |
| 215 | + |
| 216 | + void toGraphViz(std::ostream &stream) const { |
| 217 | + stream << value << ";" << std::endl; |
| 218 | + if (left != 0) { |
| 219 | + stream << left->value << ";" << std::endl; |
| 220 | + stream << value << "->" << left->value << ";" << std::endl; |
| 221 | + left->toGraphViz(stream); |
| 222 | + } |
| 223 | + if (right != 0) { |
| 224 | + stream << right->value << ";" << std::endl; |
| 225 | + stream << value << "->" << right->value << ";" << std::endl; |
| 226 | + right->toGraphViz(stream); |
| 227 | + } |
| 228 | + } |
| 229 | + }; |
| 230 | + |
| 231 | + Node *tree; |
| 232 | + unsigned numNodes; |
| 233 | +}; |
| 234 | + |
| 235 | +} // namespace alg |
| 236 | + |
| 237 | +#endif // _ALG_AVL_HPP |
| 238 | + |
0 commit comments