坚持可持续 2018-04-10 07:31 采纳率: 0%
浏览 823
已结题

C语言求大神帮忙看看写的二叉树删除,为什么我的这道程序执行顺序异常

写的一道数据结构树二叉树删除的作业
关于数据结构的正确与否不需要大神考虑,只要帮我看看为什么这道程序执行顺序这么奇怪
代码比较长:请关注main函数里面出现的几个函数就行了,其他不用管

 #define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

typedef struct aaa {
    int key;
    char    name[20];
    char    address[100];
} element;

typedef struct node *nodePointer;

typedef struct node {
    nodePointer leftChild;

    element data;

    nodePointer rightChild;
}node;

node* add( nodePointer *q );


void printall( struct node  *q );


void searchSmallestAmongLargerNodes( nodePointer search, nodePointer* parentSmall, nodePointer *searchSmall )
{
    nodePointer temp = search->rightChild;

    if ( temp->rightChild = NULL )
    {
        *parentSmall    = search;
        *searchSmall    = temp;
    }else  { *parentSmall   = temp;
         *searchSmall   = temp->rightChild; }
}


/*  nodePointer root = NULL; */
int searchParentToInsert( struct node *root, int key, struct node **parentPtr )
{
    int     found   = 0;
    struct  node    *parent = NULL, *search = root;
    while ( search != NULL )
    {
        if ( key == search->data.key )
        {
            found = 1;
            break;
        }else  {
            parent = search;
            if ( key < search->data.key )
                search = search->leftChild;
            else
                search = search->rightChild;
        }
    }
    *parentPtr = parent;
    return(found);
}


void insertBST( struct node **rootPtr, element x )
{
    struct node *ptr, *parent;

    int found = 0;
    found = searchParentToInsert( *rootPtr, x.key, &parent );
    if ( found == 1 )
    {
        printf( "The key already exists in the tree!\n" );
    } else{
        ptr     = (node *) malloc( sizeof(node) );
        ptr->data   = x;
        ptr->leftChild  = ptr->rightChild = NULL;
        if ( *rootPtr == NULL )
        {
            *rootPtr = ptr;
        } else {
            if ( x.key < parent->data.key )
                parent->leftChild = ptr;
            else parent->rightChild = ptr;
        }
    }
}


int  deleteBST( struct node **rootPtr, int key )
{
    int     find1 = 0;
    struct node **root, **parent, *search, *parentSmall, *searchSmall;
    root    = (struct node * *    ) malloc( sizeof(struct node*) );
    *root   = *rootPtr;
    parent  = (struct node * *    ) malloc( sizeof(struct node*) );

    search      = (struct node *    ) malloc( sizeof(struct node) );
    searchSmall = (struct node *    ) malloc( sizeof(struct node) );


    find1 = searchParentToInsert( *root, key, parent );
    if ( find1 == 0 )
    {
        return(0);
    }    /* NOT FOUND} */
    /* case 1. Deletion of a leaf node. */
    if ( (search->leftChild == NULL) && (search->rightChild == NULL) )
        free( search );

    if ( (search->leftChild == NULL) || (search->rightChild == NULL) )
    {
        { if ( search->leftChild != NULL )
              (*parent) = (*parent)->leftChild;
          else
              (*parent) = (*parent)->rightChild; }

        if ( (search->leftChild != NULL) && (search->rightChild != NULL) )
        {
            /* find the smallest among the larger than the key */
            searchSmallestAmongLargerNodes( search, &parentSmall, &searchSmall );
            search->data.key = searchSmall->data.key;

            if ( search == parentSmall )
                parentSmall->rightChild = searchSmall->rightChild;
            else parentSmall->leftChild = searchSmall->rightChild;
            free( searchSmall );
        }
    }
    return(0);
}


int main( void )
{
    int     cc;
    nodePointer q;
    q = (struct node *) malloc( sizeof(struct node) );

    q = add( &q );

    cc = deleteBST( &q, 3 );

    printall( q );
}


nodePointer add( nodePointer *q )
{
    int coun = 0;

    while ( 1 )
    {
        struct node *newnode = NULL;
        newnode = (struct node *) malloc( sizeof(struct node) );
        scanf( "%d,", &newnode->data.key );
        if ( newnode->data.key == -1 )
        {
            return(*q);
        }
        newnode->leftChild  = NULL;
        newnode->rightChild = NULL;
        scanf( "%[^,]%[^\n]", newnode->data.name, newnode->data.address );
        fflush( stdin );
        /* gets_s(newnode->add); */

        if ( coun == 0 )
        {
            *q = newnode;
            coun++;
        }else  { insertBST( q, newnode->data ); }
    }

    return(*q);
}


void printall( struct node  *q )  /* inorder */
{
    struct node *temp = q;
    if ( temp )
    {
        printall( temp->leftChild );
        printf( "%d %s %s\n", temp->data.key, temp->data.name, temp->data.address );
        printall( temp->rightChild );
    }
}

我输入了测试代码是
1,asdf,sdf
2,dsf,sdf
3,sdf,sdf
-1

首先看下 nodePointer add(nodePointer *q) 函数(add函数建立二叉树)
循环输入知道输入-1结束
但是当我输入第一行1,asdf,sdf结束的时候,就会main函数从上到下过一下(我用了断点调试)
输入第二行第三行也是这样

输入-1结束输入的时候

cc=deleteBST(&q, 3);这一行直接跳过
(按照顺序执行,这一行应该执行啊)
(deleteBST删除二叉树中的 3 )
图片说明

请大神输入我的测试代码调试一下
20分等解决问题的大神来拿哦

  • 写回答

2条回答 默认 最新

  • netlocks 2018-04-10 07:59
    关注

    将free(S) 放在 renturn 0; 前面
    因为 return 0;后程序退出, 不会调用free(S) 去释放内存

    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)