线索二叉树的建立

使用平台Ubuntu+Code::Blocks(GCC)

线索二叉树的意思是:利用二叉树上节点的空指针指向其前驱或者后继。数据结构课本上说:在某程序中所用二叉树需经常遍历或查找结点在遍历所得线性序列中的前驱和后继,则应采用线索链表作为存储结构。

程序中有一个头结点thrdBase,其为二叉树外的结点,结点没有数据信息,其lChild指向二叉树的根结点,其rChild指向中序遍历时访问的最后一个结点。并且让中序序列的第一个结点的lChild和最后一个结点的rChild指向这个头结点。这样做好处在于:相当与建立了一个双向线索链表,既可以从第一个结点起顺序往后进行遍历,也可从最后一个结点顺着前驱进行遍历。

 

代码为按先序序列建立的二叉树,然后进行中序建立线索thread。

(书中有部分代码,书为严蔚敏和吴伟民编著的c语言版数据结构)

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #define OK 1
  4 #define ERROR 0
  5 #define LINK 0
  6 #define THREAD 1
  7 
  8 //the type of data
  9 typedef char DataType;
 10 typedef int Status;
 11 
 12 //keep the information of node
 13 typedef struct BiThrNode
 14 {
 15     DataType data;
 16     struct BiThrNode *lChild,*rChild;
 17     int lTag,rTag;
 18 }BiThrNode,*BiThrTree;
 19 BiThrTree pre = NULL;
 20 
 21 //preorder the tree
 22 Status createBiTree(BiThrTree *T)
 23 {
 24     char ch;
 25     scanf("%c",&ch);
 26     if(ch == '\n'|| ch == '\t')
 27     {
 28         return OK;
 29     }
 30 
 31     if(ch == ' ') *T = NULL;
 32     else
 33     {
 34         *T = (BiThrNode *)malloc(sizeof(BiThrNode));
 35         (*T)->data = ch;
 36         //at start,make the tag linking
 37         (*T)->rTag = LINK;
 38         (*T)->lTag = LINK;
 39         //printf("%c",(*T)->data);
 40         createBiTree(&(*T)->lChild);
 41         createBiTree(&(*T)->rChild);
 42     }
 43     return OK;
 44 }
 45 
 46 //thread the node
 47 void InThreading(BiThrTree t)
 48 {
 49     if(t)
 50     {
 51         InThreading(t->lChild);
 52         printf("%c",t->data);
 53         //the node is thread node when its child is null
 54         if(!t->lChild)
 55         {
 56             t->lTag = THREAD;
 57             t->lChild = pre;
 58         }
 59         if(!t->rChild)
 60         {
 61             pre->rTag = THREAD;
 62             pre->rChild = t;
 63         }
 64         pre = t;
 65         InThreading(t->rChild);
 66     }
 67 }
 68 
 69 Status InOrderThreading(BiThrTree *Thrt,BiThrTree T)
 70 {
 71     //printf("inorder\n");
 72     *Thrt = (BiThrTree)malloc(sizeof(BiThrNode));
 73 
 74     (*Thrt)->lTag = LINK;
 75     (*Thrt)->rTag = THREAD;
 76     //firstly ,the right child points to itself,when finding the last point,it points to the last point
 77     (*Thrt)->rChild = *Thrt;
 78     if(!T)
 79     {
 80         //printf("inorder1\n");
 81         (*Thrt)->lChild = *Thrt;
 82     }
 83     else
 84     {
 85         //printf("inorder2\n");
 86         (*Thrt)->lChild = T;
 87         pre = *Thrt;
 88 
 89         InThreading(T);
 90 
 91         //last point is threaded
 92         pre->rChild = *Thrt;
 93         pre->rTag = THREAD;
 94         (*Thrt)->rChild = pre;
 95     }
 96 
 97     return OK;
 98 }
 99 
100 int main()
101 {
102     BiThrTree base = NULL,thrdBase = NULL;
103     //create the tree
104     createBiTree(&base);
105     InOrderThreading(&thrdBase,base);
106 
107     return 0;
108 }
代码如下:

 

转载于:https://www.cnblogs.com/Shirlies/p/3276554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值