| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 849 人关注过本帖
标题:求各位高手帮看一段二叉树程序
只看楼主 加入收藏
『点点滴滴』
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:168
专家分:1035
注 册:2007-7-9
收藏
得分:0 
Trans=T->Lchild;
        while(Trans->Rchild)
        {
            Temp=Trans;
            Trans=Trans->Rchild;
        }
        T->elem=Trans->elem;
        if(Temp!=T)
            Temp->Rchild=Trans->Rchild;
        else
            Temp->Lchild=Trans->Lchild;
        free(Trans);
看了下这块应该没什么问题,Trans是待删除节点,Temp保存的它的前驱节点。
2011-02-13 08:51
CCFzeroOH
Rank: 2
等 级:论坛游民
帖 子:79
专家分:85
注 册:2009-12-22
收藏
得分:4 
删除那需要有前驱结点
要不然你删除了本节点并且释放
前驱结点的指针接变成野指针了,所以报错
2011-02-13 10:49
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
收藏
得分:4 
『点点滴滴』他们说得没有错

小代码,大智慧
2011-02-13 16:57
点线面
Rank: 8Rank: 8
来 自:NO.-1
等 级:蝙蝠侠
帖 子:525
专家分:980
注 册:2011-1-3
收藏
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct BiTree
{
    int elem;
    struct BiTree *Lchild;
    struct BiTree *Rchild;
}BiNode;

void BiT_Create(BiNode *T,int x)
{
    BiNode *Temp;
    BiNode *New;

    Temp=T;
    if(T->elem==-1)
    {
        T->elem=x;
        T->Lchild=NULL;
        T->Rchild=NULL;
        return;
    }
    else if(Temp->elem>=x)
    {
        if(Temp->Lchild==NULL)
        {
            if((New=(BiNode*)malloc(sizeof(BiNode)))!=NULL)
            {
                New->elem=x;
                New->Lchild=NULL;
                New->Rchild=NULL;
                Temp->Lchild=New;
            }
        }
        else
            BiT_Create(Temp->Lchild,x);
    }
    else
    {
        if(Temp->Rchild==NULL)
        {
            if((New=(BiNode*)malloc(sizeof(BiNode)))!=NULL)
            {
                New->elem=x;
                New->Lchild=NULL;
                New->Rchild=NULL;
                Temp->Rchild=New;
            }
        }
        else
            BiT_Create(Temp->Rchild,x);
    }
}
void BiT_Init(BiNode *T,int array[],int size)
{
    int i;
    for(i=0;i<size;i++)
        BiT_Create(T,array[i]);
}

void BiT_Print(BiNode *T)
{
    BiNode *Temp;
    Temp=T;
    if(!Temp)
        return;

    BiT_Print(Temp->Lchild);
    printf("%d ",Temp->elem);
    BiT_Print(Temp->Rchild);

}
int BiT_Search(BiNode *T,int x,int size)
{
    int i=0;
    BiNode *Temp;
    Temp=T;
    if(i<size)
    {
        if(Temp->elem==x)
            return 1;
        else if(Temp->elem>=x&&Temp->Lchild!=NULL)
            BiT_Search(Temp->Lchild,x,size);
        else if(Temp->elem<x&&Temp->Rchild!=NULL)
            BiT_Search(Temp->Rchild,x,size);
        else
            return 0;
    }

}

void BiT_Delete(BiNode *pre,BiNode *T)
{
    BiNode *Temp;
    BiNode *Trans;
    Temp=T;

    if(Temp->Lchild==NULL)          //判断当前结点左孩子是否为零       
    {
        if(Temp==pre->Lchild)       //判断前驱结点
        {
            pre->Lchild=Temp->Rchild;
        }
        else
            if(Temp==pre->Rchild)
            {
            pre->Rchild=Temp->Rchild;
            }

       // free(Temp);              //就算没有这个不影响结果
    }
    else if(Temp->Rchild==NULL)    
    {
        if(Temp==pre->Lchild)       //判断前驱结点
        {
            pre->Lchild=Temp->Lchild;
   
        }
        else
            if(Temp==pre->Rchild)
            {
            pre->Rchild=Temp->Lchild;

            }
        //free(Temp);             //就算没有这个不影响结果

    }
    else
    {   
        Trans=T->Lchild;
        while(Trans->Rchild)
        {
            Temp=Trans;
            Trans=Trans->Rchild;
        }
        T->elem=Trans->elem;
        if(Temp!=T)
            Temp->Rchild=Trans->Rchild;
        else
            Temp->Lchild=Trans->Lchild;
       // free(Trans);            //就算没有这个不影响结果

    }
}

int BiT_Delenode(BiNode *pre,BiNode *T,int x)
{
    BiNode *Temp;
    Temp=T;

    if(!Temp)
        return 0;
    else
    {
        if(Temp->elem==x)
        {
            BiT_Delete(pre,Temp);
            return 1;
        }
        else if(Temp->elem>x)
            BiT_Delenode(T,Temp->Lchild,x);
        else
            BiT_Delenode(T,Temp->Rchild,x);
    }
}

int main()
{
    BiNode *T;
    int *array;
    int size;
    int i;

    printf("Input the size of the ARRAY:");
    scanf("%d",&size);
    array=(int*)malloc(size*sizeof(int));
    for(i=0;i<size;i++)
    {
        printf("Input the elem of No.%d in the ARRAY:",i);
        scanf("%d",&array[i]);
    }

    T=(BiNode*)malloc(sizeof(BiNode));
    T->elem=-1;
    T->Lchild=NULL;
    T->Rchild=NULL;

    BiT_Init(T,array,size);
     BiT_Print(T);
     putchar('\n');
    BiT_Delenode(T,T,3);
    BiT_Print(T);

    getchar();
    getchar();
    free(T);
    return 1;
}

不过如果连续输入3 3 3 3结果不正常
或者第一个是3时候


[ 本帖最后由 点线面 于 2011-2-13 17:56 编辑 ]

小代码,大智慧
2011-02-13 17:50
yxm870915
Rank: 1
等 级:新手上路
帖 子:18
专家分:2
注 册:2011-2-10
收藏
得分:0 
吼吼,谢谢各位了,我找到错误了~~
2011-02-13 23:51
jefffyang
Rank: 1
等 级:新手上路
帖 子:10
专家分:7
注 册:2011-2-10
收藏
得分:0 
二叉树我也有兴趣
2011-02-14 09:51
快速回复:求各位高手帮看一段二叉树程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016809 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved