node* tree_delete() { node* z = tree_maximum(T); if(z->lchild == NULL) transplant(z,z->rchild); else if(z->rchild == NULL) transplant(z,z->lchild); else { node* y = tree_minimum(z->rchild); if(y->parent != z) { transplant(y,y->rchild); y->rchild = z->rchild; y->rchild->parent = y; } transplant(z,y); y->lchild = z->lchild; y->lchild->parent = y; } z->lchild = z->rchild = z->parent = NULL; return z; }
/* * * note: refer to the pseudocode in page 292. * */ int tree_successor(bstnode *pnode, bstnode **ppsuccessor_on_pnode) { assert(pnode && ppsuccessor_on_pnode); *ppsuccessor_on_pnode = NULL; if (pnode->pright) { tree_minimum(pnode->pright, ppsuccessor_on_pnode); return 0; } else { // bstnode *ptemp = pnode; while (ptemp && ptemp->pparent) { if (ptemp->pparent->pleft == ptemp) { /* ptemp->pparent is the successor of pnode. */ break; } else { ptemp = ptemp->pparent; } } *ppsuccessor_on_pnode = ptemp->pparent; } return 0; }
void tree_delete(struct node **root,int data) { struct node *y=NULL; struct node *root_temp=*root; struct node *z=*root; while(z->left!=NULL&& z->right!=NULL) { if(data==z->data) break; else if(data<z->data) z=z->left; else z=z->right; } if(z->left==NULL) transplant(root_temp,z,z->right); else if(z->right==NULL) transplant(root_temp,z,z->left); else y=tree_minimum(z->right); if(y->parent!=z) { transplant(root_temp,y,y->right); y->right = z->right; (z->right)->parent=y; } transplant(root_temp,z,y); y->left=z->left; (y->left)->parent=y; }
node_t* tree_delete(node_t* root, int key) { node_t* z = tree_search(root, key); if (!z) { return root; } if (z->left == NULL) { root = transplant(root, z, z->right); } else if (z->right == NULL) { root = transplant(root, z, z->left); } else { node_t* y = tree_minimum(z->right); if (y->parent != z) { root = transplant(root, y, y->right); y->right = z->right; y->right->parent = y; } root = transplant(root, z, y); y->left = z->left; y->left->parent = y; } free(z); return root; }
static bst_node *node_successor (bst_node *root,bst_node *x) { if (x->right) return tree_minimum(x->right); bst_node *y = x->parent; while (y && x == y->right) { x = y; y = x->parent; } return y; }
/* * * note: refer to the pseudocode in page 291. * */ int tree_minimum(bstnode *phead, bstnode **ppminimum) { assert(phead && ppminimum); if (NULL == phead->pleft) { *ppminimum = phead; } else { tree_minimum(phead->pleft, ppminimum); } return 0; }
struct node* successor(struct node*x) { struct node *y=NULL; if(x->right!=NULL) return tree_minimum(x->right); y=x->parent; while(y!=NULL && x==(y->right)) { x=y; y=y->parent; } return y; }
node_t* tree_succesor(node_t* n) { if (n->right) { return tree_minimum(n->right); } node_t* p = n->parent; while (p && p->right == n) { n = p; p = p->parent; } return p; }
BSTree tree_successor(BSTree x) { BSTree y; if(!x && x->right != NULL) return tree_minimum(x->right); //从节点x开始沿树而上直到遇到这样的一个结点:该结点是其双亲的左孩子 y = x->p; while(y != NULL && x == y->right){ x = y; y = y->p; } return y; }
tnode_p tree_successor(tnode_p node){ tnode_p next; if(node->right != NULL) return tree_minimum(node->right); next = node->parent; while(next != NULL && node == next->right){ node = next; next = next->parent; } return next; }
int tree_successor(struct node * node_ptr) { struct node * node_ptr2 = NULL; if (node_ptr->right != NULL) { return tree_minimum(node_ptr); } else { node_ptr2 = node_ptr->parent; while (node_ptr2 != NULL && node_ptr == node_ptr2->right) { node_ptr = node_ptr2; node_ptr2 = node_ptr2->parent; } return node_ptr2->data; } }
ltree tree_successor(ltree x) { ltree y; if (x->rchild != NULL) return tree_minimum(x->rchild); y = x->parent; while (y != NULL && x == y->rchild){ x = y; y = y->parent; } return 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; }
struct tree_node *tree_successor(struct tree_node *node) { if (node == NULL) { return NULL; } if (node->right != NULL) { return tree_minimum(node->right); } struct tree_node *p = node->parent; while (p != NULL && node != p->left) { node = p; p = p->parent; } return p; }
void tree_delete(BSTHead T,BSTree z) { BSTree y; if(z->left == NULL){//z没有左孩子 transplant(T,z,z->right); }else if(z->right == NULL){//z有一个左孩子,但没有右孩子 transplant(T,z,z->left); }else{//有两个孩子的情况 y = tree_minimum(z->right);//查找z的后继,z的右子树非空 } if(y->p != z){//如果y不是z的左孩子,那么用y的右孩子替换y并成为y的双亲的一个孩子 transplant(T,y,y->right); y->right = z->right; y->right->p = y; } transplant(T,z,y); y->left = z->left; y->left->p = y; }
int tree_delete(bstnode **pphead, bstnode *pdel) { assert(pphead && pdel); if (NULL == pdel->pleft) { transplant(pphead, pdel, pdel->pright); } else if (NULL == pdel->pright) { transplant(pphead, pdel, pdel->pleft); } else { bstnode *py = NULL; // py is the successor of pdel. tree_minimum(pdel->pright, &py); if (py != pdel->pright) { transplant(pphead, py, py->pright); py->pright = pdel->pright; pdel->pright->pparent = py; } transplant(pphead, pdel, py); py->pleft = pdel->pleft; pdel->pleft->pparent = py; } free(pdel); return 0; }
int main(int argc, char** argv) { struct tree* tree; ALLOC_PTR(tree); assert(tree != 0); tree_ini(tree, 50); // int tree_node_add(struct tree* vtree, int value) int rc = tree_node_add(tree, 10); rc = tree_node_add(tree, 20); rc = tree_node_add(tree, 1); rc = tree_node_add(tree, 4); //fail rc = tree_node_add(tree, 120); rc = tree_node_add(tree, 110); rc = tree_node_add(tree, 130); rc = tree_node_add(tree, 100); //fail rc = tree_node_add(tree, 3); //fail inorder_tree_walk(tree->root); size_t start = 0; size_t finish = 0; struct node* t = 0; start = checkpoint(); t = tree_search(tree->root, 120); finish = checkpoint(); printf("\nrecursuve search %i\n", t->value); printf("time recursive: %zu\n", finish - start); start = finish = 0; start = checkpoint(); t = iterative_tree_search(tree->root, 120); finish = checkpoint(); printf("\niterative search %i\n", t->value); printf("time iterative: %zu\n", finish - start); t = tree_minimum(tree->root); printf("minimum: %i \n", t->value); t = tree_maximum(tree->root); printf("maximum: %i \n", t->value); struct node* temp; ALLOC_PTR(temp); assert(temp != 0); temp->value = 64; temp->right = 0; temp->left = 0; //T - дерево, z - указатель на вставляемую вершину tree_insert(tree, temp); inorder_tree_walk(tree->root); /* tree = tree_node_add(tree, 1, 1); //bad bug tree = tree_node_add(tree, 4, 1); tree = tree_node_add(tree, 2, 0); tree = tree_node_add(tree, 3, 0); tree = tree_node_add(tree, 4, 1); */ //tree_print(tree); /* tree_fini(tree); */ return 0; }