Skip to content

Commit

Permalink
RBT: add method to swap nodes position in tree
Browse files Browse the repository at this point in the history
  • Loading branch information
shuLhan committed Jun 2, 2017
1 parent c0c07e2 commit ae87136
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
61 changes: 61 additions & 0 deletions RBT.cc
Expand Up @@ -129,6 +129,67 @@ const char* RBT::chars()
return __str;
}

/**
* `swap_node(x, y)` will swap the parent, left, and right pointer of node `x`
* with `y` in tree.
*/
void RBT::swap_node(TreeNode* x, TreeNode* y)
{
TreeNode* xparent = x->get_parent();
TreeNode* xleft = x->get_left();
TreeNode* xright = x->get_right();

TreeNode* yparent = y->get_parent();
TreeNode* yleft = y->get_left();
TreeNode* yright = y->get_right();

if (xparent) {
if (x->is_left_of(xparent)) {
xparent->set_left(y);
} else {
xparent->set_right(y);
}
}
if (xleft) {
xleft->set_parent(y);
}
if (xright) {
xright->set_parent(y);
}

x->set_parent(yparent);
x->set_left(yleft);
x->set_right(yright);

if (yparent) {
if (y->is_left_of(yparent)) {
yparent->set_left(x);
} else {
yparent->set_right(x);
}
}
if (yleft) {
yleft->set_parent(x);
}
if (yright) {
yright->set_parent(x);
}

y->set_parent(xparent);
y->set_left(xleft);
y->set_right(xright);

int xattr = x->_attr;
x->_attr = y->_attr;
y->_attr = xattr;

if (_root == x) {
_root = y;
} else if (_root == y) {
_root = x;
}
}

void RBT::swap_content(TreeNode* x, TreeNode* y)
{
Object* ox = x->get_content();
Expand Down
1 change: 1 addition & 0 deletions RBT.hh
Expand Up @@ -23,6 +23,7 @@ public:
virtual ~RBT();

const char* chars();
void swap_node(TreeNode* x, TreeNode* y);
void swap_content(TreeNode* x, TreeNode* y);
const char* get_red_node_chars();

Expand Down

0 comments on commit ae87136

Please sign in to comment.