标题:二叉树的线索化
只看楼主
dome14426
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-3-31
 问题点数:0 回复次数:0 
二叉树的线索化
#include "Stdio.h"
#include "Conio.h"
#include "stdlib.h"
typedef struct   threadNode/* 线索节点*/
{
    char data;
    int  lTag,rTag;
    struct  threadNode *lChild,*rChild;
   
}threadNode,*threadTreePtr;
threadTreePtr initTree(void)/*建树*/
{
    char ch;
    threadTreePtr t;
    ch=getchar();
    if(ch=='0')
        t=NULL;
    else
    {
        t =(threadTreePtr) malloc(sizeof(threadNode));
        t->data=ch;
        t->lChild=initTree();
        t->rChild=initTree();
    }
    return t;
   
}

void createThreadTree(threadTreePtr t,threadTreePtr pre)/*建成线索树*/
{
    if(t)
    {
        createThreadTree( t->lChild, pre);
        if(t->lChild==NULL)
        {
            t->lTag=1;
            t->lChild=pre;
        }
        if(t->rChild==NULL)
            t->rTag=1;
        if(pre&&(pre->rTag==1))
            pre->rChild=t;
        pre=t;
        createThreadTree( t->rChild,pre);
    }
}

threadTreePtr inPostNode(threadTreePtr p)
{
    threadTreePtr post;
    post=p->rChild;
    if(p->rTag==0)
        while(post->rTag==0)
            post=post->lChild;
        return post;
        
}
void showThTree(threadTreePtr t)/*遍历线索树*/
{
    threadTreePtr p ;
    if(t)
    {
        p=t;
        while(p->lTag==0)
            p=p->lChild;
        while(p)
        {
            printf("%c\t",p->data);
            inPostNode(p) ;
        }
        
    }
   
   
}

void show(threadTreePtr th)/*遍历树*/
{
    if(th)
    {
        printf("%c\t",th->data);
        show(th->lChild);
        show(th->rChild);
    }
}
int main(void)
{
    threadTreePtr  tree ,thTree;
    tree =(threadTreePtr)malloc(sizeof(threadNode));
    thTree=(threadTreePtr)malloc(sizeof(threadNode));
    tree = initTree();
    show(tree);
    printf("\n");
    createThreadTree(tree,thTree);
    showThTree(tree);
    getch();
    return 0;
}
/*源码于上,问题出在线索化的过程中。哪位帮我改改,感激不尽*/
搜索更多相关主题的帖子: 线索 二叉树 
2009-12-02 11:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-293662-1-1.html




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

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