static tnode_p pull_out(tree_p tr, tnode_p node){ tnode_p parent, current, next; if(node->left == NULL || node->right == NULL) current = node; else current = tree_successor(node); parent = current->parent; if(current->left != NULL) next = current->left; else next = current->right; if(next != NULL) next->parent = parent; if(parent == NULL) tr->root = next; else if(current == parent->left) parent->left = next; else parent->right = next; free(node->data); if(node != current) node->data = current->data; current->data = NULL; return current; }
void print_successor(tnode_p node){ int *pia, *pib; tnode_p next = tree_successor(node); pia = (int*) node->data; pib = (int*) next->data; printf("The successor to %d is %d\n", *pia, *pib); }
/** * 二叉树删除 */ Binary_tree * tree_delete(Binary_tree * bt,Binary_tree * bt_delete) { Binary_tree * y; Binary_tree * children; if(bt_delete->left==NULL||bt_delete->right==NULL) { y = bt_delete; }else { y = tree_successor(bt_delete); } if(y->left!=NULL){ children = y->left; }else { children = y->right; } if(children!=NULL) { children->parent = y->parent; } if(y->parent==NULL) { bt = children; }else if(y==y->parent->left) { y->parent->left = children; }else { y->parent->right = children; } if(y!=bt_delete) { bt_delete->num = y->num; } return bt; }
int main(void) { bt root; root.root = NULL; int i; int key[] = {1, 4, 5, 10, 16, 17, 21}; int num = sizeof(key) / sizeof(int); printf("num:%d\n", num); bn *tmp; srand((unsigned int) 6120); for (i = 0; i < 6120; i++) { tmp = malloc(sizeof(bn)); tmp->key = rand() % 6120; tmp->p = tmp->left = tmp->right = NULL; tree_insert_rec(&root, tmp); } tmp = malloc(sizeof(bn)); tmp->key = 1991; tmp->p = tmp->left = tmp->right = NULL; tree_insert_rec(&root, tmp); tmp = tree_search(root.root, 1991); printf("%d\n", tree_successor(tmp)->key); /*inorder_tree_walk(root.root);*/ printf("\n"); tree_delete(&root, tmp); /*inorder_tree_walk(root.root);*/ /*inorder_tree_walk_nore(root.root);*/ printf("\n"); return 0; }
void tree_delete(ltree T, ltree z) { ltree x; int y; if (z->lchild == NULL && z->rchild == NULL)//z has no child { x = z->parent; if (x == NULL)//z is rooot T = NULL; else{ if (x->lchild == z) x->lchild = NULL; if (x->rchild == z) x->rchild = NULL; } } if ((z->lchild == NULL && z->rchild != NULL) || (z->lchild != NULL && z->rchild == NULL))//z has only one child { if (z->lchild != NULL){ z->lchild->parent = z->parent; if (z->parent != NULL){//z is not root if (z->parent->lchild == z){//z is the left child of its parent z->parent->lchild = z->lchild; }else{ z->parent->rchild = z->lchild; } }else{ T = z->lchild; } }else{ z->rchild->parent = z->parent; if (z->parent != NULL){ if (z->parent->lchild == z){ z->parent->lchild = z->rchild; }else{ z->parent->rchild = z->rchild; } }else{ T = z->rchild; } } } if (z->lchild != NULL && z->rchild != NULL)//z has tow children { x = tree_successor(z);//find z's successor and delete it y = x->data;//get data tree_delete(T, x);//delete the successor z->data = y; } }
treenode_t * RBTree_delete(tree_t *T, treenode_t *z){ if (z == NULL || z == gNil) { return; } if (DEBUG) printf("\nRBTree_delete\n"); treenode_t * y = gNil; treenode_t * x = gNil; // Find the place (y) to delete: if ((z->left_child == gNil) || (z->right_child == gNil)) { y = z; } else { y = tree_successor(z); } // Set the node (x) next to y: if (y->left_child != gNil) { x = y->left_child; } else { x = y->right_child; } // skip y x->parent = y->parent; if (y->parent == gNil) { T->root = x; } else { if (y == y->parent->left_child) { y->parent->left_child = x; } else { //y == y->parent->right_child y->parent->right_child = x; } } // if y != z, copy y to z if (y != z) { z->key = y->key; //TODO: Copy y's satellite date to z. } if (y->color == BLACK) { RBTree_delete_fixup(T, x); } return y; }
void tree_delete(Node* &T, Node *x) { if (NIL == T || NIL == x) return; // y是要删除的节点 Node *y = NULL; if ((NIL == LEFT(x)) || (NIL == RIGHT(x))) y = x; else y = tree_successor(x); if (y != x) KEY(x) = KEY(y); // y肯定只有一个孩子 Node * z = (NIL != LEFT(y))? LEFT(y): RIGHT(y); // 即使z是NIL,也给挂上,否则会影响rb_delete_fixup PARENT(z) = PARENT(y); if (NIL == PARENT(y)) { // 根节点发生变化 T = z; } else if (IS_LEFT(y)){ LEFT(PARENT(y)) = z; } else { RIGHT(PARENT(y)) = z; } // 调整路径上节点的max值 Node *p = PARENT(z); while (NIL != p) { MAX(p) = max(MAX(LEFT(p)), MAX(RIGHT(p)), HIGH(KEY(p))); p = PARENT(p); } // 如果y是黑色,说明破坏红黑树的规则;如果y是红色,则不会破坏 if (IS_BLACK(y)) { rb_delete_fixup(T, z); } free_node(y); }
treenode_t * tree_delete(tree_t * tree, treenode_t * z) { if (z == gNil) { return NULL; } treenode_t * y = gNil; treenode_t * x = gNil; // Find the place (y) to delete: if ((z->left_child == gNil) || (z->right_child == gNil)) { y = z; } else { y = tree_successor(z); } // Set the node (x) next to y: if (y->left_child != gNil) { x = y->left_child; } else { x = y->right_child; } // skip y if (x) { x->parent = y->parent; } if (y->parent == gNil) { tree->root = x; } else { if (y == y->parent->left_child) { y->parent->left_child = x; } else { //y == y->parent->right_child y->parent->right_child = x; } } // if y != z, copy y to z if (y != z) { z->key = y->key; //TODO: Copy y's satellite date to z. } return y; }
void tree_delete(Node* &T, Node *x) { if (NIL == T || NIL == x) return; // y是要删除的节点 Node *y = NULL; if ((NIL == LEFT(x)) || (NIL == RIGHT(x))) y = x; else y = tree_successor(x); if (y != x) KEY(x) = KEY(y); // y肯定只有一个孩子 Node * z = (NIL != LEFT(y))? LEFT(y): RIGHT(y); // 即使z是NIL,也给挂上,否则会影响rb_delete_fixup PARENT(z) = PARENT(y); if (NIL == PARENT(y)) { // 根节点发生变化 T = z; } else if (IS_LEFT(y)){ LEFT(PARENT(y)) = z; } else { RIGHT(PARENT(y)) = z; } // 沿着y节点向上,更新路径上每个节点的size Node *p = y; while (NIL != (p=PARENT(p))) { SIZE(p)--; } // 如果y是黑色,说明破坏红黑树的规则;如果y是红色,则不会破坏 if (IS_BLACK(y)) { rb_delete_fixup(T, z); } free_node(y); }
void testBSTree() { printf("\nNow is Create Binary tree with node size %d >>>>>>>>>>\n", ARRAY_SIZE); randomize_in_place(A, ARRAY_SIZE); // randomize_maxnum(A, ARRAY_SIZE, ARRAY_SIZE); print_array(A, ARRAY_SIZE); startProfileTime(); tree_t * tree = tree_create(A, ARRAY_SIZE); endProfileTime("Create Binary search tree "); #if 0 printf("\nPre order traverse:\n"); tree_preorder_traverse(tree->root, my_treenode_key_traverse); printf("\nPost order traverse:\n"); tree_postorder_traverse(tree->root, my_treenode_key_traverse); printf("\nIn order traverse:\n"); tree_inorder_traverse(tree->root, my_treenode_key_traverse); #endif int key = 50; startProfileTime(); treenode_t * search_result = tree_search(tree->root, key); endProfileTime("Binary tree search"); if (search_result != get_nil_node()) { printf("Found key:%d\n", key); } else { printf(" Not found key:%d\n", key); } tree_left_rotate(tree, search_result); tree_right_rotate(tree, search_result); traverse_no_recurise(tree->root, my_treenode_key_traverse); treenode_t * max, * min; max = tree_max(tree->root); min = tree_min(tree->root); printf("\nmax = %ld\n min = %ld\n", max->key, min->key); treenode_t * bigger = tree_successor(search_result); printf("successor = %ld\n", (bigger!=NULL) ? bigger->key : -1); treenode_t * smaller = tree_predecessor(search_result); printf("perdecessor = %ld\n", (smaller!=NULL) ? smaller->key : -1); //Test delete: treenode_t * deleted_node = RBTree_delete(tree, search_result); // treenode_t * deleted_node = tree_delete(tree, search_result); if (deleted_node) printf("del %p, key=%ld from tree.\n", deleted_node, deleted_node->key); tree_inorder_traverse(tree->root, my_treenode_key_traverse); // traverse_no_recurise(tree->root, my_treenode_key_traverse); int height = get_tree_height(tree->root); printf("\nget tree h = %d\n", height); tree_destroy(tree, NULL); }
avl_node* avl_delete(avl_node *root, int key) { avl_node *node = avl_search(root, key); if(node == NULL) { printf("no such key in tree\n"); return root; } avl_node *target = node; //if node has two children, delete its successor if((target->left != NULL)&&(target->right != NULL)) target = tree_successor(target); avl_node *parent = target->parent; avl_node *child = NULL; if(target->left == NULL) child = target->right; else child = target->left; int direction = 0; if(child != NULL) child->parent = parent; if(parent == NULL) root = child; else { if(parent->left == target) { parent->left = child; direction = 0; } else { parent->right = child; direction = 1; } } // if target is node's succesor, after delete target, copy the data from // target to node. if(target != node) node->key = target->key; free(target); //balance the tree after delete while(parent != NULL) { // you can't use child == parent->left to judge this, // because child may be null, parent has no child. if(direction == 0) { parent->balance += 1; } else { parent->balance -= 1; } if((parent->balance == 1)||(parent->balance == -1)) break; //attention: a deletion maybe require rebalancing all the way // back up the tree. if((parent->balance ==2)||(parent->balance == -2)) { tree_delete_balance(parent); if(parent == root) root = parent->parent; parent = parent->parent; //after balance, the current parent should change. } child = parent; parent = parent->parent; if(parent != NULL) direction = child == parent->left? 0:1; } return root; }
int tree_delete(bstnode **pphead, int ival) { assert(pphead); bstnode *pdel = NULL; tree_search(*pphead, ival, &pdel); if (!pdel) { return 0; } if ((NULL == pdel->pleft) && (NULL == pdel->pright)) { /* the node to be deleted has no children. */ if (NULL == pdel->pparent) { *pphead = NULL; } else if (pdel->pparent->pleft == pdel) { pdel->pparent->pleft = NULL; } else { pdel->pparent->pright = NULL; } } if ((NULL ==pdel->pleft) && (pdel->pright)) { /* the node to be deleted only has right child. */ if (NULL == pdel->pparent) { pdel->pright->pparent = NULL; *pphead = pdel->pright; } else if (pdel->pparent->pleft == pdel) { pdel->pparent->pleft = pdel->pright; pdel->pright->pparent = pdel->pparent; } else { pdel->pparent->pright = pdel->pright; pdel->pright->pparent = pdel->pparent; } } if ((pdel->pleft) && (NULL == pdel->pright)) { /* the node to be deleted only has left child. */ if (NULL == pdel->pparent) { pdel->pleft->pparent = NULL; *pphead = pdel->pleft; } else if (pdel->pparent->pleft == pdel) { pdel->pparent->pleft = pdel->pleft; pdel->pleft->pparent = pdel->pparent; } else { pdel->pparent->pright = pdel->pleft; pdel->pleft->pparent = pdel->pparent; } } if ((pdel->pleft) && (pdel->pright)) { /* the node to be deleted has left child and right child. */ bstnode *psuccessor_on_pdel; tree_successor(pdel, &psuccessor_on_pdel); if (psuccessor_on_pdel->pparent->pright == psuccessor_on_pdel) { psuccessor_on_pdel->pparent->pright = psuccessor_on_pdel->pright; if (psuccessor_on_pdel->pright) { psuccessor_on_pdel->pright->pparent = psuccessor_on_pdel->pparent; } } else { psuccessor_on_pdel->pparent->pleft = psuccessor_on_pdel->pright; if (psuccessor_on_pdel->pright) { psuccessor_on_pdel->pright->pparent = psuccessor_on_pdel->pparent; } } psuccessor_on_pdel->pleft = pdel->pleft; if (pdel->pleft) { pdel->pleft->pparent = psuccessor_on_pdel; } psuccessor_on_pdel->pright = pdel->pright; if (pdel->pright) { pdel->pright->pparent = psuccessor_on_pdel; } psuccessor_on_pdel->pparent = pdel->pparent; if (NULL == pdel->pparent) { psuccessor_on_pdel->pparent = NULL; *pphead = psuccessor_on_pdel; } else if (pdel->pparent->pleft == pdel) { pdel->pparent->pleft = psuccessor_on_pdel; } else { pdel->pparent->pright = psuccessor_on_pdel; } } free(pdel); return 0; }
int main() { // stk_stack* enumResult; int option=0; int newKey,newKey2; int* newInt; red_black_node_struct* newNode; red_black_tree_struct* tree; tree=rb_tree_create(IntComp,IntDest,InfoDest,IntPrint,InfoPrint); while(option!=8) { printf("choose one of the following:\n"); printf("(1) add to tree\n(2) delete from tree\n(3) query\n"); printf("(4) find predecessor\n(5) find sucessor\n(6) enumerate\n"); printf("(7) print tree\n(8) quit\n"); do option=fgetc(stdin); while(-1 != option && isspace(option)); option-='0'; switch(option) { case 1: { printf("type key for new node\n"); scanf("%i",&newKey); newInt=(int*) malloc(sizeof(int)); *newInt=newKey; rb_tree_insert(tree,newInt,0); } break; case 2: { printf("type key of node to remove\n"); scanf("%i",&newKey); if ( ( newNode=rb_exact_query(tree,&newKey ) ) ) rb_delete(tree,newNode);/*assignment*/ else printf("key not found in tree, no action taken\n"); } break; case 3: { printf("type key of node to query for\n"); scanf("%i",&newKey); if ( ( newNode = rb_exact_query(tree,&newKey) ) ) {/*assignment*/ printf("data found in tree at location %i\n",(int)newNode); } else { printf("data not in tree\n"); } } break; case 4: { printf("type key of node to find predecessor of\n"); scanf("%i",&newKey); if ( ( newNode = rb_exact_query(tree,&newKey) ) ) {/*assignment*/ newNode=tree_predecessor(tree,newNode); if(tree->nil == newNode) { printf("there is no predecessor for that node (it is a minimum)\n"); } else { printf("predecessor has key %i\n",*(int*)newNode->key); } } else { printf("data not in tree\n"); } } break; case 5: { printf("type key of node to find successor of\n"); scanf("%i",&newKey); if ( (newNode = rb_exact_query(tree,&newKey) ) ) { newNode=tree_successor(tree,newNode); if(tree->nil == newNode) { printf("there is no successor for that node (it is a maximum)\n"); } else { printf("successor has key %i\n",*(int*)newNode->key); } } else { printf("data not in tree\n"); } } break; case 6: { printf("type low and high keys to see all keys between them\n"); /* scanf("%i %i",&newKey,&newKey2); enumResult=rbEnumerate(tree,&newKey,&newKey2); while ( (newNode = StackPop(enumResult)) ) { tree->PrintKey(newNode->key); printf("\n"); } free(enumResult); }*/ break; case 7: { rb_tree_print(tree); } break; case 8: { red_black_node_struct *ss ;//= (red_black_node_struct*)start( tree); for ( ss= start( tree) ; ss != NULL ; ss = next(tree) ){ printf("%d\n",*(int*)ss->key); } rb_tree_destroy(tree); return 0; } break; default: printf("Invalid input; Please try again.\n"); } } } return 0; }
int main(void) { printf("The available commands are:\n" "I K - Inserts a key with the value K\n" "F K - Finds the entry with value K\n" "S K - Searches for the successor of K\n" "D - Dumps the tree (pre-order traversal)\n" "Q - Quit\n"); int key; struct tree_node *root = NULL; struct tree_node *new_root = NULL; int finish = 0; while (!finish) { printf("> "); fflush(stdout); if (fgets(input_buff, sizeof input_buff, stdin) == NULL) { break; } if (sscanf(input_buff, " I %d", &key) == 1) { new_root = tree_insert(root, key); if (new_root == NULL) { fprintf(stderr, "Out of memory; couldn't insert node.\n"); } else { root = new_root; } } else if (sscanf(input_buff, " F %d", &key) == 1) { struct tree_node *n = tree_find(root, key); if (n == NULL) { printf("No such key: %d\n", key); } else { printf("Found key %d. Left = ", key); if (n->left != NULL) { printf("%d; ", n->left->key); } else { printf("N/A; "); } printf("Right = "); if (n->right != NULL) { printf("%d; ", n->right->key); } else { printf("N/A; "); } printf("Parent = "); if (n->parent != NULL) { printf("%d; ", n->parent->key); } else { printf("N/A; "); } printf("\n"); } } else if (sscanf(input_buff, " S %d", &key) == 1) { struct tree_node *n1 = tree_find(root, key); if (n1 == NULL) { fprintf(stderr, "No such key: %d\n", key); continue; } struct tree_node *n2 = tree_successor(n1); if (n2 == NULL) { printf("Node %d has no successor.\n", n1->key); } else { printf("Successor of %d is %d\n", n1->key, n2->key); } } else { char op; if (sscanf(input_buff, " %c", &op) == 1) { switch (op) { case 'D': pre_order_traversal(root); break; case 'Q': finish = 1; break; default: fprintf(stderr, "Invalid input\n"); break; } } else { fprintf(stderr, "Invalid input\n"); break; } } } destroy_tree(root); return 0; }
int main() { tnode_t *n; searchTree_t tree; tree.root = NULL; tree.size = 0; int loops = 10; int i,rd; int max = 9; int status = -2; srand(time(NULL)); for(i = 1;i<= loops;i++) { rd = rand() % max + 1; printf("rd:%d\n",rd); n = malloc(sizeof(tnode_t)); n->data = rd; status = tree_insert(&tree,n); if(status == -1) printf("insert error\n"); // inorder_walk_tree(tree.root,print_tnode); printf("\n"); } inorder_walk_tree(tree.root,print_tnode); printf("\n"); printf("deep walk\n"); deep_walk(tree.root); struct timeval start,end; gettimeofday(&start,NULL); tnode_t *r = tree_search_v1(tree.root , max - 1); gettimeofday(&end,NULL); if(r == NULL) printf("not found\n"); else printf("found %d at 0x%x\n",r->data,(unsigned int)r); printf("it takes %ld seconds,%ld micorseconds in v1\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec)); r = NULL; gettimeofday(&start,NULL); r = tree_search_v2(tree.root , max - 1); gettimeofday(&end,NULL); if(r == NULL) printf("not found\n"); else printf("found %d at 0x%x\n",r->data,(unsigned int)r); printf("it takes %ld seconds,%ld micorseconds in v2\n",(long)(end.tv_sec - start.tv_sec),(long)(end.tv_usec - start.tv_usec)); tnode_t * min = tree_min(tree.root); if(min != NULL) printf("min is %d\n",min->data); tnode_t * mx = tree_max(tree.root); if(mx != NULL) printf("max is %d\n",mx->data); tnode_t *s = tree_successor(min); tnode_t *pre = tree_predecessor(s); printf("successor of min is %d\n",s->data); printf("predecessor of s is %d\n",pre->data); tnode_t *pre2 = tree_predecessor(mx); tnode_t *s2 = tree_successor(pre2); printf("predecessor of max is %d\n",pre2->data); printf("successor of pre2 is %d\n",s2->data); tree_delete(&tree, min); printf("delete\n"); inorder_walk_tree(tree.root,print_tnode); printf("\n"); printf("deep walk\n"); deep_walk(tree.root); tree_destroy(tree.root); }