二叉树线索化及遍历(纯C语言)

  最近在学习数据结构(c语言),严老师内容很详细,但书中的语言为类C语言。即使弄清楚了算法的理论但当我在实际编程中却发现需要根据c语言的语法修改一些。主要感觉就是书中引用的c++中的引用“&”。在C语言中我一般用函数返回值或者二重指针来进行替代。二叉树线索化及遍历中也运用了这些。分享出来希望能有所借鉴不足之处也希望指出。
typedef enum pointertag{link,thread};
typedef char elemtype;
typedef struct binode
{
	elemtype data;
	binode* lchild;
	binode* rchild;
	pointertag ltag,rtag;
}binode,*bithrtree;/*线索二叉树的存储*/

先序创建二叉树

bithrtree precreattree(bithrtree t)
{
 elemtype ch;
 scanf("%c",&ch);
 if(ch==' ')t=NULL;
 else
 {
  if(!(t=(bithrtree)malloc(sizeof(binode))))exit(0);
  t->data=ch;
  t->ltag=link;
  t->rtag=link;/*标志默认为link*/
  t->lchild=precreattree(t->lchild);
  t->rchild=precreattree(t->rchild);
 }
 return t;
}/*先序创建线索二叉树*/


接下来是二叉树的线索化,其实这里的pre也可以通过全局变量的方式,但返回多个值时感觉用指针更直观更c一点。

bithrtree inorderthreading(bithrtree t)
{
	void inthreading(bithrtree p,binode**pre);
	bithrtree thr,temp;
	binode**pre;/*建立二重指针从而能在inthreading()中返回最后一个结点的指针*/
	if(!(temp=(bithrtree)malloc(sizeof(binode))))exit(0);
	pre=&temp;/*给二重指针分配一个独立的空间*/
	if(!(thr=(bithrtree)malloc(sizeof(binode))))exit(0);
	thr->ltag=link;thr->rtag=thread;
	thr->rchild=thr;
	if(!t)thr->lchild=thr;
	else
	{
		thr->lchild=t;*pre=thr;
		inthreading(t,pre);
		(*pre)->rchild=thr;(*pre)->rtag=thread;
		thr->rchild=(*pre);
	}
	return thr;
}
void inthreading(bithrtree p,binode**pre)
{
	if(p)
	{
		inthreading(p->lchild,pre);
		if(!p->lchild){p->ltag=thread;p->lchild=*pre;}
		if(!(*pre)->rchild){(*pre)->rtag=thread;(*pre)->rchild=p;}
		(*pre)=p;
		inthreading(p->rchild,pre);
	}
}

最后就是线索二叉树的遍历了

void inordertraverse(bithrtree t)
{
	binode*p;
	p=t->lchild;
	while(p!=t)
	{
		while(!p->ltag)
		{
			p=p->lchild;
		}
		printf("%c",p->data);
		while(p->rtag&&p->rchild!=t)
		{
			p=p->rchild;
			printf("%c",p->data);
		}
		p=p->rchild;
	}
}

完整代码

#include<stdio.h>
#include<stdlib.h>
typedef enum pointertag{link,thread};
typedef char elemtype;
typedef struct binode
{
	elemtype data;
	binode* lchild;
	binode* rchild;
	pointertag ltag,rtag;
}binode,*bithrtree;
bithrtree precreattree(bithrtree t)
{
	elemtype ch;
	scanf("%c",&ch);
	if(ch==' ')t=NULL;
	else
	{
		if(!(t=(bithrtree)malloc(sizeof(binode))))exit(0);
		t->data=ch;
		t->ltag=link;
		t->rtag=link;/*标志默认为link*/
		t->lchild=precreattree(t->lchild);
		t->rchild=precreattree(t->rchild);
	}
	return t;
}/*先序创建线索二叉树*/
void inordertraverse(bithrtree t)
{
	binode*p;
	p=t->lchild;
	while(p!=t)
	{
		while(!p->ltag)
		{
			p=p->lchild;
		}
		printf("%c",p->data);
		while(p->rtag&&p->rchild!=t)
		{
			p=p->rchild;
			printf("%c",p->data);
		}
		p=p->rchild;
	}
}
bithrtree inorderthreading(bithrtree t)
{
	void inthreading(bithrtree p,binode**pre);
	bithrtree thr,temp;
	binode**pre;/*建立二重指针从而能在inthreading()中返回最后一个结点的指针*/
	if(!(temp=(bithrtree)malloc(sizeof(binode))))exit(0);
	pre=&temp;/*给二重指针分配一个独立的空间*/
	if(!(thr=(bithrtree)malloc(sizeof(binode))))exit(0);
	thr->ltag=link;thr->rtag=thread;
	thr->rchild=thr;
	if(!t)thr->lchild=thr;
	else
	{
		thr->lchild=t;*pre=thr;
		inthreading(t,pre);
		(*pre)->rchild=thr;(*pre)->rtag=thread;
		thr->rchild=(*pre);
	}
	return thr;
}
void inthreading(bithrtree p,binode**pre)
{
	if(p)
	{
		inthreading(p->lchild,pre);
		if(!p->lchild){p->ltag=thread;p->lchild=*pre;}
		if(!(*pre)->rchild){(*pre)->rtag=thread;(*pre)->rchild=p;}
		(*pre)=p;
		inthreading(p->rchild,pre);
	}
}
int main()
{
	bithrtree t,thr;
	printf("请建立先序二叉树:");
        t=precreattree(t);
	printf("对二叉树进行线索化。");
	printf("对线索二叉树进行遍历:");
	thr=inorderthreading(t);
	inordertraverse(thr);
	return 0;
}/*主函数*/

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值