Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3725
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-12 10:13
文章分类
文章存档

2015年(1)

我的朋友
最近访客

分类: C/C++

2015-11-22 18:09:51

原文地址:层次遍历二叉树 作者:nba76ers


层次遍历二叉树,关键要用到队列,父结点一出,就要判断子结点是否为空,非空则马上进入队列,类似广度优先
queue 队列, empty(), front(), pop(), push() 
 

点击(此处)折叠或打开

  1. #include
  2. #include
  3. #include
  4. using namespace std;
  5. typedef int datatype;
  6. typedef struct node
  7. {
  8.     datatype data;
  9.     struct node *lchild,*rchild;
  10. }bintnode;
  11. typedef bintnode *bintree;

  12.     void createbintree(bintree *t)
  13.     {
  14.     //输入二叉树的先序遍历序列,创建二叉链表
  15.         int ch;
  16.         //ch=getchar();
  17.         scanf("%d",&ch);
  18.         if(ch==-1)
  19.             *t=NULL;//如果读入空格字符,创建空树,T是指向指针的指针,*t就相当于一个bintree指针,专门指向bintnode;
  20.         else
  21.         {
  22.             (*t)=(bintnode*)malloc(sizeof(bintnode));
  23.             (*t)->data=ch;
  24.             createbintree(&(*t)->lchild);//根据先序遍历,继续创建左子树,让客户端继续输入
  25.             createbintree(&(*t)->rchild);//创建完左子树,继续创建右子树
  26.         } //递归调用,自动返回
  27.     }
  28.     void preorder(bintree t)
  29.     {
  30.         if(t)
  31.         {
  32.             printf("%d ",t->data);//先访问根结点,再遍历左子树,跟着右子树
  33.             preorder(t->lchild);
  34.             preorder(t->rchild);
  35.         }
  36.         
  37.     }
  38.     void inorder(bintree t)
  39.     {
  40.         if(t)
  41.         {
  42.             inorder(t->lchild);
  43.             printf("%d ",t->data);//
  44.             inorder(t->rchild);
  45.         }
  46.     }
  47.     void postorder(bintree t)
  48.     {
  49.         if(t)
  50.         {
  51.             postorder(t->lchild);
  52.             postorder(t->rchild);
  53.             printf("%d ",t->data);//
  54.         }
  55.     }

  56.     void levertravel(bintree t)
  57.     {
  58.         queueq;
  59.         if(t!=NULL)
  60.         q.push(t);
  61.         bintree b;
  62.         while(!q.empty())
  63.         {
  64.             b=q.front();
  65.             printf("%d ",b->data);
  66.             q.pop();
  67.             if(b->lchild)
  68.                 q.push(b->lchild);
  69.             if(b->rchild)
  70.                 q.push(b->rchild);

  71.         }
  72.     }

  73. int main()
  74. {

  75. /*
  76. 这里的输入要严格按照正确的顺序才能结束.这里要用到二叉树的一个性质,
  77. 就是说对于有n个节点的二叉树,就有n+1个空域,在这里即为如果你输入了n个
  78. 元素,那么一定要有n+1个#才会结束迭代过程.
  79. */
  80.     bintree t=NULL;
  81.     createbintree(&t);//这样才能改变T,指向指针的指针
  82.     printf("前序遍历: ");
  83.     preorder(t);
  84.     printf("\n中序遍历: ");
  85.     inorder(t);
  86.     printf("\n后序遍历: ");
  87.     postorder(t);
  88.     printf("\n层次遍历: ");
  89.     levertravel(t);
  90.     printf("\n");
  91.     getchar();
  92.     return 0;
  93. }

  94. /*
  95. 1 2 4 8 -1 -1 9 -1 -1 5 10 -1 -1 11 -1 -1 3 6 -1 -1 7 -1 -1
  96. 前序遍历: 1 2 4 8 9 5 10 11 3 6 7
  97. 中序遍历: 8 4 9 2 10 5 11 1 6 3 7
  98. 后序遍历: 8 9 4 10 11 5 2 6 7 3 1
  99. 层次遍历: 1 2 3 4 5 6 7 8 9 10 11
  100. Press any key to continue
  101. */

阅读(387) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

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