第11周 项目1(中序线索化二叉树的算法验证)

问题及描述:

[html]  view plain  co


[html]  view plain  copy
  1. #ifndef HEAD_H_INCLUDED  
  2. #define HEAD_H_INCLUDED  
  3. #define MaxSize 100  
  4. #include<stdio.h>  
  5. #include<malloc.h>  
  6. typedef char ElemType;  
  7. typedef struct node  
  8. {  
  9.     ElemType data;  
  10.     int ltag,rtag;  
  11.     struct node *lchild;  
  12.     struct node *rchild;  
  13. }TBTNode;  
  14. void CreateTBTNode(TBTNode * &b,char *str);//建立二叉树  
  15. void DispTBTNode(TBTNode *b);//输出二叉树  
  16. void Thread(TBTNode *&p);//线索化  
  17. TBTNode *CreaThread(TBTNode *b);//线索化二叉树  
  18. void ThInOrder(TBTNode *tb);//中序遍历  
  19. #endif // HEAD_H_INCLUDED  


[html]  view plain  copy
  1. #include "head.h"  
  2. int main()  
  3. {  
  4.     TBTNode *b,*tb;  
  5.     CreateTBTNode(b,"A(B(D(,G)),C(E,F))");  
  6.     printf(" 二叉树:");  
  7.     DispTBTNode(b);  
  8.     printf("\n");  
  9.     tb=CreaThread(b);  
  10.     printf(" 线索中序序列:");  
  11.     ThInOrder(tb);  
  12.     printf("\n");  
  13.     return 0;  
  14. }  


[html]  view plain  copy
  1. #include "head.h"  
  2. TBTNode *pre;  
  3. void CreateTBTNode(TBTNode * &b,char *str)  
  4. {  
  5.     TBTNode *St[MaxSize],*p;  
  6.     int top=-1,k,j=0;  
  7.     char ch;  
  8.     b=NULL;  
  9.     ch=str[j];  
  10.     while(ch!='\0')  
  11.     {  
  12.         switch(ch)  
  13.         {  
  14.         case '(':  
  15.             top++;  
  16.             St[top]=p;  
  17.             k=1;  
  18.             break;  
  19.         case ')':  
  20.             top--;  
  21.             break;  
  22.         case ',':  
  23.             k=2;  
  24.             break;  
  25.         default:  
  26.             p=(TBTNode *)malloc(sizeof(TBTNode));  
  27.             p->data=ch;  
  28.             p->lchild=p->rchild=NULL;  
  29.             if(b==NULL)  
  30.                 b=p;  
  31.             else  
  32.             {  
  33.                 switch(k)  
  34.                 {  
  35.                 case 1:  
  36.                     St[top]->lchild=p;  
  37.                     break;  
  38.                 case 2:  
  39.                     St[top]->rchild=p;  
  40.                     break;  
  41.                 }  
  42.             }  
  43.         }  
  44.         j++;  
  45.         ch=str[j];  
  46.     }  
  47. }  
  48.   
  49. void DispTBTNode(TBTNode *b)  
  50. {  
  51.     if(b!=NULL)  
  52.     {  
  53.         printf("%c",b->data);  
  54.         if(b->lchild!=NULL||b->rchild!=NULL)  
  55.         {  
  56.             printf("(");  
  57.             DispTBTNode(b->lchild);  
  58.             if(b->rchild!=NULL)  
  59.                 printf(",");  
  60.             DispTBTNode(b->rchild);  
  61.             printf(")");  
  62.         }  
  63.     }  
  64.   
  65. }void Thread(TBTNode *&p)  
  66. {  
  67.     if (p!=NULL)  
  68.     {  
  69.         Thread(p->lchild);          //左子树线索化  
  70.         if (p->lchild==NULL)        //前驱线索  
  71.         {  
  72.             p->lchild=pre;          //建立当前结点的前驱线索  
  73.             p->ltag=1;  
  74.         }  
  75.         else p->ltag=0;  
  76.         if (pre->rchild==NULL)      //后继线索  
  77.         {  
  78.             pre->rchild=p;          //建立前驱结点的后继线索  
  79.             pre->rtag=1;  
  80.         }  
  81.         else pre->rtag=0;  
  82.         pre=p;  
  83.         Thread(p->rchild);          //右子树线索化  
  84.     }  
  85. }  
  86. TBTNode *CreaThread(TBTNode *b)  
  87. {  
  88.     TBTNode *root;  
  89.     root=(TBTNode *)malloc(sizeof(TBTNode));  
  90.     root->ltag=0;  
  91.     root->rtag=1;  
  92.     root->rchild=b;  
  93.     if(b==NULL)  
  94.         root->lchild=root;  
  95.     else  
  96.     {  
  97.         root->lchild=b;  
  98.         pre=root;  
  99.         Thread(b);  
  100.         pre->rchild=root;  
  101.         pre->rtag=1;  
  102.         root->rchild=pre;  
  103.     }  
  104.     return root;  
  105. }  
  106. void ThInOrder(TBTNode *tb)  
  107. {  
  108.     TBTNode *p=tb->lchild;  
  109.     while (p!=tb)  
  110.     {  
  111.         while (p->ltag==0) p=p->lchild;  
  112.         printf("%c ",p->data);  
  113.         while (p->rtag==1 && p->rchild!=tb)  
  114.         {  
  115.             p=p->rchild;  
  116.             printf("%c ",p->data);  
  117.         }  
  118.         p=p->rchild;  
  119.     }  
  120. }  


运行结果:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值