Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5380
  • 博文数量: 10
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-25 20:52
文章分类
文章存档

2015年(10)

我的朋友
最近访客

分类: C/C++

2015-04-17 19:02:56

二叉线索树:
结构:Ltag,Rtag,LChild,RChild
if   Ltag=1:该节点没有左孩子,链接的是前趋
if   Ltag=0:该节点有左孩子,链接的是左孩子
if   Rtag=1:该节点没有右孩子,链接的是后继
if   Rtag=0:该节点有右孩子,链接的是右孩子

点击(此处)折叠或打开

  1. typedef struct Node
  2. {
  3. char data;
  4. int Ltag,Rtag;
  5. struct Node *LChild,*RChild;
  6. }BiTNode,*BiTree;
线索二叉树的构造函数:

点击(此处)折叠或打开

  1. void CreateBiTree(BiTree *root)
  2. {
  3. char ch;
  4. ch=getchar();
  5. if(ch=='.')
  6. *root=NULL;
  7. else
  8. {
  9. *root=(BiTree)malloc(sizeof(BiTNode));
  10. (*root)->data=ch;
  11. (*root)->Ltag=0;
  12. (*root)->Rtag=0;
  13. CreateBiTree(&(*root)->LChild);
  14. CreateBiTree(&(*root)->RChild);
  15. }
  16. }
  17. BiTNode *pre;
  18. BiTNode *next;
  19. void InThread(BiTree root)
  20. {
  21. if(root!=NULL)
  22. {
  23. InThread(root->LChild)
  24. if(root->LChild==NULL)
  25. {
  26. root->Ltag=1;
  27. root->LChild=pre;
  28. }
  29. else if(pre!=NULL&&pre->RChild==NULL)
  30. {
  31. pre->RChild=1;
  32. pre->RChild=root;
  33. }
  34. pre=root;
  35. if(pre->RChild==NULL)
  36. pre->Rtag=1;
  37. InThread(root->RChild);
  38. }
  39. }
这个就是线索二叉树的创建,它把一颗二叉树炼成了一个以NULL开头,以NULL结尾的链表。

下面是寻找前趋,寻找后继的算法:

点击(此处)折叠或打开

  1. BiTNode *Inpre(BiTNode *p)
  2. {
  3. BiTNode *w;
  4. if(p->Ltag==1)
  5. pre=p->LChild;
  6. else
  7. {
  8. for(w=p->LChild;w->Rtag==0;w=w->RChild);
  9. pre=w;
  10. return pre;
  11. }
  12. }

  13. //中序列后继函数
  14. BiTNode *InNext(BiTNode *p)
  15. {
  16. BiTNode *q;
  17. if(p->RChild==1)
  18. next=p->RChild;
  19. else
  20. {
  21. for(q=p->RChild;q->Ltag==0;q=q->LChild);
  22. next=q;
  23. }
  24. return next;
  25. }

下面是关于线索二叉树的一个完整的例子:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. typedef struct Node
  4. {
  5.     char data;
  6.     struct Node *LChild;
  7.     int Ltag;
  8.     struct Node *RChild;
  9.     int Rtag;
  10. }BiTNode,*BiTree;

  11. //建立中序列线索树
  12. void CreateTree(BiTree *root)
  13. {
  14.     char ch;
  15.     ch=getchar();
  16.     if(ch=='.')
  17.         *root=NULL;
  18.     else
  19.     {
  20.         *root=(BiTree)malloc(sizeof(BiTNode));
  21.         (*root)->data=ch;
  22.         (*root)->Ltag=0;
  23.         (*root)->Rtag=0;
  24.         CreateTree(&((*root)->LChild));
  25.         CreateTree(&((*root)->RChild));
  26.     }
  27. }

  28. BiTNode *pre;
  29. BiTNode *next;
  30. void Inthread(BiTree root)
  31. {
  32.  if(root!=NULL)
  33.  {
  34.     Inthread(root->LChild);
  35.     if(root->LChild==NULL)
  36.     {
  37.         root->Ltag=1;
  38.         root->LChild=pre;

  39.     }
  40.     if(pre!=NULL&&pre->RChild==NULL)
  41.     {
  42.         
  43.         pre->RChild=root;
  44.         pre->Rtag=1;
  45.     }
  46.     pre=root;
  47.     if(pre->RChild==NULL)
  48.         pre->Rtag=1;
  49.     Inthread(root->RChild);
  50. }
  51. }

  52. BiTNode *InPre(BiTNode *p)
  53. {
  54.     BiTNode *w;
  55.     if(p->Ltag==1)
  56.         pre=p->LChild;
  57.     else
  58.     {
  59.         for(w=p->LChild;w->Rtag==0;w=w->RChild);
  60.             pre=w;
  61.     }
  62.     return pre;
  63. }

  64. //寻找后继
  65. BiTNode *InNext(BiTNode *p)
  66. {
  67.     BiTNode *q;
  68.     if(p->Rtag==1)
  69.         next=p->RChild;
  70.     else
  71.     {
  72.         for(q=p->RChild;q->Ltag==0;q=q->LChild);
  73.         next=q;
  74.     }
  75.     return next;
  76. }
  77. void Find(BiTree root)
  78. {
  79.     BiTNode *p;
  80.     p=root;
  81.     while(p->LChild!=NULL)
  82.         p=p->LChild;
  83.     printf("%c的Ltag=%d,Rtag=%d\n",p->data,p->Ltag,p->Rtag);
  84.     p=p->RChild;
  85.     while(p)
  86.     {
  87.         printf("%c的Ltag=%d,Rtag=%d\n",p->data,p->Ltag,p->Rtag);
  88.         p=p->RChild;
  89.     }
  90. }
  91. void SortVisit(BiTree root)
  92. {    
  93.     BiTNode *p,*n;
  94.     BiTNode *q,*m;
  95.     p=root;
  96.     while(p->LChild!=NULL)
  97.             p=p->LChild;
  98.     printf("请找出节点%c的前趋与后继\n",p->data);
  99.     q=(BiTNode *)malloc(sizeof(BiTNode));
  100.     m=(BiTNode *)malloc(sizeof(BiTNode));
  101.     q=InPre(p);
  102.     m=InNext(p);
  103.     if(q==NULL)
  104.         printf("前趋是NULL,后继是%c\n",m->data);
  105.     else if(m==NULL)
  106.         printf("前趋是%c,后继是NULL\n",q->data);
  107.     else
  108.         printf("前趋是%c\t后继是 %c\n",q->data,m->data);
  109.     p=p->RChild;
  110.     while(p)
  111.     {
  112.         printf("请找出节点%c的前趋与后继\n",p->data);
  113.         q=(BiTNode *)malloc(sizeof(BiTNode));
  114.         m=(BiTNode *)malloc(sizeof(BiTNode));
  115.         q=InPre(p);
  116.         m=InNext(p);
  117.         if(q==NULL)
  118.             printf("前趋是NULL,后继是%c\n",m->data);
  119.         else if(m==NULL)
  120.             printf("前趋是%c,后继是NULL\n",q->data);
  121.         else
  122.             printf("前趋是%c\t后继是 %c\n",q->data,m->data);
  123.         p=p->RChild;
  124.     }
  125. }
  126. int main()
  127. {    
  128.     BiTree root;
  129.     BiTree t;
  130.     printf("请创建一个线索二叉树\n");
  131.     CreateTree(&root);
  132.     Inthread(root);
  133.     Find(root);
  134.     SortVisit(root);
  135. }

这个例子可以打出所有节点的前趋和后继以及他们是否有左右孩子。

阅读(203) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~