123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- #include "libbinom/include/binom_impl/avl_tree.hxx"
- using namespace binom;
- using namespace binom::priv;
- using AVLNode = AVLTree::AVLNode;
- // ============================================================ Node
- void AVLNode::swapPosition(AVLNode& other, AVLTree& avl_tree) {
- auto this_position = getPosition();
- auto other_position = other.getPosition();
- if(parent != &other && other.parent != this) {
- std::swap(parent, other.parent);
- std::swap(left, other.left);
- std::swap(right, other.right);
- std::swap(depth, other.depth);
- } elif (parent == &other) {
- parent = other.parent;
- other.parent = this;
- if(other.left == this) {
- other.left = left;
- left = &other;
- std::swap(right, other.right);
- } else {
- other.right = right;
- right = &other;
- std::swap(left, other.left);
- }
- std::swap(depth, other.depth);
- } else {
- other.parent = parent;
- parent = &other;
- if(left == &other) {
- left = other.left;
- other.left = this;
- std::swap(right, other.right);
- } else {
- right = other.right;
- other.right = this;
- std::swap(left, other.left);
- }
- }
- if(hasLeft()) left->parent = this;
- if(hasRight()) right->parent = this;
- if(other.hasLeft()) other.left->parent = &other;
- if(other.hasRight()) other.right->parent = &other;
- switch (other_position) {
- case NodePosition::left: parent->left = this; break;
- case NodePosition::right: parent->right = this; break;
- case NodePosition::root: avl_tree.root = this; break;
- }
- switch (this_position) {
- case NodePosition::left: other.parent->left = &other; break;
- case NodePosition::right: other.parent->right = &other; break;
- case NodePosition::root: avl_tree.root = &other; break;
- }
- }
- void AVLNode::unpin(AVLTree& avl_tree) {
- if(isLeft()) parent->left = nullptr;
- elif(isRight()) parent->right = nullptr;
- else avl_tree.root = nullptr;
- parent = nullptr;
- }
- AVLNode::AVLNode(KeyValue key, AVLNode* parent) : parent(parent), key(std::move(key)) {}
- AVLNode::AVLNode(AVLNode& other)
- : depth(other.depth),
- left(other.left),
- right(other.right),
- parent(other.parent),
- key(other.key) {}
- AVLNode::AVLNode(AVLNode&& other)
- : depth(other.depth),
- left(other.left),
- right(other.right),
- parent(other.parent),
- key(std::move(other.key)) {}
- AVLNode& AVLNode::operator=(AVLNode other) { this->~AVLNode(); return *new(this) AVLNode(std::move(other)); }
- bool AVLNode::isRoot() const noexcept {return !parent;}
- bool AVLNode::isLeft() const noexcept {return isRoot() ? false : parent->left == this;}
- bool AVLNode::isRight() const noexcept {return isRoot() ? false : parent->right == this;}
- bool AVLNode::hasLeft() const noexcept {return left;}
- bool AVLNode::hasRight() const noexcept {return right;}
- bool AVLNode::hasChild() const noexcept {return left || right;}
- KeyValue& AVLNode::getKey() {return key;}
- const KeyValue& AVLNode::getKey() const {return key;}
- AVLNode::NodePosition AVLNode::getPosition() const {
- if(isLeft()) return NodePosition::left;
- elif(isRight()) return NodePosition::right;
- else return NodePosition::root;
- }
- // ============================================================ AVLTree
- i64 AVLTree::max(i64 a, i64 b) noexcept {return (a > b) ? a : b;}
- i64 AVLTree::depth(AVLNode* node) noexcept {return node ? node->depth : 0;}
- AVLNode* AVLTree::rotateRight(AVLNode* y) {
- auto y_position = y->getPosition();
- AVLNode* x = y->left;
- AVLNode* T2 = x->right;
- x->right = y;
- y->left = T2;
- y->depth = max(depth(y->left), depth(y->right)) + 1;
- x->depth = max(depth(x->left), depth(x->right)) + 1;
- // Update parent poionters
- x->parent = y->parent;
- y->parent = x;
- if(T2) T2->parent = y;
- // Update parent's child poionter
- if(y_position == AVLNode::NodePosition::left) x->parent->left = x;
- elif(y_position == AVLNode::NodePosition::right) x->parent->right = x;
- else root = x;
- return x;
- }
- AVLNode* AVLTree::rotateLeft(AVLNode* x) {
- auto x_position = x->getPosition();
- AVLNode* y = x->right;
- AVLNode* T2 = y->left;
- y->left = x;
- x->right = T2;
- y->depth = max(depth(y->left), depth(y->right)) + 1;
- x->depth = max(depth(x->left), depth(x->right)) + 1;
- // Update parent poionters
- y->parent = x->parent;
- x->parent = y;
- if(T2) T2->parent = x;
- // Update parent's child poionter
- if(x_position == AVLNode::NodePosition::left) y->parent->left = y;
- elif(x_position == AVLNode::NodePosition::right) y->parent->right = y;
- else root = y;
- return y;
- }
- i64 AVLTree::getBalance(AVLNode* node) { return node ? depth(node->left) - depth(node->right) : 0; }
- AVLNode* AVLTree::minKeyNode(AVLNode* node) noexcept {
- if(!node) return nullptr;
- while(node->left) node = node->left;
- return node;
- }
- AVLNode* AVLTree::minKeyNode() const noexcept {return minKeyNode(root);}
- AVLNode* AVLTree::maxKeyNode(AVLNode* node) noexcept {
- if(!node) return nullptr;
- while (node->right) node = node->right;
- return node;
- }
- AVLNode* AVLTree::maxKeyNode() const noexcept {return maxKeyNode(root);}
- AVLNode* AVLTree::insert(AVLNode* new_node) {
- AVLNode* node = root;
- if(!node) return root = new_node;
- forever {
- auto cmp = new_node->key.getCompare(node->key);
- if(cmp == KeyValue::lower) {
- if(node->left) { node = node->left; continue; }
- else { node->left = new_node; new_node->parent = node; break; }
- } elif(cmp == KeyValue::highter) {
- if(node->right) { node = node->right; continue; }
- else { node->right = new_node; new_node->parent = node; break; }
- } else return nullptr;
- }
- while(node) { // Balancing
- node->depth = 1 + max(depth(node->left), depth(node->right));
- i64 balance = getBalance(node);
- if (balance > 1 && getBalance(root->left) >= 0) {
- rotateRight(node);
- } elif (balance > 1 && getBalance(root->left) < 0) {
- rotateLeft(node->left);
- rotateRight(node);
- } elif (balance < -1 && getBalance(node->right) <= 0) {
- rotateLeft(node);
- } elif (balance < -1 && getBalance(node->right) > 0) {
- rotateRight(node->right);
- rotateLeft(node);
- }
- node = node->parent;
- }
- return new_node;
- }
- AVLNode* AVLTree::extract(KeyValue key) {
- AVLNode* result = nullptr;
- AVLNode* node = root;
- forever {
- // If node isn't finded
- if(!node) return nullptr;
- // Searching key...
- auto cmp = key.getCompare(node->key);
- if(cmp == KeyValue::lower) {node = node->left; continue;}
- elif(cmp == KeyValue::highter) {node = node->right; continue;}
- else { // Node is finded
- forever if(!node->left || !node->right) {
- AVLNode* tmp = node->left ? node->left : node->right;
- if(!tmp) { // If node hasn't child
- tmp = node;
- node = node->parent;
- tmp->unpin(self);
- result = tmp;
- } else { // If node has 1 child
- node->swapPosition(*tmp, self);
- node->unpin(self);
- result = node;
- node = tmp; // For balancing
- }
- break;
- } else { // If node has 2 childs (Test it!!!)
- // Change the position of the node
- // to be deleted whith the position
- // of the leftmost node in th right branch
- AVLNode* tmp = minKeyNode(node->right);
- node->swapPosition(*tmp, self);
- continue;
- }
- break;
- }
- }
- while(node) { // Balancing
- node->depth = 1 + max(depth(node->left), depth(node->right));
- i64 balance = getBalance(node);
- if (balance > 1 && getBalance(root->left) >= 0) {
- rotateRight(node);
- } elif (balance > 1 && getBalance(root->left) < 0) {
- rotateLeft(node->left);
- rotateRight(node);
- } elif (balance < -1 && getBalance(node->right) <= 0) {
- rotateLeft(node);
- } elif (balance < -1 && getBalance(node->right) > 0) {
- rotateRight(node->right);
- rotateLeft(node);
- }
- node = node->parent;
- }
- return result;
- }
- AVLNode* AVLTree::extract(AVLNode* node) {
- AVLNode* result = node;
- forever if(!node->left || !node->right) {
- AVLNode* tmp = node->left ? node->left : node->right;
- if(!tmp) { // If node hasn't child
- tmp = node;
- node = nullptr;
- tmp->unpin(self);
- result = tmp;
- } else { // If node has 1 child
- node->swapPosition(*tmp, self);
- node->unpin(self);
- result = node;
- node = tmp; // For balancing
- }
- break;
- } else { // If node has 2 childs (Test it!!!)
- // Change the position of the node
- // to be deleted whith the position
- // of the leftmost node in th right branch
- AVLNode* tmp = minKeyNode(node->right);
- node->swapPosition(*tmp, self);
- continue;
- }
- while(node) { // Balancing
- node->depth = 1 + max(depth(node->left), depth(node->right));
- i64 balance = getBalance(node);
- if (balance > 1 && getBalance(root->left) >= 0) {
- rotateRight(node);
- } elif (balance > 1 && getBalance(root->left) < 0) {
- rotateLeft(node->left);
- rotateRight(node);
- } elif (balance < -1 && getBalance(node->right) <= 0) {
- rotateLeft(node);
- } elif (balance < -1 && getBalance(node->right) > 0) {
- rotateRight(node->right);
- rotateLeft(node);
- }
- node = node->parent;
- }
- return result;
- }
- AVLNode* AVLTree::get(KeyValue key) const {
- AVLNode* node = root;
- forever {
- // If node isn't finded
- if(!node) return nullptr;
- // Searching key...
- auto cmp = key.getCompare(node->key);
- if(cmp == KeyValue::lower) {node = node->left; continue;}
- elif(cmp == KeyValue::highter) {node = node->left; continue;}
- else return node; // Node is finded
- }
- }
- AVLTree::Iterator AVLTree::begin() noexcept {return minKeyNode();}
- AVLTree::Iterator AVLTree::end() noexcept {return nullptr;}
- AVLTree::ReverseIterator AVLTree::rbegin() noexcept {return maxKeyNode();}
- AVLTree::ReverseIterator AVLTree::rend() noexcept {return nullptr;}
- AVLTree::ConstIterator AVLTree::cbegin() const noexcept {return minKeyNode();}
- AVLTree::ConstIterator AVLTree::cend() const noexcept {return nullptr;}
- AVLTree::ConstReverseIterator AVLTree::crbegin() const noexcept {return maxKeyNode();}
- AVLTree::ConstReverseIterator AVLTree::crend() const noexcept {return nullptr;}
- void AVLTree::clear(std::function<void(AVLNode*)> destructor) {
- AVLNode* node = minKeyNode();
- if(!node) return;
- if(node->hasRight()) node = node->right;
- while(node) {
- AVLNode* last = node;
- if(!!node->parent) {
- if(node->isLeft() && node->parent->hasRight()) {
- node = minKeyNode(node->parent->right);
- if(node->hasRight()) node = node->right;
- } elif(node->isLeft() || node->isRight())
- node = node->parent;
- } else node = nullptr;
- destructor(last);
- }
- root = nullptr;
- }
- AVLTree::Iterator::Iterator(AVLNode* node) : node(node) {}
- AVLTree::Iterator::Iterator(const Iterator& other) : node(other.node) {}
- AVLTree::Iterator::Iterator(Iterator&& other) : node(other.node) {}
- AVLTree::Iterator& AVLTree::Iterator::operator=(Iterator& other) noexcept {return *new(this) Iterator(other);}
- AVLTree::Iterator& AVLTree::Iterator::operator=(Iterator&& other) noexcept {return *new(this) Iterator(std::move(other));}
- AVLTree::Iterator& AVLTree::Iterator::operator=(AVLNode* node) noexcept {return *new(this) Iterator(node);}
- AVLTree::Iterator& AVLTree::Iterator::operator=(AVLNode& node) noexcept {return *new(this) Iterator(&node);}
- AVLTree::Iterator& AVLTree::Iterator::operator++() {
- if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
- elif(node->isLeft()) node = node->parent;
- elif(node->isRight()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isRight() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- AVLTree::Iterator& AVLTree::Iterator::operator--() {
- if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
- elif(node->isRight()) node = node->parent;
- elif(node->isLeft()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- AVLTree::Iterator AVLTree::Iterator::operator++(int) {Iterator tmp(self); ++self; return tmp;}
- AVLTree::Iterator AVLTree::Iterator::operator--(int) {Iterator tmp(self); --self; return tmp;}
- const AVLTree::Iterator& AVLTree::Iterator::operator++() const {
- if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
- elif(node->isLeft()) node = node->parent;
- elif(node->isRight()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isRight() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- const AVLTree::Iterator& AVLTree::Iterator::operator--() const {
- if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
- elif(node->isRight()) node = node->parent;
- elif(node->isLeft()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- const AVLTree::Iterator AVLTree::Iterator::operator++(int) const {Iterator tmp(self); ++self; return tmp;}
- const AVLTree::Iterator AVLTree::Iterator::operator--(int) const {Iterator tmp(self); --self; return tmp;}
- bool AVLTree::Iterator::operator==(Iterator other) const noexcept {return node == other.node;}
- bool AVLTree::Iterator::operator!=(Iterator other) const noexcept {return node != other.node;}
- AVLNode& AVLTree::Iterator::operator*() {return *node;}
- AVLNode* AVLTree::Iterator::operator->() {return node;}
- const AVLNode& AVLTree::Iterator::operator*() const {return *node;}
- const AVLNode* AVLTree::Iterator::operator->() const {return node;}
- AVLTree::ReverseIterator::ReverseIterator(AVLNode* node) : node(node) {}
- AVLTree::ReverseIterator::ReverseIterator(const ReverseIterator& other) : node(other.node) {}
- AVLTree::ReverseIterator::ReverseIterator(ReverseIterator&& other) : node(other.node) {}
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(ReverseIterator& other) noexcept {return *new(this) ReverseIterator(other);}
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(ReverseIterator&& other) noexcept {return *new(this) ReverseIterator(std::move(other));}
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(AVLNode* node) noexcept {return *new(this) ReverseIterator(node);}
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator=(AVLNode& node) noexcept {return *new(this) ReverseIterator(&node);}
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator++() {
- if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
- elif(node->isRight()) node = node->parent;
- elif(node->isLeft()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator--() {
- if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
- elif(node->isLeft()) node = node->parent;
- elif(node->isRight()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isRight() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- AVLTree::ReverseIterator AVLTree::ReverseIterator::operator++(int) {ReverseIterator tmp(self); ++self; return tmp;}
- AVLTree::ReverseIterator AVLTree::ReverseIterator::operator--(int) {ReverseIterator tmp(self); --self; return tmp;}
- const AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator++() const {
- if(node->hasLeft()) node = AVLTree::maxKeyNode(node->left);
- elif(node->isRight()) node = node->parent;
- elif(node->isLeft()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isLeft() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- const AVLTree::ReverseIterator& AVLTree::ReverseIterator::operator--() const {
- if(node->hasRight()) node = AVLTree::minKeyNode(node->right);
- elif(node->isLeft()) node = node->parent;
- elif(node->isRight()) {
- AVLNode* tmp = node->parent;
- node = node->parent->parent;
- while (tmp->isRight() && node) {tmp = node; node = node->parent;}
- } elif(node->isRoot()) node = nullptr;
- return *this;
- }
- const AVLTree::ReverseIterator AVLTree::ReverseIterator::operator++(int) const {ReverseIterator tmp(self); ++self; return tmp;}
- const AVLTree::ReverseIterator AVLTree::ReverseIterator::operator--(int) const {ReverseIterator tmp(self); --self; return tmp;}
- bool AVLTree::ReverseIterator::operator==(ReverseIterator other) const noexcept {return node == other.node;}
- bool AVLTree::ReverseIterator::operator!=(ReverseIterator other) const noexcept {return node != other.node;}
- AVLNode& AVLTree::ReverseIterator::operator*() {return *node;}
- AVLNode* AVLTree::ReverseIterator::operator->() {return node;}
- const AVLNode& AVLTree::ReverseIterator::operator*() const {return *node;}
- const AVLNode* AVLTree::ReverseIterator::operator->() const {return node;}
|