2018-07-18 20:08:55 +02:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#ifdef DUMP
|
|
|
|
#include <iostream>
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef unsigned int Umword;
|
|
|
|
typedef int Mword;
|
|
|
|
|
|
|
|
template <typename Key>
|
|
|
|
class Bin_tree;
|
|
|
|
|
|
|
|
template <typename _Key = Umword>
|
|
|
|
struct Bin_tree_node {
|
2018-07-19 14:48:32 +02:00
|
|
|
public:
|
|
|
|
typedef _Key Key;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef Bin_tree_node<Key> Self;
|
|
|
|
typedef Bin_tree<Key> Tree;
|
|
|
|
|
2018-07-18 20:08:55 +02:00
|
|
|
private:
|
|
|
|
friend class Bin_tree<_Key>;
|
|
|
|
|
|
|
|
void recalc()
|
|
|
|
{
|
|
|
|
Umword leftHeight =
|
|
|
|
(left == nullptr) ? 0 : left->height + 1;
|
|
|
|
Umword rightHeight =
|
|
|
|
(right == nullptr) ? 0 : right->height + 1;
|
|
|
|
|
|
|
|
balance = leftHeight - rightHeight;
|
|
|
|
height = (leftHeight < rightHeight ? rightHeight : leftHeight);
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
void replaceChild(Self* child, Self* replace)
|
|
|
|
{
|
|
|
|
assert(child == left || child == right);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
if(child == left)
|
|
|
|
left = replace;
|
|
|
|
else if(child == right)
|
|
|
|
right = replace;
|
|
|
|
|
|
|
|
if(replace)
|
|
|
|
replace->parent = this;
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
child->parent = nullptr;
|
|
|
|
|
|
|
|
recalc();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-07-18 20:08:55 +02:00
|
|
|
constexpr Bin_tree_node(Key key)
|
|
|
|
: key(key), parent(nullptr), left(nullptr),
|
|
|
|
right(nullptr), tree(nullptr),
|
|
|
|
balance(0), height(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
constexpr Bin_tree_node()
|
|
|
|
: Bin_tree_node(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Self* parent;
|
|
|
|
Self* left;
|
|
|
|
Self* right;
|
|
|
|
Tree* tree;
|
|
|
|
|
|
|
|
// AVL Tree
|
|
|
|
Mword balance;
|
|
|
|
Umword height;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Key key;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Key = Umword>
|
|
|
|
class Bin_tree {
|
|
|
|
protected:
|
|
|
|
typedef Bin_tree_node<Key> Node;
|
|
|
|
private:
|
|
|
|
Node* root;
|
|
|
|
|
|
|
|
Node* findByKey(Key key) const
|
|
|
|
{
|
|
|
|
Node* iterparent = nullptr;
|
|
|
|
Node* iter = root;
|
|
|
|
|
|
|
|
while(iter != nullptr)
|
|
|
|
{
|
|
|
|
iterparent = iter;
|
|
|
|
|
|
|
|
if(key < iter->key) {
|
|
|
|
iter = iter->left;
|
|
|
|
} else if(key > iter->key) {
|
|
|
|
iter = iter->right;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* findLeft(Node* start) const
|
|
|
|
{
|
2018-07-19 14:48:32 +02:00
|
|
|
assert(start);
|
|
|
|
|
2018-07-18 20:08:55 +02:00
|
|
|
Node* iter = start;
|
|
|
|
while(iter->left != nullptr)
|
|
|
|
{
|
|
|
|
iter = iter->left;
|
|
|
|
}
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// void replaceInParent(Node* node, Node* replace) {
|
|
|
|
// assert(node->parent);
|
|
|
|
// assert(node->parent->left == node
|
|
|
|
// || node->parent->right == node);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// assert(node->parent);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// if(node->parent->left == node)
|
|
|
|
// node->parent->left = replace;
|
|
|
|
// else if(node->parent->right == node)
|
|
|
|
// node->parent->right = replace;
|
|
|
|
|
|
|
|
// if(replace)
|
|
|
|
// {
|
|
|
|
// replace->parent = node->parent;
|
|
|
|
// replace->recalc();
|
|
|
|
// }
|
|
|
|
// }
|
2018-07-18 20:08:55 +02:00
|
|
|
|
|
|
|
void leafRemove(Node* leaf)
|
|
|
|
{
|
2018-07-19 14:48:32 +02:00
|
|
|
assert(leaf);
|
2018-07-18 20:08:55 +02:00
|
|
|
assert(leaf->left == nullptr
|
|
|
|
|| leaf->right == nullptr);
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
Node* child;
|
|
|
|
|
2018-07-18 20:08:55 +02:00
|
|
|
if(leaf->left == nullptr && leaf->right == nullptr)
|
2018-07-19 14:48:32 +02:00
|
|
|
child = nullptr;
|
2018-07-18 20:08:55 +02:00
|
|
|
else if(leaf->left == nullptr)
|
2018-07-19 14:48:32 +02:00
|
|
|
child = leaf->right;
|
|
|
|
else if(leaf->right == nullptr)
|
|
|
|
child = leaf->left;
|
|
|
|
else
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
if(leaf->parent)
|
2018-07-18 20:08:55 +02:00
|
|
|
{
|
2018-07-19 14:48:32 +02:00
|
|
|
leaf->parent->replaceChild(leaf, child);
|
|
|
|
rebalance(leaf->parent, 1);
|
2018-07-18 20:08:55 +02:00
|
|
|
}
|
2018-07-19 14:48:32 +02:00
|
|
|
else
|
2018-07-18 20:08:55 +02:00
|
|
|
{
|
2018-07-19 14:48:32 +02:00
|
|
|
root = child;
|
|
|
|
if(child)
|
|
|
|
child->parent = nullptr;
|
2018-07-18 20:08:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// if(leaf->left == nullptr && leaf->right == nullptr)
|
|
|
|
// {
|
|
|
|
// if(leaf->parent != nullptr)
|
|
|
|
// {
|
|
|
|
// replaceInParent(leaf, nullptr);
|
|
|
|
// rebalance(leaf->parent, 1, 2);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// root = nullptr;
|
|
|
|
// }
|
|
|
|
// else if(leaf->left == nullptr)
|
|
|
|
// {
|
|
|
|
// if(leaf->parent != nullptr)
|
|
|
|
// {
|
|
|
|
// replaceInParent(leaf, leaf->right);
|
|
|
|
// rebalance(leaf->parent, 1, 2);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// root = leaf->right;
|
|
|
|
// leaf->right->parent = nullptr;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// else if(leaf->right == nullptr)
|
|
|
|
// {
|
|
|
|
// if(leaf->parent != nullptr)
|
|
|
|
// {
|
|
|
|
// replaceInParent(leaf, leaf->left);
|
|
|
|
// rebalance(leaf->parent, 1, 2);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// root = leaf->left;
|
|
|
|
// leaf->left->parent = nullptr;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2018-07-18 20:08:55 +02:00
|
|
|
leaf->parent = nullptr;
|
|
|
|
leaf->left = nullptr;
|
|
|
|
leaf->right = nullptr;
|
|
|
|
leaf->tree = nullptr;
|
|
|
|
|
|
|
|
leaf->recalc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-07-19 14:48:32 +02:00
|
|
|
rebalance(Node* start, Mword abortCond)
|
2018-07-18 20:08:55 +02:00
|
|
|
{
|
|
|
|
for(Node* iter = start; iter != nullptr; iter = iter->parent)
|
|
|
|
{
|
|
|
|
iter->recalc();
|
|
|
|
|
|
|
|
Mword absBalance =
|
|
|
|
(iter->balance < 0) ? -(iter->balance) : iter->balance;
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
if(absBalance == 2) // needs rotation
|
2018-07-18 20:08:55 +02:00
|
|
|
{
|
|
|
|
// Left side is taller
|
|
|
|
if(iter->balance > 0)
|
|
|
|
{
|
|
|
|
// Left side's right side is taller
|
|
|
|
if(iter->left && iter->left->balance < 0)
|
|
|
|
{
|
|
|
|
rotateLeft(iter->left);
|
|
|
|
}
|
|
|
|
rotateRight(iter);
|
|
|
|
}
|
|
|
|
// Right side is taller
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Right side's left side is taller
|
|
|
|
if(iter->right && iter->right->balance > 0)
|
|
|
|
{
|
|
|
|
rotateRight(iter->right);
|
|
|
|
}
|
|
|
|
rotateLeft(iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// then abort
|
|
|
|
break;
|
|
|
|
}
|
2018-07-19 14:48:32 +02:00
|
|
|
else if(absBalance == abortCond) // can abort
|
|
|
|
break;
|
2018-07-18 20:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rotateLeft(Node* node) {
|
|
|
|
assert(node);
|
|
|
|
assert(node->right);
|
|
|
|
|
|
|
|
Node* partner = node->right;
|
|
|
|
|
|
|
|
node->right = partner->left;
|
|
|
|
|
|
|
|
if(node->right)
|
|
|
|
node->right->parent = node;
|
|
|
|
|
|
|
|
// No parent, partner is the new root
|
|
|
|
if(node->parent == nullptr)
|
2018-07-19 14:48:32 +02:00
|
|
|
{
|
|
|
|
root = partner;
|
|
|
|
partner->parent = nullptr;
|
|
|
|
}
|
2018-07-18 20:08:55 +02:00
|
|
|
else
|
2018-07-19 14:48:32 +02:00
|
|
|
node->parent->replaceChild(node, partner);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// partner->parent = node->parent;
|
2018-07-18 20:08:55 +02:00
|
|
|
node->parent = partner;
|
|
|
|
partner->left = node;
|
|
|
|
|
|
|
|
assert(node->parent == nullptr
|
|
|
|
|| node->parent->left == node
|
|
|
|
|| node->parent->right == node);
|
|
|
|
|
|
|
|
assert(partner->parent == nullptr
|
|
|
|
|| partner->parent->left == partner
|
|
|
|
|| partner->parent->right == partner);
|
|
|
|
|
|
|
|
assert(node->left == nullptr
|
|
|
|
|| node->left->parent == node);
|
|
|
|
|
|
|
|
assert(node->right == nullptr
|
|
|
|
|| node->right->parent == node);
|
|
|
|
|
|
|
|
assert(partner->left == nullptr
|
|
|
|
|| partner->left->parent == partner);
|
|
|
|
|
|
|
|
assert(partner->right == nullptr
|
|
|
|
|| partner->right->parent == partner);
|
|
|
|
|
|
|
|
node->recalc();
|
|
|
|
partner->recalc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void rotateRight(Node* node) {
|
|
|
|
assert(node);
|
|
|
|
assert(node->left);
|
|
|
|
|
|
|
|
Node* partner = node->left;
|
|
|
|
|
|
|
|
node->left = partner->right;
|
|
|
|
|
|
|
|
if(node->left)
|
|
|
|
node->left->parent = node;
|
|
|
|
|
|
|
|
// No parent, partner is the new root
|
|
|
|
if(node->parent == nullptr)
|
2018-07-19 14:48:32 +02:00
|
|
|
{
|
|
|
|
root = partner;
|
|
|
|
partner->parent = nullptr;
|
|
|
|
}
|
2018-07-18 20:08:55 +02:00
|
|
|
else
|
2018-07-19 14:48:32 +02:00
|
|
|
node->parent->replaceChild(node,partner);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// partner->parent = node->parent;
|
2018-07-18 20:08:55 +02:00
|
|
|
node->parent = partner;
|
|
|
|
|
|
|
|
partner->right = node;
|
|
|
|
|
|
|
|
assert(node->parent == nullptr
|
|
|
|
|| node->parent->left == node
|
|
|
|
|| node->parent->right == node);
|
|
|
|
|
|
|
|
assert(partner->parent == nullptr
|
|
|
|
|| partner->parent->left == partner
|
|
|
|
|| partner->parent->right == partner);
|
|
|
|
|
|
|
|
assert(node->left == nullptr
|
|
|
|
|| node->left->parent == node);
|
|
|
|
|
|
|
|
assert(node->right == nullptr
|
|
|
|
|| node->right->parent == node);
|
|
|
|
|
|
|
|
assert(partner->left == nullptr
|
|
|
|
|| partner->left->parent == partner);
|
|
|
|
|
|
|
|
assert(partner->right == nullptr
|
|
|
|
|| partner->right->parent == partner);
|
|
|
|
|
|
|
|
node->recalc();
|
|
|
|
partner->recalc();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DUMP
|
|
|
|
void dump(Node* node, int indent = 0)
|
|
|
|
{
|
|
|
|
if(node == nullptr) return;
|
|
|
|
assert(node->tree == this);
|
|
|
|
|
|
|
|
assert(node->left == nullptr
|
|
|
|
|| node->left->parent == node);
|
|
|
|
dump(node->left, indent+1);
|
|
|
|
for(int i = 0; i < indent; i++)
|
|
|
|
cout << " ";
|
|
|
|
cout << node->key << endl;
|
|
|
|
|
|
|
|
assert(node->right == nullptr
|
|
|
|
|| node->right->parent == node);
|
|
|
|
dump(node->right, indent+1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr Bin_tree() : root(nullptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool insert(Node* node)
|
|
|
|
{
|
|
|
|
assert(node->tree == nullptr);
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
Node* parent = nullptr;
|
2018-07-18 20:08:55 +02:00
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
if(root == nullptr)
|
|
|
|
// Tree is empty, add node as root
|
|
|
|
root = node;
|
2018-07-18 20:08:55 +02:00
|
|
|
else
|
|
|
|
{
|
2018-07-19 14:48:32 +02:00
|
|
|
// else, find a suitable parent
|
|
|
|
parent = findByKey(node->key);
|
|
|
|
assert(parent);
|
|
|
|
|
|
|
|
if(node->key < parent->key)
|
|
|
|
parent->left = node;
|
|
|
|
else if(node->key > parent->key)
|
|
|
|
parent->right = node;
|
|
|
|
else // Node with same key exists
|
|
|
|
return false;
|
2018-07-18 20:08:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// setup node
|
2018-07-18 20:08:55 +02:00
|
|
|
node->parent = parent;
|
|
|
|
node->left = nullptr;
|
|
|
|
node->right = nullptr;
|
|
|
|
node->tree = this;
|
|
|
|
node->height = 0;
|
|
|
|
node->balance = 0;
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
// rebalance tree
|
|
|
|
if(parent)
|
|
|
|
rebalance(parent,0);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove(Node* node)
|
|
|
|
{
|
|
|
|
assert(node);
|
|
|
|
assert(node->tree == this);
|
|
|
|
|
|
|
|
if(node->left == nullptr || node->right == nullptr)
|
|
|
|
{
|
|
|
|
leafRemove(node);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* replacement = findLeft(node->right);
|
|
|
|
|
|
|
|
leafRemove(replacement);
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
if(node->parent == nullptr)
|
2018-07-18 20:08:55 +02:00
|
|
|
{
|
|
|
|
root = replacement;
|
|
|
|
replacement->parent = nullptr;
|
|
|
|
}
|
|
|
|
else
|
2018-07-19 14:48:32 +02:00
|
|
|
node->parent->replaceChild(node, replacement);
|
2018-07-18 20:08:55 +02:00
|
|
|
|
|
|
|
replacement->left = node->left;
|
|
|
|
replacement->right = node->right;
|
|
|
|
replacement->tree = this;
|
|
|
|
|
|
|
|
if(node->left)
|
|
|
|
node->left->parent = replacement;
|
|
|
|
|
|
|
|
if(node->right)
|
|
|
|
node->right->parent = replacement;
|
|
|
|
|
|
|
|
replacement->recalc();
|
|
|
|
|
|
|
|
node->left = nullptr;
|
|
|
|
node->right = nullptr;
|
|
|
|
node->parent = nullptr;
|
|
|
|
node->tree = nullptr;
|
2018-07-19 14:48:32 +02:00
|
|
|
node->recalc();
|
2018-07-18 20:08:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
Node* lookup(Key key) const
|
|
|
|
{
|
|
|
|
if(root == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-07-18 20:08:55 +02:00
|
|
|
Node* node = findByKey(key);
|
|
|
|
|
2018-07-19 14:48:32 +02:00
|
|
|
if(node == nullptr)
|
|
|
|
__builtin_unreachable();
|
|
|
|
|
|
|
|
if(node->key != key)
|
2018-07-18 20:08:55 +02:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DUMP
|
|
|
|
void dump()
|
|
|
|
{
|
|
|
|
cout << "---------------------------------------------" << endl;
|
|
|
|
dump(root);
|
|
|
|
cout << "---------------------------------------------" << endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, typename Key = typename T::Key>
|
|
|
|
class Bin_tree_t : Bin_tree<Key> {
|
|
|
|
private:
|
|
|
|
typedef Bin_tree<Key> Base;
|
|
|
|
public:
|
|
|
|
inline
|
|
|
|
bool insert(T* node)
|
|
|
|
{ return Base::insert(static_cast<typename Base::Node*>(node)); }
|
|
|
|
|
|
|
|
inline
|
|
|
|
void remove(T* node)
|
|
|
|
{ Base::remove(static_cast<typename Base::Node*>(node)); }
|
|
|
|
|
|
|
|
inline
|
|
|
|
T* lookup(Key key)
|
|
|
|
{ return static_cast<T*>(Base::lookup(key)); }
|
|
|
|
|
|
|
|
#ifdef DUMP
|
|
|
|
inline
|
|
|
|
void dump()
|
|
|
|
{ Base::dump(); }
|
|
|
|
#endif
|
|
|
|
};
|