注册 登录
编程论坛 数据结构与算法

(线索化二叉树)小弟根据数据结构书上的改了。为什么还是会出赋值失败。应该是指针问题。麻烦大神给于小弟解说。

dengluoy 发布于 2013-04-10 18:54, 577 次点击
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef enum {Link,Prik} Order;
typedef struct node
{
    struct node *Lchild;
    struct node *Rchild;
    Order Rchd,Lchd;
    char ch;
}Node, *Base;
Base per;
void prit(Base T);
void Creat(Node **T);
void Insertding(Node **T,Base *Ther);
void Insert(Node **T);
void do_prit(Node **T);
void main()
{
    Base T=NULL,Ther = NULL;
    Creat(&T);
    prit(T);
    Insertding(&T,&Ther);
    do_prit(&Ther);
}
void Creat(Node **T)
{
    char ch;
    scanf(" %c",&ch);
    if(ch == '0')
        *T = NULL;
    else
    {
        *T = (Node *)malloc(sizeof(Node));
        assert((*T));
        (*T)->ch = ch;
        (*T)->Lchd = Link;
        (*T)->Rchd = Link;
        (*T)->Lchild = (*T)->Rchild = NULL;
        Creat(&(*T)->Lchild);
        Creat(&(*T)->Rchild);
    }
}
void prit(Node *T)
{
    if(T!=NULL)
    {
        prit(T->Lchild);
        printf("%c",T->ch);
        prit(T->Rchild);
    }
}
void Insertding(Node **T,Base *Ther)
{
    *Ther =(Base)malloc(sizeof(Node));
    assert(*Ther);
    (*Ther)->Lchd = Link;
    (*Ther)->Rchd = Prik;
//    (*Ther)->Rchild = *Ther;
    if(*T)
    {
        (*Ther)->Lchild = *T;
        per = *Ther;
        Insert(&(*T));
        per->Rchd = Prik;
        per->Rchild = *Ther;
        (*Ther)->Rchild = per;
    }
    else
    {
        (*Ther)->Lchild =*Ther;
    }
}
void Insert(Node **T)             //线索化
{
    if(*T)
    {
        Insert(&(*T)->Lchild);
        if(!(*T)->Lchild)
        {
            printf("%c",(*T)->ch);
            (*T)->Lchd = Prik;              //这个已经调试过赋值是成功的。
            (*T)->Lchild = per;
        }
        if(!per->Rchild)
        {
            per->Rchd = Prik;
            per->Rchild = *T;                                                
        }
        per = *T;
        Insert(&(*T)->Rchild);
    }
}
void do_prit(Node **T)
{
    Base p= NULL;
    p = (*T)->Lchild;
    while(p!=*T)
    {
        while(p->Lchd == Prik)
            p = p->Lchild;
        printf("name=%5c",p->ch);
        printf("ss=%5d",p->Lchd);
        printf("%p",p);
        while(p->Rchd == Prik && p->Rchild !=*T)      //该p->Rchd 并不为Prik。(1);但是线索函数已经赋值成功了。
        {
            p = p->Rchild;
            printf("%c",p->ch);
            printf("ss%5d",p->Lchd);
        }
        p = p->Rchild;
    }
}
0 回复
1