スレッド二叉樹(作業二)


以下にいくつかの が示されている.
// A code block
var foo = 'bar';
// An highlighted block
#include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;

typedef enum {
      Link, Thread } PointerTag;

typedef struct BiThrNode {
     
    TElemType data;
    struct BiThrNode* lchild, * rchild;
    PointerTag LTag;
    PointerTag RTag;
}BiThrNode, * BiThrTree;
void    visit(BiThrTree T) {
     

	printf("%c", T->data);


}
//        
Status CreateBiThrNode(BiThrTree* B) {
     
    char ch;
    scanf_s("%c", &ch);
    if (ch == '#') *B = NULL;
    else {
     
        if (!((*B) = (BiThrNode*)malloc(sizeof(BiThrNode)))) exit(OVERFLOW);
        (*B)->data = ch;
        (*B)->LTag = Link;
        (*B)->RTag = Link;
        CreateBiThrNode(&(*B)->lchild);
        CreateBiThrNode(&(*B)->rchild);
    }
    return OK;
}

//        
void InThreading(BiThrTree B, BiThrTree* pre) {
     
    if (!B) return;

    InThreading(B->lchild, pre);

    if (!B->lchild) {
     
        B->LTag = Thread;//           
        B->lchild = *pre;
    }

    if (!(*pre)->rchild) {
     
        (*pre)->RTag = Thread;//         ;
        (*pre)->rchild = B;
    }

    *pre = B;
    InThreading(B->rchild, pre);
}

//           ,        
Status InOrderThreading(BiThrTree* Thrt, BiThrTree T) {
     
    if (!(*Thrt = (BiThrTree)malloc(sizeof(BiThrNode)))) exit(OVERFLOW);
    (*Thrt)->LTag = Link;//        
    (*Thrt)->RTag = Thread;
    (*Thrt)->rchild = (*Thrt);
    if (!T) {
     
        (*Thrt)->lchild = (*Thrt);
        return OK;       //       ,       ,         .
    }
    BiThrTree pre;
    //             
    pre = (*Thrt);
    (*Thrt)->lchild = T;
    //         
    InThreading(T, &pre);
    //                ,                     .
    //                 ,                 .
    pre->rchild = *Thrt;
    pre->RTag = Thread;//         ;
    (*Thrt)->rchild = pre;
    return OK;
}

//          
Status InOrderTraverse(BiThrTree T) {
     
    BiThrTree p = T->lchild;

    while (p != T) {
     
        while (p->LTag == Link)p = p->lchild;//       ;
        visit(p);//
        while (p->RTag == Thread && p->rchild != T) {
     //          ;

            p = p->rchild;
            visit(p);
        }
        p = p->rchild;

    }

    return OK;

}

int main() {
     
    BiThrTree B, T;
    CreateBiThrNode(&B);
    InOrderThreading(&T, B);
    printf("   :");
    InOrderTraverse(T);
 
}

//    :abc##de#g##f###