Skip to content
Snippets Groups Projects
Commit e4e8dccc authored by Jonas Seidel's avatar Jonas Seidel
Browse files

debugging BiasedBinaryNode::split_at

parent a787f2a0
No related branches found
No related tags found
No related merge requests found
......@@ -40,7 +40,6 @@ bool BiasedBinaryNode<T>::is_left_child(){
return this->me() == parent_lock->left_child();
}
template <typename T>
BiasedBinaryNode<T>::BiasedBinaryNode(unsigned long rank) : _rank(rank){}
......@@ -51,11 +50,6 @@ BiasedBinaryNode<T>::~BiasedBinaryNode(){
template <typename T>
void BiasedBinaryNode<T>::rewire_right_child_with(std::shared_ptr<BiasedBinaryNode<T>> new_child){
#ifdef DEBUG
if(this->right_child() != nullptr){
std::cerr << "\033[31mWarning: Some Nodes may have been orphaned\033[39m" << std::endl;
}
#endif
if(new_child == nullptr){
this->right_child().reset();
}else{
......@@ -75,9 +69,6 @@ void BiasedBinaryNode<T>::rewire_right_child_with(std::shared_ptr<BiasedBinaryNo
template <typename T>
void BiasedBinaryNode<T>::rewire_left_child_with(std::shared_ptr<BiasedBinaryNode<T>> new_child){
if(this->left_child() != nullptr){
#ifdef DEBUG
std::cerr << "\033[31mWarning: Some Nodes may have been orphaned\033[39m" << std::endl;
#endif
this->left_child()->parent().reset();
}
if(new_child == nullptr){
......@@ -116,7 +107,7 @@ std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::tilt_left(){
return this->me();
}else if(this->right_child()->rank() == this->rank() && this->left_child()->rank() < this->rank()){
#ifdef PROTOCOL
std::cout << "executing left rotation" << std::endl;
std::cout << "\033[1;32mLeft Rotation applies\033[0m" << std::endl;
#endif
std::weak_ptr<BiasedBinaryNode<T>> old_parent = this->parent();
std::shared_ptr<BiasedBinaryNode<T>> new_root = this->right_child();
......@@ -135,7 +126,7 @@ std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::tilt_right(){
return this->me();
}else if(this->right_child()->rank() < this->rank() && this->left_child()->rank() == this->rank()){
#ifdef PROTOCOL
std::cout << "executing right rotation" << std::endl;
std::cout << "\033[1;32mRight Rotation applies\033[0m" << std::endl;
#endif
std::weak_ptr<BiasedBinaryNode<T>> old_parent = this->parent();
std::shared_ptr<BiasedBinaryNode<T>> new_root = this->left_child();
......@@ -144,10 +135,7 @@ std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::tilt_right(){
new_root->parent() = old_parent;
return new_root;
}
#ifdef DEBUG
std::cerr << "\033[31mError: no case applicable\033[39m" << std::endl;
assert(1==0);
#endif
return this->me();
}
template <typename T>
......@@ -169,52 +157,79 @@ std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::local_join(std::shared
return new_common_parent;
}else if(this->rank() > other->rank() && !this->is_leaf()){
std::shared_ptr<BiasedBinaryNode<T>> temporary = this->tilt_left();
this->rewire_right_child_with(temporary.right_child() == nullptr ? other : &temporary.right_child()->local_join(other));
this->rewire_right_child_with(temporary->right_child() == nullptr ? other : temporary->right_child()->local_join(other));
return temporary;
}else
#ifdef DEBUG
if(this->rank() < other->rank() && !other->is_leaf())
#endif
{
BiasedBinaryNode<T> temporary = other.tilt_right();
other->rewire_left_child_with(temporary.left_child() == nullptr ? other : &temporary.left_child()->local_join(this));
std::shared_ptr<BiasedBinaryNode<T>> temporary = other->tilt_right();
other->rewire_left_child_with(temporary->left_child() == nullptr ? other : temporary->left_child()->local_join(this->me()));
return temporary;
}
#ifdef DEBUG
else{
std::cerr << "\033[31mError: no case applicable\033[39m" << std::endl;
assert(0==1);
assert(false);
}
#endif
}
template <typename T>
void BiasedBinaryNode<T>::split_at(BiasedBinaryNode<T>& split_pos,std::shared_ptr<BiasedBinaryNode<T>> before, std::shared_ptr<BiasedBinaryNode<T>> after){
void BiasedBinaryNode<T>::split_at(std::shared_ptr<BiasedBinaryNode<T>> split_pos, std::shared_ptr<BiasedBinaryNode<T>>& before, std::shared_ptr<BiasedBinaryNode<T>>& after){
#ifdef PROTOCOL
std::cout << "\033[36mSplitting\033[0m" << std::endl;
std::cout << *this;
std::cout << "at" << std::endl;
std::cout << *split_pos;
std::cout << "State: \nbefore:" << std::endl;
if(before == nullptr){
std::cout << "nullptr" << std::endl;
}else{
std::cout << *before;
}
std::cout << "after:" << std::endl;
if(before == nullptr){
std::cout << "nullptr" << std::endl;
}else{
std::cout << *before;
}
#endif
#ifdef DEBUG
assert(split_pos.left_child() == nullptr && split_pos.right_child() == nullptr);
assert(split_pos->left_child() == nullptr && split_pos->right_child() == nullptr);
#endif
std::shared_ptr<BiasedBinaryNode<T>> parent_lock = split_pos.parent().lock();
std::shared_ptr<BiasedBinaryNode<T>> parent_lock = split_pos->parent().lock();
if(parent_lock == nullptr){
return;
}else if(split_pos.is_left_child()){
}else if(split_pos->is_left_child()){
if(parent_lock->right_child() != nullptr){
after = parent_lock->right_child()->local_join(after);
after = parent_lock->right_child()->global_join(after);
after->parent().reset();
}
parent_lock->left_child() == nullptr;
}else{
if(parent_lock->left_child() != nullptr){
before = parent_lock->left_child()->local_join(before);
before = parent_lock->left_child()->global_join(before);
before->parent().reset();
}
parent_lock->right_child() == nullptr;
}
split_at(*parent_lock,before, after);
parent_lock->right_child().reset(); //orphan one side and disconnect the joined tree from the remaining nodes
parent_lock->left_child().reset();
split_pos.reset();
this->split_at(parent_lock, before, after);
}
template <typename T>
std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::global_join(std::shared_ptr<BiasedBinaryNode<T>> other){
#ifdef PROTOCOL
std::cout << *this << std::endl;
std::cout << *other << std::endl;
std::cout << "\033[36mJoin\033[0m" << std::endl;
std::cout << *this;
std::cout << "with" << std::endl;
if(other == nullptr){
std::cout << "nullptr" << std::endl;
}else{
std::cout << *other;
}
#endif
if(other == nullptr){
......@@ -256,6 +271,7 @@ std::shared_ptr<BiasedBinaryNode<T>> BiasedBinaryNode<T>::global_join(std::share
temporary_v = this->left_child();
}
std::shared_ptr<BiasedBinaryNode<T>> temporary_z = (temporary_u->right_child() == nullptr ? temporary_v->left_child() : temporary_u->right_child()->global_join(temporary_v->left_child()->me()));
temporary_z->parent().reset();
// we note that global_join is defined such that currently temporary_z may be is child of temporary_u->right_child()
if(temporary_z->rank() == this->rank()){
#ifdef PROTOCOL
......@@ -353,10 +369,10 @@ std::ostream& operator<<(std::ostream& os, BiasedBinaryNode<T>& root){
if(active_level == true) return next_level;
return std::queue<std::shared_ptr<BiasedBinaryNode<T>>>();
};
while(!current_level.empty()){
std::cout << current_level.size();
current_level = print_level(current_level);
while(!current_level.empty()){
os << std::endl;
current_level = print_level(current_level);
}
return os;
}
......
......@@ -25,10 +25,6 @@ private:
std::shared_ptr<BiasedBinaryNode<T>> _left_child;
std::shared_ptr<BiasedBinaryNode<T>> _right_child;
std::weak_ptr<BiasedBinaryNode<T>>& parent();
std::shared_ptr<BiasedBinaryNode<T>>& left_child();
std::shared_ptr<BiasedBinaryNode<T>>& right_child();
std::shared_ptr<BiasedBinaryNode<T>> tilt_right();
std::shared_ptr<BiasedBinaryNode<T>> tilt_left();
std::shared_ptr<BiasedBinaryNode<T>> local_join(std::shared_ptr<BiasedBinaryNode<T>>);
......@@ -43,11 +39,15 @@ public:
T& value();
size_t& rank();
std::weak_ptr<BiasedBinaryNode<T>>& parent();
std::shared_ptr<BiasedBinaryNode<T>>& left_child();
std::shared_ptr<BiasedBinaryNode<T>>& right_child();
void rewire_right_child_with(std::shared_ptr<BiasedBinaryNode<T>>);
void rewire_left_child_with(std::shared_ptr<BiasedBinaryNode<T>>);
void replace_with(std::shared_ptr<BiasedBinaryNode<T>>);
void split_at(BiasedBinaryNode<T>& split_pos,std::shared_ptr<BiasedBinaryNode<T>> before, std::shared_ptr<BiasedBinaryNode<T>> after);
void split_at(std::shared_ptr<BiasedBinaryNode<T>> split_pos,std::shared_ptr<BiasedBinaryNode<T>>& before, std::shared_ptr<BiasedBinaryNode<T>>& after);
std::shared_ptr<BiasedBinaryNode<T>> global_join(std::shared_ptr<BiasedBinaryNode<T>>);
};
......
......@@ -21,6 +21,15 @@ void BiasedBinaryTree<T>::join(BiasedBinaryTree<T> other){
other.root() = std::shared_ptr<BiasedBinaryNode<T>>();
}
template <typename T>
void BiasedBinaryTree<T>::split_at(std::shared_ptr<BiasedBinaryNode<T>> split_pos,BiasedBinaryTree<T>& before, BiasedBinaryTree<T>& after){
std::shared_ptr<BiasedBinaryNode<T>> root_before;
std::shared_ptr<BiasedBinaryNode<T>> root_after;
this->root()->split_at(split_pos, root_before, root_after);
before = BiasedBinaryTree<T>(root_before);
after = BiasedBinaryTree<T>(root_after);
}
template <typename T>
std::ostream& operator<<(std::ostream& os, BiasedBinaryTree<T>& tree){
if(tree.root() != nullptr) os << *tree.root();
......
......@@ -19,6 +19,7 @@ public:
std::shared_ptr<BiasedBinaryNode<T>>& root();
void join(BiasedBinaryTree<T>);
void split_at(std::shared_ptr<BiasedBinaryNode<T>>, BiasedBinaryTree<T>&, BiasedBinaryTree<T>&);
};
#include "biased_binary_tree.cpp"
......
......@@ -9,6 +9,7 @@ int main(){
start.join(BiasedBinaryTree(std::make_shared<BiasedBinaryNode<int>>(2)));
}
BiasedBinaryTree<int> second (std::make_shared<BiasedBinaryNode<int>>(5));
std::shared_ptr<BiasedBinaryNode<int>> split_pos = second.root();
for(int i = 1; i < 5; i++){
std::cout << "joining " << i << std::endl;
second.join(BiasedBinaryTree(std::make_shared<BiasedBinaryNode<int>>(i)));
......@@ -16,4 +17,14 @@ int main(){
start.join(second);
std::cout << "----------------" << std::endl;
std::cout << start;
BiasedBinaryTree<int> before;
BiasedBinaryTree<int> after;
std::cout << "-.-.-.-" << std::endl;
std::cout << "use_count : " << split_pos.use_count() << std::endl;
start.split_at(split_pos, before, after);
std::cout << "----------------" << std::endl;
std::cout << before;
std::cout << "----------------" << std::endl;
std::cout << after;
std::cout << split_pos;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment