/* * Call func() for each node, passing it the node data and a cookie; * If func() returns non-zero for a node, the traversal stops and the * error value is returned. Returns 0 on successful traversal. */ int rbtree_traverse_node(struct rbtree *tree, struct rbnode *node, int (*func)(void *, void *), void *cookie, enum rbtree_traversal order) { int error; if (node != rbnil(tree)) { if (order == rbpreorder) { if ((error = func(node->data, cookie)) != 0) { return (error); } } if ((error = rbtree_traverse_node(tree, node->left, func, cookie, order)) != 0) { return (error); } if (order == rbinorder) { if ((error = func(node->data, cookie)) != 0) { return (error); } } if ((error = rbtree_traverse_node(tree, node->right, func, cookie, order)) != 0) { return (error); } if (order == rbpostorder) { if ((error = func(node->data, cookie)) != 0) { return (error); } } } return (0); }
/* * Returns the successor of node, or nil if there is none. */ static struct rbnode * rbsuccessor(struct rbtree *tree, struct rbnode *node) { struct rbnode *succ; if ((succ = node->right) != rbnil(tree)) { while (succ->left != rbnil(tree)) succ = succ->left; } else { /* No right child, move up until we find it or hit the root */ for (succ = node->parent; node == succ->right; succ = succ->parent) node = succ; if (succ == rbroot(tree)) succ = rbnil(tree); } return succ; }
/* * Returns the successor of node, or nil if there is none. */ static struct rbnode * rbsuccessor(struct rbtree *tree, struct rbnode *node) { struct rbnode *succ; debug_decl(rbsuccessor, SUDO_DEBUG_RBTREE) if ((succ = node->right) != rbnil(tree)) { while (succ->left != rbnil(tree)) succ = succ->left; } else { /* No right child, move up until we find it or hit the root */ for (succ = node->parent; node == succ->right; succ = succ->parent) node = succ; if (succ == rbroot(tree)) succ = rbnil(tree); } debug_return_ptr(succ); }
/* * Recursive portion of rbdestroy(). */ static void _rbdestroy(struct rbtree *tree, struct rbnode *node, void (*destroy)(void *)) { if (node != rbnil(tree)) { _rbdestroy(tree, node->left, destroy); _rbdestroy(tree, node->right, destroy); if (destroy != NULL) destroy(node->data); efree(node); } }
/* * Delete node 'z' from the tree and return its data pointer. */ void *rbtree_delete(struct rbtree *tree, struct rbnode *z) { struct rbnode *x, *y; void *data = z->data; if (z->left == rbnil(tree) || z->right == rbnil(tree)) { y = z; } else { y = rbsuccessor(tree, z); } x = (y->left == rbnil(tree)) ? y->right : y->left; if ((x->parent = y->parent) == rbtree_root(tree)) { rbtree_first(tree) = x; } else { if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } } if (y->color == black) { rbrepair(tree, x); } if (y != z) { y->left = z->left; y->right = z->right; y->parent = z->parent; y->color = z->color; z->left->parent = z->right->parent = y; if (z == z->parent->left) { z->parent->left = y; } else { z->parent->right = y; } } free(z); tree->num_nodes--; return (data); }
/* * Look for a node matching key in tree. * Returns a pointer to the node if found, else NULL. */ struct rbnode *rbtree_find_node(struct rbtree *tree, void *key) { struct rbnode *node = rbtree_first(tree); int res; while (node != rbnil(tree)) { if ((res = tree->compar(key, node->data)) == 0) { return (node); } node = res < 0 ? node->left : node->right; } return (NULL); }
/* * Look for a node matching key in tree. * Returns a pointer to the node if found, else NULL. */ struct rbnode * rbfind(struct rbtree *tree, void *key) { struct rbnode *node = rbfirst(tree); int res; while (node != rbnil(tree)) { if ((res = tree->compar(key, node->data)) == 0) return node; node = res < 0 ? node->left : node->right; } return NULL; }
/* * Look for a node matching key in tree. * Returns a pointer to the node if found, else NULL. */ struct rbnode * rbfind(struct rbtree *tree, void *key) { struct rbnode *node = rbfirst(tree); int res; debug_decl(rbfind, SUDO_DEBUG_RBTREE) while (node != rbnil(tree)) { if ((res = tree->compar(key, node->data)) == 0) debug_return_ptr(node); node = res < 0 ? node->left : node->right; } debug_return_ptr(NULL); }
/* * Delete node 'z' from the tree and return its data pointer. */ void *rbdelete(struct rbtree *tree, struct rbnode *z) { struct rbnode *x, *y; void *data = z->data; if (z->left == rbnil(tree) || z->right == rbnil(tree)) y = z; else y = rbsuccessor(tree, z); x = (y->left == rbnil(tree)) ? y->right : y->left; if ((x->parent = y->parent) == rbroot(tree)) { rbfirst(tree) = x; } else { if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; } if (y->color == black) rbrepair(tree, x); if (y != z) { y->left = z->left; y->right = z->right; y->parent = z->parent; y->color = z->color; z->left->parent = z->right->parent = y; if (z == z->parent->left) z->parent->left = y; else z->parent->right = y; } free(z); return data; }
/* * Perform a right rotation starting at node. */ static void rotate_right(struct rbtree *tree, struct rbnode *node) { struct rbnode *child; child = node->left; node->left = child->right; if (child->right != rbnil(tree)) child->right->parent = node; child->parent = node->parent; if (node == node->parent->left) node->parent->left = child; else node->parent->right = child; child->right = node; node->parent = child; }
/* * Perform a left rotation starting at node. */ static void rotate_left(struct rbtree *tree, struct rbnode *node) { struct rbnode *child; child = node->right; node->right = child->left; if (child->left != rbnil(tree)) { child->left->parent = node; } child->parent = node->parent; if (node == node->parent->left) { node->parent->left = child; } else { node->parent->right = child; } child->left = node; node->parent = child; }
/* * Perform a right rotation starting at node. */ static void rotate_right(struct rbtree *tree, struct rbnode *node) { struct rbnode *child; debug_decl(rotate_right, SUDO_DEBUG_RBTREE) child = node->left; node->left = child->right; if (child->right != rbnil(tree)) child->right->parent = node; child->parent = node->parent; if (node == node->parent->left) node->parent->left = child; else node->parent->right = child; child->right = node; node->parent = child; debug_return; }
/* * Call func() for each node, passing it the node data and a cookie; * If func() returns non-zero for a node, the traversal stops and the * error value is returned. Returns 0 on successful traversal. */ int rbapply_node(struct rbtree *tree, struct rbnode *node, int (*func)(void *, void *), void *cookie, enum rbtraversal order) { int error; if (node != rbnil(tree)) { if (order == preorder) if ((error = func(node->data, cookie)) != 0) return error; if ((error = rbapply_node(tree, node->left, func, cookie, order)) != 0) return error; if (order == inorder) if ((error = func(node->data, cookie)) != 0) return error; if ((error = rbapply_node(tree, node->right, func, cookie, order)) != 0) return error; if (order == postorder) if ((error = func(node->data, cookie)) != 0) return error; } return 0; }
/* * Call func() for each node, passing it the node data and a cookie; * If func() returns non-zero for a node, the traversal stops and the * error value is returned. Returns 0 on successful traversal. */ int rbapply_node(struct rbtree *tree, struct rbnode *node, int (*func)(void *, void *), void *cookie, enum rbtraversal order) { int error; debug_decl(rbapply_node, SUDO_DEBUG_RBTREE) if (node != rbnil(tree)) { if (order == preorder) if ((error = func(node->data, cookie)) != 0) debug_return_int(error); if ((error = rbapply_node(tree, node->left, func, cookie, order)) != 0) debug_return_int(error); if (order == inorder) if ((error = func(node->data, cookie)) != 0) debug_return_int(error); if ((error = rbapply_node(tree, node->right, func, cookie, order)) != 0) debug_return_int(error); if (order == postorder) if ((error = func(node->data, cookie)) != 0) debug_return_int(error); } debug_return_int(0); }
/* * If a node matching "data" already exists, a pointer to * the existant node is returned. Otherwise we return NULL. */ struct rbnode *rbtree_insert(struct rbtree *tree, void *data) { struct rbnode *node = rbtree_first(tree); struct rbnode *parent = rbtree_root(tree); int res; /* Find correct insertion point. */ while (node != rbnil(tree)) { parent = node; if ((res = tree->compar(data, node->data)) == 0) { return (node); } node = res < 0 ? node->left : node->right; } node = (struct rbnode *) malloc(sizeof(*node)); node->data = data; node->left = node->right = rbnil(tree); node->parent = parent; if (parent == rbtree_root(tree) || tree->compar(data, parent->data) < 0) { parent->left = node; } else { parent->right = node; } node->color = red; /* * If the parent node is black we are all set, if it is red we have * the following possible cases to deal with. We iterate through * the rest of the tree to make sure none of the required properties * is violated. * * 1) The uncle is red. We repaint both the parent and uncle black * and repaint the grandparent node red. * * 2) The uncle is black and the new node is the right child of its * parent, and the parent in turn is the left child of its parent. * We do a left rotation to switch the roles of the parent and * child, relying on further iterations to fixup the old parent. * * 3) The uncle is black and the new node is the left child of its * parent, and the parent in turn is the left child of its parent. * We switch the colors of the parent and grandparent and perform * a right rotation around the grandparent. This makes the former * parent the parent of the new node and the former grandparent. * * Note that because we use a sentinel for the root node we never * need to worry about replacing the root. */ while (node->parent->color == red) { struct rbnode *uncle; if (node->parent == node->parent->parent->left) { uncle = node->parent->parent->right; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else { /* if (uncle->color == black) */ if (node == node->parent->right) { node = node->parent; rotate_left(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_right(tree, node->parent->parent); } } else { /* if (node->parent == node->parent->parent->right) */ uncle = node->parent->parent->left; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else { /* if (uncle->color == black) */ if (node == node->parent->left) { node = node->parent; rotate_right(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_left(tree, node->parent->parent); } } } tree->num_nodes++; rbtree_first(tree)->color = black; /* first node is always black */ return (NULL); }
/* * Insert data pointer into a redblack tree. * Returns a 0 on success, 1 if a node matching "data" already exists * (filling in "existing" if not NULL), or -1 on malloc() failure. */ int rbinsert(struct rbtree *tree, void *data, struct rbnode **existing) { struct rbnode *node = rbfirst(tree); struct rbnode *parent = rbroot(tree); int res; debug_decl(rbinsert, SUDOERS_DEBUG_RBTREE) /* Find correct insertion point. */ while (node != rbnil(tree)) { parent = node; if ((res = tree->compar(data, node->data)) == 0) { if (existing != NULL) *existing = node; debug_return_int(1); } node = res < 0 ? node->left : node->right; } node = malloc(sizeof(*node)); if (node == NULL) { sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_LINENO, "unable to allocate memory"); debug_return_int(-1); } node->data = data; node->left = node->right = rbnil(tree); node->parent = parent; if (parent == rbroot(tree) || tree->compar(data, parent->data) < 0) parent->left = node; else parent->right = node; node->color = red; /* * If the parent node is black we are all set, if it is red we have * the following possible cases to deal with. We iterate through * the rest of the tree to make sure none of the required properties * is violated. * * 1) The uncle is red. We repaint both the parent and uncle black * and repaint the grandparent node red. * * 2) The uncle is black and the new node is the right child of its * parent, and the parent in turn is the left child of its parent. * We do a left rotation to switch the roles of the parent and * child, relying on further iterations to fixup the old parent. * * 3) The uncle is black and the new node is the left child of its * parent, and the parent in turn is the left child of its parent. * We switch the colors of the parent and grandparent and perform * a right rotation around the grandparent. This makes the former * parent the parent of the new node and the former grandparent. * * Note that because we use a sentinel for the root node we never * need to worry about replacing the root. */ while (node->parent->color == red) { struct rbnode *uncle; if (node->parent == node->parent->parent->left) { uncle = node->parent->parent->right; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else /* if (uncle->color == black) */ { if (node == node->parent->right) { node = node->parent; rotate_left(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_right(tree, node->parent->parent); } } else { /* if (node->parent == node->parent->parent->right) */ uncle = node->parent->parent->left; if (uncle->color == red) { node->parent->color = black; uncle->color = black; node->parent->parent->color = red; node = node->parent->parent; } else /* if (uncle->color == black) */ { if (node == node->parent->left) { node = node->parent; rotate_right(tree, node); } node->parent->color = black; node->parent->parent->color = red; rotate_left(tree, node->parent->parent); } } } rbfirst(tree)->color = black; /* first node is always black */ debug_return_int(0); }