最近在学习数据结构(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;
}/*主函数*/