树的层序遍历:

层次遍历:即每一层从左向右输出

元素需要储存有先进先出的特性,所以选用队列存储。

队列的定义:

  1. #define MAX 1000
  2. typedef struct seqqueue{
  3. bintree data[MAX];
  4. int front;
  5. int rear;
  6. }seqqueue;
  7. void enter(seqqueue *q,bintree t){
  8. if(q->rear == MAX){
  9. printf("the queue is full!\n");
  10. }else{
  11. q->data[q->rear] = t;
  12. q->rear++;
  13. }
  14. }
  15. bintree del(seqqueue *q){
  16. if(q->front == q->rear){
  17. return NULL;
  18. }else{
  19. q->front++;
  20. return q->data[q->front-1];
  21. }
  22. }

遍历实现 :

    1. void level_tree(bintree t){
    2. seqqueue q;
    3. bintree temp;
    4. q.front = q.rear = 0;
    5. if(!t){
    6. printf("the tree is empty\n");
    7. return ;
    8. }
    9. enter(&q,t);
    10. while(q.front != q.rear){
    11. t=del(&q);
    12. printf("%c ",t->data);
    13. if(t->lchild){
    14. enter(&q,t->lchild);
    15. }
    16. if(t->rchild){
    17. enter(&q,t->rchild);
    18. }
    19. }
    20. }
时间: 2024-08-04 01:20:00

树的层序遍历:的相关文章

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

637. Average of Levels in Binary Tree(一棵树每层节点的平均数)(二叉树的层序遍历)

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / 9 20 / 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on le

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

天梯 - 是否完全二叉搜索树(判断序列插完是否是完全二叉树,并求出层序遍历)

将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数N:第二行给出N个互不相同的正整数,其间以空格分隔. 输出格式: 将输入的N个正整数顺序插入一个初始为空的二叉搜索树.在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格.第二行输出YES,如果该树是完全二叉树:否则输出NO. 输入样例1: 9 38 45 42 24 5

Java实现二叉搜索树的添加,前序、后序、中序及层序遍历,求树的节点数,求树的最大值、最小值,查找等操作

什么也不说了,直接上代码. 首先是节点类,大家都懂得 /** * 二叉树的节点类 * * @author HeYufan * * @param <T> */ class Node<T extends Comparable<? super T>> { /** * 节点储存的值 */ private T data; /** * 左子节点 */ private Node<T> leftNode; /** * 右子节点 */ private Node<T>

Java数据结构系列之——树(6):二叉树的层序遍历

package tree.binarytree; import java.util.LinkedList; /** * 层序遍历二叉树 * * @author wl * */ public class PrintFromTopToBotton { public static void printfromtoptobotton(BiTreeNode root) { if (root == null) { return; } LinkedList<BiTreeNode> queue = new L

二叉树层序遍历的实现

我们可以很容易的使用队列来实现二叉树的层序遍历,代码如下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX 10 4 5 6 //二叉树存储结构定义 7 typedef char Item; 8 typedef struct node *link; 9 struct node {Item item; link l, r;}; 10 11 int Create(link *tp); 12 void show(link

编程之美问题之二叉树层序遍历多种解法

二叉树的层序遍历(要求区分层,例如每层遍历完输出换行) 单单层序遍历非常简单,一个队列就搞定了,但是区分层则要麻烦些.总的思路无非就是在每次print的时候,要能通过某个东西 区分出当前节点是否是一层最后一个节点,或者下一层的最后一个节点,感觉有点类似于机器学习中找个区分度明显的特征: 1.自己的解法,在单队列基础上,输入队列的数据添加一个标志 ,LevelHeaded,同时,在后面插入两个孩子的时候,判断是否这次输出的是队头,如果是的话,先插 个队头标志,再插入孩子,而且插入一次之后,后面不能

C语言非递归实现二叉树的先序、中序、后序、层序遍历

C语言非递归实现二叉树的先序.中序.后序.层序遍历代码如下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <stack> 4 #include <queue> 5 using namespace std; 6 7 //*****二叉树的二叉链表存储表示*****// 8 typedef struct BiNode 9 { 10 char data; 11 struct BiNode *lchil