线索二叉树之「后序线索二叉树」(三叉链表实现遍历)

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

ThreadNode *pre=NULL;

//初始化一棵树
void initTree(ThreadTree &T){
    T = (ThreadNode *)malloc(sizeof(ThreadNode));
    ThreadNode *n2 =(ThreadNode *)malloc(sizeof(ThreadNode));
    ThreadNode *n3 =(ThreadNode *)malloc(sizeof(ThreadNode));
    ThreadNode *n4 =(ThreadNode *)malloc(sizeof(ThreadNode));
    ThreadNode *n5 =(ThreadNode *)malloc(sizeof(ThreadNode));
    
    T->data=1;
    T->ltag=T->rtag=0;

    n2->data=2;
    n2->ltag=n2->rtag=0;
    n2->lchild=NULL;
    n2->rchild=NULL;
    T->lchild=n2;
    n2->pnode=T;
    
    n3->data=3;
    n3->ltag=n3->rtag=0;
    n3->lchild=NULL;
    n3->rchild=NULL;
    T->rchild=n3;
    n3->pnode=T;
    
    n4->data=4;
    n4->ltag=n4->rtag=0;
    n4->lchild=NULL;
    n4->rchild=NULL;
    n2->lchild=n4;
    n4->pnode=n2;
    
    n5->data=5;
    n5->ltag=n5->rtag=0;
    n5->lchild=NULL;
    n5->rchild=NULL;
    n3->lchild=n5;
    n5->pnode=n3;
}
//线索化后序二叉树
void visit(ThreadNode *q){
    if (q->lchild==NULL) {
        q->lchild=pre;
        q->ltag=1;
    }
    if (pre!=NULL&&pre->rchild==NULL) {
        pre->rchild=q;
        pre->rtag=1;
    }
    pre=q;
}

void PostThread(ThreadTree T){
    if (T!=NULL) {
        if (T->ltag==0) {
            PostThread(T->lchild);
        }
        if (T->rtag==0) {
            PostThread(T->rchild);
        }
        visit(T);
    }
}

//后序线索化二叉树T
void CreatePostThread(ThreadTree T){
    if (T!=NULL) {
        PostThread(T);
        if (pre->rchild==NULL) {
            pre->rtag=1;
        }
    }
}

void printing(ThreadNode *q){
    printf("%d",q->data);
}

ThreadNode *FirstNode(ThreadNode *T){
    ThreadNode *q=T;
    while (q->ltag==0) {
        q=q->lchild;
    }
    if (q->rtag==0) {
        q=q->rchild;
    }
    return q;
}

ThreadNode *NextNode(ThreadNode *q){
    if (q->rtag==1) {
        return q->rchild;
    }
    if (q->pnode!=NULL) {
        //当前结点是父结点的右孩子,那么后继结点为双亲结点
        if (q->pnode->rchild==q) {
            return q->pnode;
        }
        //当前结点是父结点的左孩子,并且「没有」兄弟结点,那么后继结点为双亲结点
        if (q->pnode->lchild==q&&q->pnode->rtag==1) {
            return q->pnode;
        }
        //当前结点是父结点的左孩子,并且「有」兄弟结点,那么后继结点为父节点的右子树中后序遍历的第一个结点
        if (q->pnode->lchild==q&&q->pnode->rtag==0) {
            return FirstNode(q->pnode->rchild);
        }
    }
    return NULL;
}

//后序遍历
void PostOrder(ThreadNode *T){
    //利用三叉链表实现遍历
    for(ThreadNode *p=FirstNode(T);p!=NULL;p=NextNode(p)){
        printf("%d",p->data);
    }
}
上一篇:React项目中使用HighCharts


下一篇: