数据结构后序线索二叉树C语言

#include <iostream>
using namespace std;
#define Status int 
#define OK 1
#define ERROR 0
#define Elemtype char

typedef enum{
	Link,Thread
}PointerTag;

typedef struct BiThrNode{
	Elemtype data;
	struct BiThrNode *lchild, *rchild; 
	struct BiThrNode *parent;
	PointerTag LTag , RTag;   
}BiThrNode, *BiThrTree;
BiThrTree pre;
 
Status visit(Elemtype e){
	cout << e << endl;
	return OK;
}

char Vexch[26]={'A','B','D','H','$','$','I','$','$','E','J','$','$','$','C','F','$','$','G','$','$'};  
int i=0;  

Status CreatBiThrTree(BiThrTree &T, BiThrTree &p)  
{  
    if(Vexch[i++]=='$') T=NULL;  
    else {  
        T= new BiThrNode;
        if(!T)  return 0;  
        T->data=Vexch[i-1]; 
		T->parent = p;
		T->LTag=Link; 
        CreatBiThrTree(T->lchild, T);
		T->RTag=Link; 
        CreatBiThrTree(T->rchild, T);
    }  
    return 1;  
}  
 
void PostThreading(BiThrTree p) {
	if(p) {
		PostThreading(p->lchild);  
		PostThreading(p->rchild); 
		if(!p->lchild) {             
			p->LTag = Thread;    
			p->lchild = pre;       
		}
		if(pre && !pre->rchild) {
			pre->RTag = Thread;   
			pre->rchild = p ;   
		}
		pre = p;
	}
}

Status PostOrderTraverse_Thr(BiThrTree T) {
	BiThrTree p;
	p = T;  
	pre=NULL;
	while(p != NULL){     
		while(p->LTag == Link)  
			p = p->lchild;
		 
		while(p->RTag == Thread) { 
			visit(p->data);
			pre = p;
			p = p->rchild ;            
		}
		if(p == T){ 
			visit(p->data); 
			break;
		}
		while(p && p->rchild == pre) { 
			visit(p->data);
			pre = p;
			p = p->parent;
		}
		if(p && p->RTag == Link)
			p = p->rchild;
	}
	return OK;
}

int main() {
	BiThrTree PostT;
	pre = NULL;
	CreatBiThrTree(PostT,pre);
	PostThreading(PostT);
	PostOrderTraverse_Thr(PostT);
	printf("\n");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值