二叉树广义优先遍历为层遍历(从左到右)。思想就是采用队列,步骤如下:
(1) 树的根结点入队
(2)判断队列是否为空,不为空,则出队,并输出出队树结点的值
(3)出队树结点的左子树进入队列
(4)出队树结点的右子树进入队列
(5)循环回到(2)
创建如下二叉树:
代码如下:
// BreadthFS.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
typedef int type;
using namespace std;
//树结点
typedef struct node
{
type data;
struct node *lchild;
struct node *rchild;
}node,*bTree;
//链队结点
typedef struct queue
{
bTree tree;
struct queue *next;
}queue,*pQueue;
//队列表定义
typedef struct LQueue
{
pQueue front;//队首指针
pQueue rear; //队尾指针
}LQueue,*LinkQueue;
//二叉树的创建
void CreateTree(bTree &T)
{
type ch;
cin>>ch;
if(0=