zoukankan      html  css  js  c++  java
  • 线索二叉树的建立

    使用平台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 == '
    '|| ch == '	')
     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
    ");
     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
    ");
     81         (*Thrt)->lChild = *Thrt;
     82     }
     83     else
     84     {
     85         //printf("inorder2
    ");
     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 }
    代码如下:
  • 相关阅读:
    去掉链接A标签周围的虚线框(IE、Firefox)<转>
    解决ckeditor 错误 和一些WEB开发的错误
    asp.net程序编译调试时偶尔出现访问被拒绝的错误的解决方法
    手工创建全新的DataSet
    玩3c `
    C#写个很不错的资源管理器
    asp.net调用JavaScript自定义方法
    JavaScript压缩格式化和加密
    ASP.NET格式化时间
    Response.ContentType 详细列表
  • 原文地址:https://www.cnblogs.com/Shirlies/p/3276554.html
Copyright © 2011-2022 走看看