| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 469 人关注过本帖, 1 人收藏
标题:中序线索化二叉树后空指针没有改变,谁能帮我分析下,谢谢
只看楼主 加入收藏
大虫归来
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-14
结帖率:50%
收藏(1)
已结贴  问题点数:20 回复次数:2 
中序线索化二叉树后空指针没有改变,谁能帮我分析下,谢谢
按先序遍历输入二叉树结点:AB##CD##E##,中序线索二叉树后B的右孩子的后继指针还是空,求指点
[code][#include <stdio.h>
#include <stdlib.h>

typedef enum _PointerTag{
    Link,    //Link=0指针
        Thread    //Thread线索
}PointerTag;

typedef struct _ThreadNode{
    char elem;
    PointerTag ltag,rtag;
    struct _ThreadNode *lchild,*rchild;
}ThreadNode,*ThreadTree;

/*
创建二叉树
*/
void CreateThreadTree(ThreadTree *T)
{
    char ch;
   
    ch = getchar();
   
    if(ch == '#')*T = NULL;
    else
    {
        *T = (ThreadNode *)malloc(sizeof(ThreadNode));
        printf("0x%x\r\n",*T);
        (*T)->elem = ch;
        CreateThreadTree(&((*T)->lchild));
        CreateThreadTree(&((*T)->rchild));
    }   
}

/*
先序遍历二叉树
*/
void PreThreadTreeOrder(ThreadTree T)
{
    if(T)
    {
        printf("%3c",T->elem);
        PreThreadTreeOrder(T->lchild);
        PreThreadTreeOrder(T->rchild);
    }
}

/*
中序遍历二叉树
*/
void MidThreadTreeOrder(ThreadTree T)
{
    if(T)
    {
        MidThreadTreeOrder(T->lchild);
        printf("%3c",T->elem);
        MidThreadTreeOrder(T->rchild);
    }
}

/*
后序遍历二叉树
*/
void PostThreadTreeOrder(ThreadTree T)
{
    if(T)
    {
        PostThreadTreeOrder(T->lchild);
        PostThreadTreeOrder(T->rchild);
        printf("%3c",T->elem);
    }
}

/*
中序线索化二叉树
*/
void MidThreadTree(ThreadTree T,ThreadTree pre)
{
    if(T)
    {
        //printf("pre = 0x%x\r\n",pre);
        MidThreadTree(T->lchild,pre);
        
        if(T->lchild == NULL)
        {
            T->ltag = Thread;
            T->lchild = pre;
        }
        else T->ltag = Link;
        
        if(T->rchild == NULL)
        {
            T->rtag = Thread;
        }
        else T->rtag = Link;

        if(pre->lchild == NULL)pre->ltag = Thread;
        else pre->ltag = Link;

        if(pre->rchild == NULL)pre->rtag = Thread;
        else pre->rtag = Link;
        
        if(pre && pre->rtag == Thread)pre->rchild = T;
        pre = T;
        
        MidThreadTree(T->rchild,pre);
    }
}

/*
在中序线索化二叉树中寻找结点的前驱
*/
ThreadTree MidFindPreElem(ThreadTree T)
{
    ThreadTree pre;
   
    pre = T->lchild;
    if(T->ltag == Thread)return pre;
    else
    {
        while(pre->rtag == Link)
        {
            pre = pre->rchild;
        }
    }
   
    return pre;
}

/*
在中序线索化二叉树中寻找结点的后继
*/
ThreadTree MidFindNextElem(ThreadTree T)
{
    ThreadTree next;
   
    next = T->rchild;
    if(T->rtag == Thread)return next;
    else
    {
        while(next->ltag == Link)
        {
            next = next->lchild;
        }
    }
   
    return next;
}

/*
遍历中序线索化二叉树
*/
void MidThreadTreeTraverse(ThreadTree T)
{
    ThreadTree p;

    if(T)
    {
        p = T;
        while(p->ltag == Thread)p = p->lchild;
        while(p)
        {            
            printf("%3c\r\n",p->elem);
            printf("0x%x",p);
            p = MidFindNextElem(p);
            printf("\t0x%x\r\n",p);
        }
    }
}

/*
寻找中序线索化二叉树中的某个结点
*/
ThreadTree MidFindThreadTreeElem(ThreadTree T,char elem)
{
    ThreadTree p;
   
    if(T)
    {
        p = T;
        while(p->ltag == Thread)p = p->lchild;
        while(p && p->elem != elem)
        {
            p = MidFindNextElem(p);
        }
    }
   
    return p;
}

void OperateThreadTree(void)
{
    ThreadTree T,p,pre=NULL;
   
    printf("按先序次序输入二叉树的结点(结点为字符,字符'#'表示空树):\r\n");
    CreateThreadTree(&T);
    printf("先序遍历二叉树的结点: \r\n");
    PreThreadTreeOrder(T);
    printf("\r\n");
    printf("中序遍历二叉树的结点: \r\n");   
    MidThreadTreeOrder(T);
    printf("\r\n");
    printf("后序遍历二叉树的结点: \r\n");
    PostThreadTreeOrder(T);
    printf("\r\n");
   
    p = T;
    printf("AAAAAAAAAAAAAAAAAA\r\n");
    printf("pre = 0x%x\r\n",pre);
    printf("0x%x\r\n",T);
    printf("0x%x\r\n",T->lchild);
    printf("0x%x\r\n",T->lchild->lchild);

    MidThreadTree(T,pre);
   
    printf("\r\n\r\n");
    printf("BBBBBBBBBBBBBBBBBB\r\n");
    printf("pre = 0x%x\r\n",pre);
    printf("0x%x\r\n",T);
    printf("0x%x\r\n",T->lchild);
    printf("0x%x\r\n",T->lchild->lchild);
    printf("中序线索二叉树的遍历:\r\n");
    MidThreadTreeTraverse(T);
    printf("\r\n");
   
}

int main(void)
{
    OperateThreadTree();
   
    return NULL;
}/code]
搜索更多相关主题的帖子: include 二叉树 
2013-12-18 16:49
wangdayong99
Rank: 2
等 级:论坛游民
帖 子:9
专家分:97
注 册:2013-12-24
收藏
得分:10 
回复 楼主 大虫归来
//没有函数调用MidFindPreElem这个函数,pre的值没有变化,所以在void MidThreadTree(ThreadTree T,ThreadTree pre)中的pre值为空,程序执行去pre的lchild时出现异常。

ThreadTree MidFindPreElem(ThreadTree T)
{
    ThreadTree pre;

    pre = T->lchild;
    if(T->ltag == Thread)return pre;
    else
    {
        while(pre->rtag == Link)
        {
            pre = pre->rchild;
        }
    }

    return pre;
}
2013-12-25 13:16
so_love
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:蒙面侠
威 望:7
帖 子:812
专家分:4151
注 册:2013-11-25
收藏
得分:10 
参观

一花一世界、一叶一追寻、片片花叶落、情系何人身。
2013-12-25 13:43
快速回复:中序线索化二叉树后空指针没有改变,谁能帮我分析下,谢谢
数据加载中...
 
   



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

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