zoukankan      html  css  js  c++  java
  • 线索二叉树实现(中序)

    /* 目的在于让没有孩子的指针标示前驱和后继一次方便遍历 */
    /* 首先建立树的头结点,然后若发现没有左孩子,就指向pre标示前驱
    ,由于中序遍历的特点,回溯的那个结点便是下一个要访问的结点,所
    以此时判断pre有无右孩子,若无,则指向当前回溯结点标示下一个要访
    问的结点 */
    /* 最后完成头结点的右结点工作,完成线索二叉树 */

       1:  #include <stdio.h>
       2:  #include <stdlib.h>
       3:   
       4:  //link(0)指向孩子节点
       5:  //thread(1)指向双亲节点
       6:   
       7:  typedef enum{link,thread} pointertag;
       8:  typedef char    elemtype;
       9:   
      10:  typedef struct bitthrnode
      11:  {
      12:      elemtype    data;
      13:      pointertag  ltag;
      14:      pointertag  rtag;
      15:      struct bitthrnode  *lchild;
      16:      struct bitthrnode  *rchild;
      17:  }bitthrnode,*bitthrtree;
      18:   
      19:  bitthrtree  pre;
      20:   
      21:  void treecreat(bitthrtree *T)
      22:  {
      23:      elemtype    ch;
      24:   
      25:      scanf("%c",&ch);
      26:      if( ' ' == ch )
      27:      {
      28:          (*T)=NULL;
      29:          exit(0);
      30:      }
      31:      else
      32:      {
      33:          (*T) = (bitthrnode *)malloc(sizeof(bitthrnode));
      34:          (*T)->data = ch;
      35:          (*T)->ltag = link;
      36:          (*T)->rtag = link;
      37:   
      38:          treecreat(&(*T)->lchild);
      39:          treecreat(&(*T)->rchild);
      40:      }
      41:  }
      42:   
      43:  void visit(bitthrtree T)
      44:  {
      45:   
      46:  }
      47:   
      48:  int inorder_threading_init(bitthrtree *thrt, bitthrtree bt)
      49:  {
      50:      *thrt = (bitthrtree) malloc (sizeof(bitthrnode));
      51:      if (!*thrt)
      52:          exit(1);
      53:                                                     /* 将头结点线索化 */
      54:      (*thrt)->ltag = link;
      55:      (*thrt)->rtag = thread;
      56:      (*thrt)->rchild = (*thrt);
      57:   
      58:      if (!bt)
      59:      {                                               /* 若二叉树为空,则将lchild指向自己 */
      60:          (*thrt)->lchild = (*thrt);
      61:      }
      62:      else
      63:      {
      64:          (*thrt)->lchild = bt;                   /* 头结点左指针指向根结点 */
      65:          pre = (*thrt);
      66:          in_threading(bt);                       /* 中序遍历进行中序线索化 */
      67:   
      68:          //收尾工作,形成双向链表
      69:          pre->rchild = *thrt;
      70:          pre->rtag = thread;
      71:          (*thrt)->rchild = pre;
      72:      }
      73:   
      74:  //中序遍历线索化
      75:  void inthreading(bitthrtree T)
      76:  {
      77:      if( T )
      78:      {
      79:          //递归调用左结点中序线索化
      80:          inthreading(T->lchild);
      81:   
      82:          if( !T->lchild )//如果左孩子为空
      83:          {
      84:              T->ltag = thread;
      85:              T->lchild = pre;
      86:          }
      87:          //处理结点
      88:          if( !pre->rchild )
      89:          {
      90:              pre->rtag = thread;
      91:              pre->rchild = T;
      92:          }
      93:          pre = T;
      94:   
      95:          //递归调用左结点中序线索化
      96:          inthreading(T->rchild);
      97:      }
      98:  }
      99:   
     100:  //传入头结点,来一次循环遍历
     101:  void inorder_threading(bitthrtree T)
     102:  {
     103:      bitthrtree  p;
     104:      p=T->lchild;
     105:      while( p != T )
     106:      {
     107:          while( p->ltag == link )
     108:          {
     109:              p = p->lchild;
     110:          }
     111:          
     112:          while( p->rchild != T && p->rtag == thread)
     113:          {
     114:              p = p->rchild;
     115:              visit(p);
     116:          }
     117:          p = p->rchild;
     118:      }
     119:  }
     120:   
     121:   
     122:  int main()
     123:  {
     124:      //脑补部分
     125:      return 0;
     126:  }
  • 相关阅读:
    Springboot 2响应式编程 WebFlux 初体验
    MyBatis集成到Spring
    Java 常用的转换、排序
    Springboot 2使用外部Tomcat源码分析
    Springboot 2启动内置Tomcat源码分析
    Springboot 2启动源码流程
    Springboot 2使用SpringApplication
    IDEA效率插件JRebel的使用
    Spring 源码总结
    Spring事件监听器源码
  • 原文地址:https://www.cnblogs.com/hyd-desert-camel/p/3377872.html
Copyright © 2011-2022 走看看