Several splay tree fixes.
This commit is contained in:
parent
faaaa1ef38
commit
bded1b74cc
1 changed files with 33 additions and 38 deletions
|
@ -379,7 +379,7 @@ splay_node_t *splay_insert(splay_tree_t *tree, void *data) {
|
||||||
new->data = data;
|
new->data = data;
|
||||||
splay_insert_top(tree, new);
|
splay_insert_top(tree, new);
|
||||||
} else {
|
} else {
|
||||||
closest = splay_search_closest_node_nosplay(tree, data, &result);
|
closest = splay_search_closest_node(tree, data, &result);
|
||||||
|
|
||||||
if(!result)
|
if(!result)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -403,7 +403,7 @@ splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
|
||||||
if(!tree->root)
|
if(!tree->root)
|
||||||
splay_insert_top(tree, node);
|
splay_insert_top(tree, node);
|
||||||
else {
|
else {
|
||||||
closest = splay_search_closest_node_nosplay(tree, node->data, &result);
|
closest = splay_search_closest_node(tree, node->data, &result);
|
||||||
|
|
||||||
if(!result)
|
if(!result)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -418,7 +418,7 @@ splay_node_t *splay_insert_node(splay_tree_t *tree, splay_node_t *node) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
|
void splay_insert_top(splay_tree_t *tree, splay_node_t *node) {
|
||||||
node->prev = node->next = node->parent = NULL;
|
node->prev = node->next = node->left = node->right = node->parent = NULL;
|
||||||
tree->head = tree->tail = tree->root = node;
|
tree->head = tree->tail = tree->root = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -432,23 +432,22 @@ void splay_insert_before(splay_tree_t *tree, splay_node_t *before, splay_node_t
|
||||||
}
|
}
|
||||||
|
|
||||||
node->next = before;
|
node->next = before;
|
||||||
node->parent = before;
|
if((node->prev = before->prev))
|
||||||
node->prev = before->prev;
|
|
||||||
|
|
||||||
if(before->left) {
|
|
||||||
splay_insert_after(tree, before->prev, node);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(before->prev)
|
|
||||||
before->prev->next = node;
|
before->prev->next = node;
|
||||||
else
|
else
|
||||||
tree->head = node;
|
tree->head = node;
|
||||||
|
|
||||||
before->prev = node;
|
before->prev = node;
|
||||||
before->left = node;
|
|
||||||
|
|
||||||
splay_bottom_up(tree, node);
|
splay_bottom_up(tree, before);
|
||||||
|
|
||||||
|
node->right = before;
|
||||||
|
before->parent = node;
|
||||||
|
if((node->left = before->left))
|
||||||
|
before->left->parent = node;
|
||||||
|
before->left = NULL;
|
||||||
|
|
||||||
|
node->parent = NULL;
|
||||||
|
tree->root = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
|
void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *node) {
|
||||||
|
@ -460,33 +459,31 @@ void splay_insert_after(splay_tree_t *tree, splay_node_t *after, splay_node_t *n
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(after->right) {
|
|
||||||
splay_insert_before(tree, after->next, node);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->prev = after;
|
node->prev = after;
|
||||||
node->parent = after;
|
if((node->next = after->next))
|
||||||
node->next = after->next;
|
|
||||||
|
|
||||||
if(after->next)
|
|
||||||
after->next->prev = node;
|
after->next->prev = node;
|
||||||
else
|
else
|
||||||
tree->tail = node;
|
tree->tail = node;
|
||||||
|
|
||||||
after->next = node;
|
after->next = node;
|
||||||
after->right = node;
|
|
||||||
|
|
||||||
splay_bottom_up(tree, node);
|
splay_bottom_up(tree, after);
|
||||||
|
|
||||||
|
node->left = after;
|
||||||
|
after->parent = node;
|
||||||
|
if((node->right = after->right))
|
||||||
|
after->right->parent = node;
|
||||||
|
after->right = NULL;
|
||||||
|
|
||||||
|
node->parent = NULL;
|
||||||
|
tree->root = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
|
splay_node_t *splay_unlink(splay_tree_t *tree, void *data) {
|
||||||
splay_node_t *node;
|
splay_node_t *node;
|
||||||
int result;
|
|
||||||
|
|
||||||
node = splay_search_closest_node_nosplay(tree, data, &result);
|
node = splay_search_node(tree, data);
|
||||||
|
|
||||||
if(node && !result)
|
if(node)
|
||||||
splay_unlink_node(tree, node);
|
splay_unlink_node(tree, node);
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
|
@ -503,18 +500,16 @@ void splay_unlink_node(splay_tree_t *tree, splay_node_t *node) {
|
||||||
else
|
else
|
||||||
tree->tail = node->prev;
|
tree->tail = node->prev;
|
||||||
|
|
||||||
if(node->left) {
|
splay_bottom_up(tree, node);
|
||||||
|
|
||||||
|
if(node->prev) {
|
||||||
node->left->parent = NULL;
|
node->left->parent = NULL;
|
||||||
tree->root = node->left;
|
tree->root = node->left;
|
||||||
|
if((node->prev->right = node->right))
|
||||||
if(node->right) {
|
|
||||||
splay_bottom_up(tree, node->prev);
|
|
||||||
node->prev->right = node->right;
|
|
||||||
node->right->parent = node->prev;
|
node->right->parent = node->prev;
|
||||||
}
|
} else if(node->next) {
|
||||||
} else if(node->right) {
|
|
||||||
node->right->parent = NULL;
|
|
||||||
tree->root = node->right;
|
tree->root = node->right;
|
||||||
|
node->right->parent = NULL;
|
||||||
} else {
|
} else {
|
||||||
tree->root = NULL;
|
tree->root = NULL;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue