线索二叉树

头文件(线索二叉树结构体以及相关函数声明部分)

#ifndef TREAD
#define THREAD

typedef enum{link =0,thread =1} pointtag;

typedef char elemtype;

typedef struct bithrnode
{
	bithrnode *leftchild;
	bithrnode *rightchild;
	pointtag ltag,rtag;
	elemtype data;
}bithrnode,*bithetree;

bithrnode *buynode();

void freenode(bithrnode *ptr);

bithrnode *createthr(char *&str);

bithrnode *createthrtree(char *str);

void inorder(bithrnode *ptr);

void make(bithrnode *ptr,bithrnode *&p);

void makethreadtree(bithrnode *ptr);

bithrnode * first(bithrnode *p);

bithrnode * next(bithrnode *p);

void thrinorder(bithrnode *ptr);

#endif

相关函数:

bithrnode *buynode()
{
	bithrnode *s=(bithrnode *)malloc(sizeof(bithrnode));
	return s;
}

void freenode(bithrnode *ptr)
{
	free(ptr);
}
//ABC##DE##F##G#H##
bithrnode *createthr(char *&str)
{
	bithrnode *s=NULL;
	if (*str!='#')
	{
		s=buynode();
		s->data=*str;
		s->ltag=s->rtag=link;
		s->leftchild=createthr(++str);
		s->rightchild=createthr(++str);
	}
	return s;
}
bithrnode *createthrtree(char *str)
{
	if (str==NULL)
	{
		return NULL;
	}
	else
	{
		return createthr(str);
	}
}

void inorder(bithrnode *ptr)
{
	if (ptr!=NULL)
	{
		inorder(ptr->leftchild);
		printf("%c ",ptr->data);
		inorder(ptr->rightchild);
	}
}
void makethreadtree(bithrnode *ptr)
{
	bithrnode *p=NULL;
	make(ptr,p);
	p->rightchild=NULL;
	p->rtag=thread;
}
void make(bithrnode *ptr,bithrnode *&p)
{
	if (ptr!=NULL)
	{
		makethreadtree(ptr->leftchild);
		if (ptr->leftchild==NULL)
		{
			ptr->leftchild=p;
			ptr->ltag=thread;
		}
		if (p!=NULL && p->rightchild==NULL)
		{
			p->rightchild=ptr;
			p->rtag=thread;
		}
		p=ptr;

		makethreadtree(ptr->rightchild);
	}
}

bithrnode * first(bithrnode *p) 
{
	while(p!=NULL && p->ltag!=thread)
	{
		p=p->leftchild;
	}
	return p;
}
bithrnode * next(bithrnode *p)
{
	if (p==NULL)
	{
		return NULL;
	}
	if (p->rtag == thread)
	{
		return p->rightchild;
	}
	else 
	{
		return first(p->rightchild);
	}
}
void thrinorder(bithrnode *ptr)
{
	bithrnode *p=NULL;
	for (p=first(ptr);p!=NULL;p=next(p))
	{
		printf("%c",p->data);
	}
	printf("\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值