二叉链表

struct bnode                   // 结点类型

{ struct bnode * lchild       // 左孩子指针

    ElemType data               // 抽象数据元素类型

    struct bnode * rchild       // 右孩子指针

  }*root

 

生成二叉链表 C 算法1

#define LENG sizeof(struct bnode) /* 结点所占空间的大小*/

creat_tree(struct bnode ** root)    / *root 指向指针 的指针*/

{ char ch

  scanf("%c",&ch)                    /* 输入结点, 字符型*/

  if (ch == F ) (* root)=NULL        /* 生成空二叉树*/

  else                                /* 生成非空二叉树*/

      { (* root)=(struct bnode *)malloc(LENG)

       (* root)->data=ch                   /* 生成根结点 */

       creat_tree(& (* root)->lchild)     /* 递归生成左子树*/

     creat_tree(& (* root)->rchild)     /* 递归生成右子树*/

      }     

  return

  }

 

  main( )  /* 主函数*/

 

{ struct bnode *root

    creat_tree(& root)

  }

----------------------------------------------------------------

 

建立二叉链表算法2, 避免使用 指针的指针

#define LENG sizeof(struct bnode) // 结点所占空间的大小

struct bnode *CreatBiTree( )

{ char ch

  struct bnode *root         /* 二叉链表根结点指针*/

  scanf("%c",&ch)            /* 输入结点, 字符型*/

  if (ch==' ')root=NULL      /* 生成空二叉树*/

  else

  { root=(struct bnode *)malloc(LENG ) /* 生成根结点*/

    root->data=ch

    root->lchild=CreatBiTree( ) /* 递归构造左子树 */

    root->rchild=CreatBiTree( ) /* 递归构造右子树 */

   }

  return root

  }

  main( )  /* 主函数*/

 

  { struct bnode *root

   root=CreatBiTree( )

  }

 

-------------------------------------------------------------------------------

 

前序遍历递归算法

preorder(struct bnode *root)  

   //root 为根指针

 

{   if (root)         // 为非空二叉树

      { printf("%c,",root->data)   // 访问根结点

      preorder(root->lchild)     // 递归遍历左子树

      preorder(root->rchild)     // 递归遍历右子树

    }

  }

 

--------------------------------------------------------------------

 

inorder(struct bnode *root)

  //root 为根指针

 

{ if (root)     // 为非空二叉树  

    { inorder(root->lchild)      // 递归遍历左子树

    printf("%c",root->data)   // 访问根结点

    inorder(root->rchild)      // 递归遍历右子树

   }

  }

 

 

 

 

 

------------------------------------------------- 

中序遍历 非递归算法

inorder2(struct bnode *t)        //t 为根指针

{ struct bnode *st[maxleng+1]   // 定义指针( 地址)st

  int top=0                   // 置空栈

  do           

  { while(t)                      // 为非空二叉树

    { if (top==maxleng) exit("overflow") // 栈已满, 退出

      st[++top]=t               // 根地址进栈

      t=t->lchild               //t 移向左子树

     }                      

    if (top)                      // 为非空栈  

    { t=st[top--]               // 弹出根地址

      printf("%c",t->data)      // 访问根结点

      t=t->rchild               // 遍历右子树

     }

   }

   while(top||t)

  }  // 父结点未访问 右子树未遍历

 

 

 

 

求二叉树中结点的个数

creat_tree(struct bnode **root)    // 生成二叉树

{ ...........

  }

preorder(struct bnode *root,int *n )// 求二叉树中结点的个数

{ if (root)

  { (*n)++                        // 计数

    preorder(root->lchild,n )      // 递归遍历左子树

    preorder(root->rchild,n )      // 递归遍历右子树

   }

  }

main()

{ struct bnode *root

  int n=0                       //n 为结点数

  creat_tree(&root )             // 假定成功生成二叉树

  preorder(root,&n )             // 调用preorder(root,&n)

  printf("%d",n)                // 输出结点数n

 

  }

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值