0

我有一个有 2 行的二叉树:keycombination. 当我尝试删除组合长于 5 位的元素时出现问题。
我称之为ifMore(root);当它找到组合长度超过 5 位的元素时,它会调用DeleteTooLong().
因此,当我启动程序时,它会删除 2 个 ( b, l) 元素,然后出现 (on i) "Segmentation fault: 11"

这是我的树:

h - 001
d - 002
b - 004444
a - 0080
c - 009
f - 005
e - 010
g - 011
l - 003333
j - 006
i - 0121212
k - 013
n - 007
m - 0141414
o - 015



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

int point =0;
typedef struct key_combinations
{
    char *key;
    char *combination;
} KEY_COMBINATION;


typedef struct tree
{
    KEY_COMBINATION kc;
    struct tree *left,*right;
}Tree;

Tree *root;

void AddNode (Tree * proot, Tree * pnew) ;
void GetData ();
void PrintTree();
void ShowTree(Tree *rooot, int lvl);
void PutTree(Tree *rooot);
void ifMore(Tree *rooot);
void DeleteTooLong(Tree **rooot);
void FreeNode (Tree * node) ;


void main()
{
    GetData ();
    PrintTree();
    ifMore(root);
    PrintTree();
}

void GetData ()
{
    Tree * pel;
    KEY_COMBINATION * pkc;

    char bufk[3],bufc[25];
    FILE* f;

    int choise;
    printf("choose file balanced[1] not balanced[2] :   ");
    scanf("%d",&choise);

    if(choise==2)
        f = fopen("TreeNB","r");
    else
        f = fopen("TreeB","r");

    while(fscanf(f,"%s %s",bufk,bufc)!=EOF)
    {
        pkc->key = (char * ) malloc(strlen(bufk)+1);
        strcpy(pkc->key, bufk);

        pkc->combination =(char * ) malloc(strlen(bufc)+1);
        strcpy(pkc->combination, bufc);

        pel=(Tree *)malloc(sizeof(Tree));
        pel->kc = * pkc;

        pel->left = NULL;
        pel->right = NULL;

        AddNode(root,pel);
    }
}

void AddNode (Tree * proot, Tree * pnew)
{
    if (root == NULL)
       root = pnew;
    else
        if (strcmp(proot->kc.key, pnew->kc.key)>0)
            if (proot->left == NULL)
                proot->left = pnew;
            else
                AddNode(proot->left, pnew);
        else
            if (proot->right == NULL)
                proot->right = pnew;
            else
                AddNode(proot->right, pnew);
}

void PrintTree()
{
    puts("\n===============================");
    PutTree(root);
    puts("\n===============================\n");
    ShowTree(root,1);
    puts("===============================");
}

void ShowTree(Tree *rooot, int lvl)
{
    if(rooot==NULL)
        return;

    ShowTree(rooot->right,lvl+1);
    printf("%*c%s\n", lvl*3,' ', rooot->kc.key);
    ShowTree(rooot->left,lvl+1);
}

void PutTree(Tree *rooot)
{
    if(rooot==NULL)
        return;
    printf("\n\t%s -\t%s", rooot->kc.key, rooot->kc.combination);
    PutTree(rooot->left);
    PutTree(rooot->right);
}

void ifMore(Tree *rooot)
{

    if(rooot==NULL)
        return;
    if(strlen(rooot->kc.combination)>5){
        printf("\n\t%s -\t%s", rooot->kc.key, rooot->kc.combination);
        DeleteTooLong(&rooot);
    }
    ifMore(rooot->left);
    ifMore(rooot->right);
}

void DeleteTooLong(Tree **rooot)
{
    Tree *treeroot=*rooot, *newroot, *parent;
    int subtrees;
    if(rooot==NULL)
        return;

    if(treeroot->left==NULL && treeroot->right==NULL)
        subtrees=0;
    else if(treeroot->left==NULL)
        subtrees=-1;
    else if(treeroot->right==NULL)
        subtrees=1;
    else
        subtrees=2;

    switch(subtrees)
    {
    case  0: *rooot=NULL;             break;
    case  1: *rooot=treeroot->left;   break;
    case  -1: *rooot=treeroot->right; break;
    case  2:
        newroot=treeroot->left;
        parent=treeroot;
        while(newroot->right!=NULL)
        {
            parent=newroot;
            newroot=newroot->right;
        }

        newroot->right=treeroot->right;
        if(parent!=treeroot)
        {
            parent->right=newroot->left;
            newroot->left=treeroot->left;
        }
        *rooot=newroot;
    }
    printf("\n point : %d",point++);
    printf("\t free...");
    FreeNode(treeroot);
}

void FreeNode (Tree * node)
{
    free(node->kc.key);
    free(node->kc.combination);
    free(node);
}
4

0 回答 0