pj_rbtree_node* pj_rbtree_erase( pj_rbtree *tree, pj_rbtree_node *node ) { pj_rbtree_node *succ; pj_rbtree_node *null = tree->null; pj_rbtree_node *child; pj_rbtree_node *parent; PJ_CHECK_STACK(); if (node->left == null || node->right == null) { succ = node; } else { for (succ=node->right; succ->left!=null; succ=succ->left) /* void */; } child = succ->left != null ? succ->left : succ->right; parent = succ->parent; child->parent = parent; if (parent != null) { if (parent->left == succ) parent->left = child; else parent->right = child; } else tree->root = child; if (succ != node) { succ->parent = node->parent; succ->left = node->left; succ->right = node->right; succ->color = node->color; parent = node->parent; if (parent != null) { if (parent->left==node) parent->left=succ; else parent->right=succ; } if (node->left != null) node->left->parent = succ;; if (node->right != null) node->right->parent = succ; if (tree->root == node) tree->root = succ; } if (succ->color == PJ_RBCOLOR_BLACK) { if (child != null) delete_fixup(tree, child); tree->null->color = PJ_RBCOLOR_BLACK; } --tree->size; return node; }
/* * tree_delete -- * Delete the item whose key is equal to 'key' from the tree. * See section 13.4, "Introduction to Algorithms", 2/e for the * algorithm. */ void tree_delete(T tree, Key key) { assert(tree); Node *x, *y, *z; /* * find the to-be-deleted node z */ z = search(tree, key); if (z == NIL(tree)) return; /* * y is the spliced out node */ if ((z->left == NIL(tree)) || (z->right == NIL(tree))) y = z; else y = successor(tree, z); /* * Maintain the size field of each node on the path from y to the * root. */ Node *p = y->parent; while (p != NIL(tree)) { p->size--; p = p->parent; } /* * link y's only child to y's parent */ if (y->left != NIL(tree)) x = y->left; else x = y->right; x->parent = y->parent; if (y->parent == NIL(tree)) // we are deleting the root tree->root = x; else if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; /* if y is not z copy satellite data */ if (y != z) z->item = y->item; /* maintain red-black property if necessary */ if (y->color == BLACK) delete_fixup(tree, x); free(y); }
void * rbtree_delete(struct rbtree * tree, void * key) { struct rbtree_node * node = find_node(tree, key); if(node == NULL) return NULL; void * retval = node->data; struct rbtree_node * z = node; struct rbtree_node * y = z; struct rbtree_node * x; int y_orig_color = y->color; if(z->left == tree->nil) { x = z->right; transplant(tree, z, z->right); } else if(z->right == tree->nil) { x = z->left; transplant(tree, z, z->left); } else { y = tree_minimum(tree, z->right); y_orig_color = y->color; x = y->right; if(y->parent == z) x->parent = y; else { transplant(tree, y, y->right); y->right = z->right; y->right->parent = y; } transplant(tree, z, y); y->left = z->left; y->left->parent = y; y->color = z->color; } if(y_orig_color == RBTREE_BLACK) delete_fixup(tree, x); if(tree->free_key) tree->free_key(node->key); free(node); return retval; }
void thrcode_fixup_op_break( THRCODE *thrcode, const char *loop_label, int address ) { FIXUP *fixup, *next , *unused = NULL; for( fixup = thrcode->op_break_fixups; fixup != NULL; ) { next = fixup_next( fixup ); if( strcmp( loop_label, fixup_name( fixup )) == 0 ) { thrcode_fixup( thrcode, fixup, address ); delete_fixup( fixup ); } else { unused = fixup_append( fixup, unused ); } fixup = next; } thrcode->op_break_fixups = unused; }
void thrcode_fixup_function_calls( THRCODE *thrcode, const char *function_name, int address ) { FIXUP *fixup, *next , *unused = NULL; for( fixup = thrcode->forward_function_calls; fixup != NULL; ) { next = fixup_next( fixup ); if( strcmp( function_name, fixup_name( fixup )) == 0 ) { thrcode_fixup( thrcode, fixup, address ); delete_fixup( fixup ); } else { unused = fixup_append( fixup, unused ); } fixup = next; } thrcode->forward_function_calls = unused; }
//free node, return val void * delete_node(struct rbtree *rbt, struct rbnode *nd) { void *val = NULL; struct rbnode *ret = nd; struct rbnode *tmp, *itor; if (nd == NULL || rbt == NULL) return NULL; val = nd->key; //pthread_mutex_lock(&(rbt->lock)); if (nd->left == &rbt->nil || nd->right == &rbt->nil) tmp = nd; else tmp = rbt_successor(rbt, nd); if (tmp->left != &rbt->nil) itor = tmp->left; else itor = tmp->right; itor->parent = tmp->parent; if (tmp->parent == &rbt->nil) rbt->root = itor; else { if (tmp == tmp->parent->left) tmp->parent->left = itor; else tmp->parent->right = itor; } if (tmp != itor) nd->key = tmp->key; if (tmp->color == BLACK) delete_fixup(rbt, itor); if (ret == NULL) printf("ret is null\n"); free(tmp); rbt->size--; //pthread_mutex_unlock(&(rbt->lock)); return val; }
//free node, return val void * delete_node(struct rbtree *rbt, void *key) { void *val = NULL; struct rbnode *nd = NULL; struct rbnode *tmp, *itor; nd = find_node(rbt, key); if (nd == NULL || rbt == NULL) { printf("find node error\n"); return NULL; } val = nd->key; if (nd->left == &rbt->nil || nd->right == &rbt->nil) tmp = nd; else tmp = rbt_successor(rbt, nd); if (tmp->left != &rbt->nil) itor = tmp->left; else itor = tmp->right; itor->parent = tmp->parent; if (tmp->parent == &rbt->nil) rbt->root = itor; else { if (tmp == tmp->parent->left) tmp->parent->left = itor; else tmp->parent->right = itor; } if (tmp != itor) nd->key = tmp->key; if (tmp->color == BLACK) delete_fixup(rbt, itor); //free(tmp); rbt->size--; return tmp; }
struct _sthread *rbt_remove(struct rbt *tree, int vruntime) { struct node *delete_node = rbt_find(tree, vruntime); struct node *y; struct node *x; struct _sthread *thread; if(delete_node == NULL){ printf("Node with vruntime = %d doesn't exist\n", vruntime); return NULL; } if (delete_node->queue->first->next != NULL) return queue_remove(delete_node->queue); if(delete_node->left == tree->nil || delete_node->right == tree->nil) y = delete_node; else { y = sucessor(tree, delete_node); if(!(y->color == RED || !black_leef(tree, y))) y = predecessor(tree, delete_node); } if (y->left != tree->nil) x = y->left; else x = y->right; x->parent = y->parent; if (y->parent == tree->nil) tree->root = x; else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if(y != delete_node){ substitute(tree, delete_node, y); if (isRoot(tree, y)) tree->root = y; } if (y == tree->first){ if (y->parent == tree->nil && y->right != tree->nil) tree->first = minimum(tree, y->right); else tree->first = x->parent; } if(y->color == BLACK) delete_fixup(tree, x); treeRoot(tree); lower(tree); thread = queue_remove(y->queue); destroy_node(y); return thread; }
/** Delete an element (z) from the tree * */ static void rbtree_delete_internal(rbtree_t *tree, rbnode_t *z, bool skiplock) { rbnode_t *x, *y; rbnode_t *parent; if (!z || z == NIL) return; if (!skiplock) { PTHREAD_MUTEX_LOCK(tree); } if (z->left == NIL || z->right == NIL) { /* y has a NIL node as a child */ y = z; } else { /* find tree successor with a NIL node as a child */ y = z->right; while (y->left != NIL) y = y->left; } /* x is y's only child */ if (y->left != NIL) { x = y->left; } else { x = y->right; /* may be NIL! */ } /* remove y from the parent chain */ parent = y->parent; if (x != NIL) x->parent = parent; if (parent) { if (y == parent->left) { parent->left = x; } else { parent->right = x; } } else { tree->root = x; } if (y != z) { if (tree->free) tree->free(z->data); z->data = y->data; y->data = NULL; if ((y->colour == BLACK) && parent) { delete_fixup(tree, x, parent); } /* * The user structure in y->data MAy include a * pointer to y. In that case, we CANNOT delete * y. Instead, we copy z (which is now in the * tree) to y, and fix up the parent/child * pointers. */ memcpy(y, z, sizeof(*y)); if (!y->parent) { tree->root = y; } else { if (y->parent->left == z) y->parent->left = y; if (y->parent->right == z) y->parent->right = y; } if (y->left->parent == z) y->left->parent = y; if (y->right->parent == z) y->right->parent = y; talloc_free(z); } else { if (tree->free) tree->free(y->data); if (y->colour == BLACK) delete_fixup(tree, x, parent); talloc_free(y); } tree->num_elements--; if (!skiplock) { PTHREAD_MUTEX_UNLOCK(tree); } }
rbt_status rbt_remove( rbt_tree *tree, rbt_iterator *i ) { if ( tree == NULL || i == NULL ) { return RBT_STATUS_KEY_NOT_FOUND; } node_type *x, *y; tag_type *rbt = ( tag_type * )tree; node_type *z = ( node_type * )i; if ( z->left == &rbt->sentinel || z->right == &rbt->sentinel ) { y = z; // y has a &rbt->sentinel node as a child } else { // Find tree successor with a &rbt->sentinel node as a child y = z->right; while ( y->left != &rbt->sentinel ) { y = y->left; } } // x is y's only child if ( y->left != &rbt->sentinel ) { x = y->left; } else { x = y->right; } // Remove y from the parent chain x->parent = y->parent; if ( y->parent ) { if ( y == y->parent->left ) { y->parent->left = x; } else { y->parent->right = x; } } else { rbt->root = x; } if ( y != z ) { z->key = y->key; z->val = y->val; } if ( y->color == BLACK ) { delete_fixup( rbt, x ); } free( y ); return RBT_STATUS_OK; }
void tree_delete (TREE **root, void *tree) { TREE *youngest, *descendent; TREE_COLOUR colour; if ((!tree) || (tree == TREE_NULL)) return; if ((((TREE *) tree)-> left == TREE_NULL) || (((TREE *) tree)-> right == TREE_NULL)) /* descendent has a TREE_NULL node as a child */ descendent = tree; else { /* find tree successor with a TREE_NULL node as a child */ descendent = ((TREE *) tree)-> right; while (descendent-> left != TREE_NULL) descendent = descendent-> left; } /* youngest is descendent's only child, if there is one, else TREE_NULL */ if (descendent-> left != TREE_NULL) youngest = descendent-> left; else youngest = descendent-> right; /* remove descendent from the parent chain */ if (youngest != TREE_NULL) youngest-> parent = descendent-> parent; if (descendent-> parent) if (descendent == descendent-> parent-> left) descendent-> parent-> left = youngest; else descendent-> parent-> right = youngest; else *root = youngest; colour = descendent-> colour; if (descendent != (TREE *) tree) { /* Conceptually what we are doing here is moving the data from */ /* descendent to tree. In fact we do this by linking descendent */ /* into the structure in the place of tree. */ descendent-> left = ((TREE *) tree)-> left; descendent-> right = ((TREE *) tree)-> right; descendent-> parent = ((TREE *) tree)-> parent; descendent-> colour = ((TREE *) tree)-> colour; if (descendent-> parent) { if (tree == descendent-> parent-> left) descendent-> parent-> left = descendent; else descendent-> parent-> right = descendent; } else *root = descendent; if (descendent-> left != TREE_NULL) descendent-> left-> parent = descendent; if (descendent-> right != TREE_NULL) descendent-> right-> parent = descendent; } if ((youngest != TREE_NULL) && (colour == BLACK)) delete_fixup (root, youngest); }