cxfans
8/28/2019 - 6:31 AM

ThreadTree

ThreadTree

#include <stddef.h>
#include <stdbool.h>

#define ElemType int

typedef struct ThreadNode {
    ElemType data;
    struct ThreadNode *rchild, *lchild;
    int rtag, ltag;
} ThreadNode, *ThreadTree;

ElemType visit(ThreadNode T) {
    return T.data;
}

void InThread(ThreadTree p, ThreadTree pre) {
    if (p != NULL) {
        InThread(p->lchild, pre);
        if (p->lchild == NULL) {
            p->lchild = pre;
            p->ltag = 1;
        }
        if (pre != NULL && pre->rchild == NULL) {
            pre->rchild = p;
            p->rtag = 1;
        }
        pre = p;
        InThread(p->rchild, pre);
    }

}

void CreateTnThread(ThreadTree T) {
    ThreadTree pre = NULL;
    if (T != NULL) {
        InThread(T, pre);
        pre->rchild = NULL;
        pre->rtag = 1;
    }
}

ThreadNode *FirstNode(ThreadNode *p) {
    while (p->ltag == 0)p = p->lchild;
    return p;
}

ThreadNode *NextNode(ThreadNode *p) {
    if (p->rtag = 0)return FirstNode(p->rchild);
    else return p->rchild;
}

void InOrder(ThreadNode *T) {
    for (ThreadNode *p = FirstNode(T); p != NULL; p = NextNode(p))
        visit(p);
}