从二叉树

时间:2017-04-24 14:57:34

标签: c binary-tree

标题解释了这一切,我的删除功能似乎发生了一些奇怪的事情,我似乎无法弄清楚。我认为它基于标准的打印语句而停留在程序的递归部分。如果有人能提供帮助,那将非常感激

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


typedef struct btree
{
    char name[10];
    //int data;
    struct btree *rlink;
    struct btree *llink;
}node;
void insert(node**, char[]);
void inorder(node*);
void preorder(node*);
void postorder(node*);
int charToNum(node*);
node* findmin(node*);
node* findmax(node*);
node* del(node*, char[]);

int main(void)
{
    node *root;
    root = (node*)malloc(sizeof(node));
    root = NULL;
    int ch, x;
    x = 0;
    char num[10];
    while (1)
    {
        printf("\n 1.insert2.inorder3.preorder4.postorder5.findmin6.findmax7.Delete");
        printf("\n Enter your choice>");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("\n Enter node to insert>");
            scanf("%s", &num);
            insert(&root, num);
            break;
        case 2:
            printf("\n inorder display");
            inorder(root);
            break;
        case 3:
            printf("\n pre-order display");
            preorder(root);
            break;
        case 4:
            printf("\n post-order display");
            postorder(root);
            break;
        case 5:
            printf("\n Min in the tree is %s", findmin(root)->name);
            break;
        case 6:
            printf("\n Max in the tree is %s", findmax(root)->name);
            break;
        case 7:
            printf("\n enter the node to delete>");
            scanf("%s", &num);
            root = del(root, num);
            break;

        case 8:
            exit(0);


        }
    }
}
void insert(node** h, char info[10])
{
    if (*h == NULL)
    {

        node *temp;
        temp = (node*)malloc(sizeof(node));
        strcpy((temp->name), info);
        //temp->name = info;
        temp->llink = NULL;
        temp->rlink = NULL;
        *h = temp;
        printf("successfully added %s\n", temp->name);
    }
    else if (info > (*h)->name) {
        insert(&(*h)->rlink, info);
    }
    else if (info <(*h)->name)
        insert(&(*h)->llink, info);
}
void inorder(node* h)
{
    if (h != NULL)
    {
        inorder(h->llink);
        printf("%s->", h->name);
        inorder(h->rlink);
    }
}

void preorder(node* h)
{
    if (h != NULL)
    {
        printf("%s->", h->name);
        preorder(h->llink);

        preorder(h->rlink);
    }
}

void postorder(node* h)
{
    if (h != NULL)
    {

        postorder(h->llink);

        postorder(h->rlink);
        printf("%s->", h->name);
    }
}

node* findmin(node* h)
{
    if (h->llink == NULL)
        return h;
    else
        findmin(h->llink);
}

node* findmax(node* h)
{
    if (h->rlink == NULL)
        return h;
    else
        findmax(h->rlink);
}

node* del(node *h, char info[])
{

    if (h == NULL)
        return h;

    else if (info > h->name) {
        printf("info > h->name\n");
        h->rlink = del(h->rlink, info);
    }
    else if (info < h->name) {
        h->llink = del(h->llink, info);
        printf("info < h->name\n");
    }
    else
    {
        if (h->llink == NULL && h->rlink == NULL)
        {
            printf("first if excuting");
            /*node *tmp;
            tmp = (node*)malloc(sizeof(node));
            tmp = h;*/
            h = NULL;

            return h;
        }
        else if (h->llink == NULL)
        {
            node *tmp;
            tmp = (node*)malloc(sizeof(node));
            tmp = h;
            h = h->rlink;
            free(tmp);
            printf("else if finished\n");
        }
        else if (h->rlink == NULL)
        {
            printf("second else if is ecuting \n");
            node *tmp;
            tmp = (node*)malloc(sizeof(node));
            tmp = h;
            h = h->llink;
            free(tmp);
            printf("second else if finished\n");
        }
        else
        {
            printf("final else  is excuting\n");
            node *tmp;
            tmp = (node*)malloc(sizeof(node));
            tmp = findmin(h->rlink);
            strcpy((h->name),tmp->name);
            //h->name = tmp->name;
            h->rlink = del(h->rlink, tmp->name);
            free(tmp);
            printf("final else finished\n");
        }
    }
    return h;
}

0 个答案:

没有答案