Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7534288
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-03-26 21:09:51

  1. /*******************************************
  2. *文件名:
  3. * 功能:二叉树应用编程
  4. * 说明:输入字符序列,以#号表示空
  5.         用递归方法建立二叉表
  6.         用递归方法前序遍历二叉树
  7. * 时间:2011-3-26      --Lzy
  8. /********************************************/

  9. #include <stdio.h>
  10. #include <malloc.h>

  11. typedef struct Node            /*二叉链表存储结构*/
  12. {
  13.     char data;
  14.     struct Node *lchild, *rchild;
  15. }BiTree;

  16. BiTree * createBiTree(void)
  17. {                            /*创建二叉表--递归算法*/
  18.     char ch;
  19.     BiTree *T;
  20.     
  21.     ch = getchar();
  22.     if(ch == '#')
  23.         return NULL;        /*递归结束*/
  24.     else
  25.     {
  26.         T = (BiTree *)malloc(sizeof(BiTree));
  27.         T->data = ch;                    /*数据域赋值*/
  28.         T->lchild = createBiTree();        /*递归调用*/
  29.         T->rchild = createBiTree();
  30.         return T;    
  31.     }
  32. }

  33. void preOrder(BiTree *T)    /*前序遍历二叉树--递归算法*/
  34. {
  35.     if(T)
  36.     {
  37.         printf("%c",T->data);        /*访问根节点*/
  38.         preOrder(T->lchild);        /*前序遍历左子树*/
  39.         preOrder(T->rchild);        /*前序遍历右子树*/
  40.     }
  41. }

  42. int main(void)
  43. {
  44.     BiTree *root;
  45.     printf("\n输入结点的前序序列创建二叉树(ABC##FG###D#E##)#表示空:\n > ");
  46.     root = createBiTree();
  47.     printf("\n前序遍历二叉树--递归:\n");
  48.     preOrder(root);    
  49.     printf("\n");
  50. }
阅读(2048) | 评论(0) | 转发(2) |
0

上一篇:队列应用编程

下一篇:类与对象简单应用

给主人留下些什么吧!~~