鱼C论坛

 找回密码
 立即注册
查看: 5278|回复: 14

用递归中序遍历二叉树该如何实现

[复制链接]
发表于 2014-6-26 20:46:04 | 显示全部楼层 |阅读模式
10鱼币
  1. // 中序遍历二叉树,非递归
  2. void InOrderTraverse( BiThrTree T )
  3. {
  4.         BiThrTree p;
  5.         p = T->lchild;

  6.         while( p != T )
  7.         {
  8.                 while( p->ltag == Link )
  9.                 {
  10.                         p = p->lchild;
  11.                 }
  12.                 visit(p->data);

  13.                 while( p->rtag == Thread && p->rchild != T )
  14.                 {
  15.                         p = p->rchild;
  16.                         visit(p->data);
  17.                 }
  18.                
  19.                 p = p->rchild;
  20.         }
  21. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-26 22:24:09 | 显示全部楼层
void inorder (BiTree &T)
{
        if (T)
        {
                preorder(T->lchild);
                printf("-%c-", T->data);                preorder(T->rchild);
        }

        return;
}

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-27 06:58:15 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-27 07:14:18 From FishC Mobile | 显示全部楼层
先访问左子树,再访问结点,最后右子树。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-27 08:13:44 | 显示全部楼层
santaclaus 发表于 2014-6-27 07:14
先访问左子树,再访问结点,最后右子树。

能给个代码吗思路是这样 代码写不来
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-28 06:35:30 | 显示全部楼层
void InOrderTraverse( BiThrTree T)
{
        BiThrTree p;
        p = T->lchild;

        while(p != T )
        {
                while( p->ltag == Link )
                {
                        p = p->lchild;
                }

                visit(p->data);

                while(p->rtag == Thread && p->rchild != T )
                {
                        p = p->rchild;
                        visit(p->data);
                }

                p = p->rchild;//收尾
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-28 06:37:55 | 显示全部楼层
//中序遍历
void InThreading(BiThrTree T)
{
        if( T )
        {
                InThreading( T->lchild ); //递归左孩子线索化

                if( !T->lchild ) // 如果该节点没有做孩子,设置ltag为Therad,并把lchild指向前驱
                {
                        (*T).ltag = Thread;
                        T->lchild = pre;
                }

               
                if( !pre->rchild )
                {
                        (*T).rtag = Thread;
                        pre->rchild = T;
                }

                pre = T;

                InThreading( T->rchild );//递归右孩子线索化
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-28 08:35:41 | 显示全部楼层
Mikel 发表于 2014-6-28 06:37
//中序遍历
void InThreading(BiThrTree T)
{

这不是视频上的吗
  1. // 中序遍历二叉树,非递归
  2. void InOrderTraverse( BiThrTree T )
  3. {
  4.         BiThrTree p;
  5.         p = T->lchild;

  6.         while( p != T )
  7.         {
  8.                 while( p->ltag == Link )
  9.                 {
  10.                         p = p->lchild;
  11.                 }
  12.                 visit(p->data);

  13.                 while( p->rtag == Thread && p->rchild != T )
  14.                 {
  15.                         p = p->rchild;
  16.                         visit(p->data);
  17.                 }
  18.                
  19.                 p = p->rchild;
  20.         }
  21. }
复制代码
用递归如何实现
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-6-28 12:18:08 | 显示全部楼层
luckin 发表于 2014-6-28 08:35
这不是视频上的吗用递归如何实现

没仔细看,以为你要递归的呢。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-6-28 20:12:05 | 显示全部楼层
Mikel 发表于 2014-6-28 12:18
没仔细看,以为你要递归的呢。

恩恩
还是谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-4 17:00:24 | 显示全部楼层
  1. // 为了不让你误解我给出我自己版本的书阶段的定义
  2. typedef struct Bitnode
  3. {
  4.      char data;      //树结点存储的值暂且设定为字符型
  5.      struct Bitnode *lchild;
  6.      struct Bitnode *rchild;
  7. }Bitnode;



  8. void qwe(Bitnode *T)   //这里qwe是函数名字你爱什么名就什么名,Bitnode是数结点结构体
  9. {
  10.      if(NULL != T)    //这里也可以if(T <span style="line-height: 1.5;">!= NULL</span><span style="line-height: 1.5;">),是一样的作用,只是个小技巧,有时候自己把x == 2写成x=2很麻烦,但是把2 ==x写成2=x  编译器会报错。</span>
复制代码
我给个遍历的模板给你

void travese(Bitnode *T)
{
      if(NULL  !=  T)
      {
             //1,如果这个位置写对结点的访问  就是前序遍历
            
            travese(T->lchild);
           
            //2, 如果这个位置对结点访问      就是中序遍历

            travese(T->rchild);


             //3, 如果这个位置对结点访问      就是后序遍历
       }
}




小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-4 17:04:55 | 显示全部楼层
风骚居士 发表于 2014-8-4 17:00
我给个遍历的模板给你

void travese(Bitnode *T)

void qwe(Bitnode *T)
{
      if( NULL  !=  T )
      {           
            qwe(T->lchild);
            visit(T);
            qwe(T->rchild);
       }
}
void visit(Bitnode* T)
{
            printf("%c",T->data);
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-4 17:05:58 | 显示全部楼层
第一份回复出了点问题  , 我重新打了代码上去, 你凑合着看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-8-4 17:48:33 | 显示全部楼层
好牛逼的样子{:1_1:}{:1_1:}{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-10-13 12:10:00 | 显示全部楼层

谁能告诉我小甲鱼的数据第43讲为什么不能下载
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 06:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表