- 论坛徽章:
- 0
|
帮你格式化一下,感觉哥们是不是还是个学生
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct binode //线索定义....
- {
- char data ;
- struct binode *lchild; //左孩子。...
- struct binode *rchild; //右孩子...
- int ltag;
- int rtag;
- } bithrnode ,*bithrtree ;
- int inittree(bithrtree *t) //新键二叉树.....
- {
- char ch;
- *t=(bithrtree )malloc(sizeof(bithrnode )); //开空间.....
- ch=getchar(); //输入结点......
- (*t)->data=ch;
- (*t)->lchild=NULL;
- (*t)->rchild=NULL; //对左右子树..初始化...
- if(ch!=' ')
- {
- inittree(&(*t)->lchild); //递归遍历二叉树....
- inittree(&(*t)->rchild);
- }
- return 1;
- }
- int intreading(bithrtree *p,bithrnode *pre) //线索化.......
- {
- if(*p)
- {
- intreading(&(*p)->lchild,pre); //中序线索化.....
- if((*p)->lchild==NULL) //如果左孩子为空。.就连接前驱结点....
- {
- (*p)->ltag=1;
- (*p)->lchild=pre;
- }
- if(( pre)->rchild==NULL) //如果右孩子为空。.就连接后续结点....
- {
- ( pre)->rtag=1;
- pre->rchild=*p;
- }
- pre=*p; //per指针。.跟上 p指针.....
- intreading(&(*p)->rchild,pre);
- }
- return 1;
- }
- int InOrderThreading(bithrtree *p,bithrtree *thrt) //线索化.......
- {
- bithrtree pre;
- *thrt=(bithrtree )malloc(sizeof(bithrnode)); //开空间..
- if(*thrt==NULL)
- return 0;
- (*thrt)->ltag=0; //右标志为0
- (*thrt)->rtag=1; //右标志为1
- (*thrt)->rchild=*thrt ; //右指针回指,,,,
- if(p==NULL)
- (*thrt)->lchild=*thrt; //如果二叉树为空。.就左指针也回指..
- else
- {
- (*thrt)->lchild=*p; //如果不为空。. 就把。.根结点给头结点的左孩子。.
- pre=*thrt; //pre指针。.指向头结点...
- }
- intreading(p,pre); //调用过去..进行线索化...
- (*thrt)->rtag=1; //最后一个结点的线索化....
- pre->rchild=*thrt;
- (*thrt)->rchild=pre;
- return 1;
- }
- InOrderTraverse(bithrtree *t,bithrtree *thrt) //二叉数输出...
- {
- *t=(*thrt)->lchild ; //头结点的左孩子给*t,,,,
- while((*t)!=(*thrt)) //用来判断结束...
- {
- while((*t)->ltag==0) //如果没左孩子。..
- {
- *t=(*t)->lchild ; //指向前驱。...
- printf("%d %c %d\n",(*t)->ltag,(*t)->data,(*t)->rtag);
- }
- while((*t)->rtag==1&&(*t)->rchild!=*thrt) //如果没有右孩子且最后一个结点不指向头结点.
- {
- *t=(*t)->rchild ; //指向后续...
- printf("%d %c %d\n",(*t)->ltag,(*t)->data,(*t)->rtag);
- }
- *t=(*t)->rchild; //指针继续找后续。...不断变化,,,
- }
- return 1;
- }
- int main()
- {
- bithrtree a,thrt; //thrt为结点
- inittree(&a);
- InOrderThreading(&a,&thrt);
- InOrderTraverse(&a,&thrt);
- return 1;
- }
- ////////////////////////////////////////////////////////////////////////////
复制代码
[ 本帖最后由 hardboy_du 于 2007-5-28 09:02 编辑 ] |
|