C - 二进制搜索树,删除节点

时间:2017-08-29 06:28:42

标签: c tree binary-tree binary-search-tree nodes

我对我的代码有疑问。 " delete_single_node"不能正常工作。我的程序(删除节点)是在" head"中找到节点。树,临时保存子节点,删除节点和所有子节点(" head"树)并合并" head"具有临时保存的子节点的树到新的正确二叉树 我想继续这个程序,但我找不到我的错误。

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>


struct tnode {
    int content; 
    struct tnode *left; 
    struct tnode *right; 
};

struct tnode* head;

void *malloc(size_t size);
struct tnode *talloc(void)
{
    return(struct tnode*)malloc(sizeof(struct tnode));
}




/* delete a node with all child nodes*/
int deletenode(struct tnode *p)
{
    if (p == NULL) return 0;
    else
    {
        deletenode(p->left);
        deletenode(p->right);
        p->left = NULL;
        p->right = NULL;
        free(p);
        return 0;
    }
}


/* insert new node*/
struct tnode *addelement(struct tnode *p, int i) {
    int cond;
    if (p == NULL) {
        p = talloc(); 
        p->content = i;
        p->left = p->right = NULL;
    }
    else if (p->content == i) {
        return p;
    }
    else if (i < p->content)
        p->left = addelement(p->left, i);
    else 
        p->right = addelement(p->right, i);
    return p;
}

/*merges two binary trees into one*/
struct tnode *addtree(struct tnode *top, struct tnode *p) {
    if (p == NULL)
        return top;
    else
        return addtree(addtree(addelement(top, p->content), p->right), p->left);
}
/*prints out tree*/
void showtree(struct tnode* nd)
{
    if (nd == NULL)
        return;
    printf("ZZ %d", nd->content);
    if (nd->left != NULL)
    {
        printf("left:%d", nd->left->content);
    }
    if (nd->right != NULL)
    {
        printf("right:%d\n", nd->right->content);
    }
    printf("\n");
    showtree(nd->left);
    showtree(nd->right);
}
/*removes connection to a node*/
void removeconnection(struct tnode *head, struct tnode *p) {
    if (p->content > head->content) {
        if (head->right == p)
        {
            head->right = NULL;
        }
        else
        {
            removeconnection(head->right, p);
        }       
    }
    if (p->content < head->content) {
        if (head->left == p)
        {
            head->left = NULL;
        }
        else
        {
            removeconnection(head->left, p);
        }
    }
}


/*delete single node*/
struct tnode *delete_single_node(struct tnode *p, int content) {
    struct tnode* temp2 = NULL;
    struct tnode temp1;
    struct tnode* temp4 = NULL;
    struct tnode temp3;

    if (p == NULL)
        return NULL;
    if (content > p->content)
    {
        p->right = delete_single_node(p->right, content);
    }
    else if (content < p->content)
    {
        p->left = delete_single_node(p->left, content);
    }
    else if (content == p->content)
    {
        if (p->left == NULL && p->right != NULL)
        {

            temp1.content = p->right->content;
            temp1.right = p->right->right;
            temp1.left = p->right->left;
            temp2 = &temp1;

            removeconnection(head, p);
            deletenode(p); 
            head = addtree(head, temp2);
            showtree(head);
        }
        else if (p->right == NULL && p->left != NULL)
        {
            temp1.content = p->left->content;
            temp1.right = p->left->right;
            temp1.left = p->left->left;
            temp2 = &temp1;

            removeconnection(head, p);
            deletenode(p);
            head = addtree(head, temp2);        
            showtree(head);
        }
        else if (p->right == NULL && p->left == NULL)
        {
            removeconnection(head, p);
            deletenode(p);
            showtree(head);
        }
        else
        {
            temp1.content = p->left->content;
            temp1.right = p->left->right;
            temp1.left = p->left->left;
            temp2 = &temp1;

            temp3.content = p->right->content;
            temp3.right = p->right->right;
            temp3.left = p->right->left;
            temp4 = &temp3;

            removeconnection(head, p);
            deletenode(p);
            head = addtree(head, temp2);
            head = addtree(head, temp4);
            showtree(head);
        }
    }
}

int main()
{
    struct tnode* start = NULL;
    int temp_int;
    start = addelement(start, 5);
    start = addelement(start, 6);
    start = addelement(start, 2);
    start = addelement(start, 9);
    start = addelement(start, 3);
    start = addelement(start, 2);
    start = addelement(start, 1);
    start = addelement(start, 7);
    start = addelement(start, 8);

    showtree(start);

    printf("Which node to delete?\n");
    scanf_s("%d", &temp_int);
    head = start;
    delete_single_node(start, temp_int);
    return 0;
}

0 个答案:

没有答案