#include <iostream>
#define _OK 1
#define _ERROR 0
using namespace std;
// Define node element type in binary tree.
typedef char Element;
// Binary tree.
typedef struct BTNode
{
Element data;
BTNode *lChild,*rChild; // Define left,right subtree.
} BTNode, *BTree;
// Define node element type in queue. (The node element type in queue is the pointer to binary tree node)
typedef BTNode *QElementType;
typedef int status;
// --------------------------------------------------------------------
// We need use queue to perform level traverse. So, define queue first.
typedef struct QNode
{
QElementType data;
QNode *next;
} QNode, *QueuePtr;
// Definition of queue.
typedef struct
{
QueuePtr front;
QueuePtr rear;
} LinkQueue;
status InitQueue(LinkQueue &Q)
{
Q.front = NULL;
Q.rear = NULL;
return _OK;
}
bool IsEmpty(LinkQueue Q)
{
return Q.front == NULL;
}
status EnQueue(LinkQueue &Q, QElementType e)
{
// Construct queue node.
QNode *ptrNode = (QNode*) malloc(sizeof(QNode));
if(!ptrNode)
{
return _ERROR;
}
ptrNode->data=e;
ptrNode->next=NULL;
if(IsEmpty(Q))
{
Q.front=Q.rear=ptrNode;
return _OK;
}
Q.rear->next=ptrNode;
Q.rear = ptrNode;
return _OK;
}
status DeQueue(LinkQueue &Q, QElementType &e)
{
if(IsEmpty(Q))
{
return _ERROR;
}
QNode *tempPtr = Q.front;
e = tempPtr->data;
Q.front = tempPtr->next;
free(tempPtr);
return _OK;
}
// ------------------------------------------
int CreateBTree(BTree &T)
{
char ch;
cout << "Please input a character:" << endl;
cin >> ch;
if(ch=='#')
{
T = NULL;
}
else
{
// Allocate memory for new node.
if(!(T = (BTNode*)malloc(sizeof(BTNode))))
{
return 0; // Allocation fails.
}
T->data = ch;
// Create left subtree.
CreateBTree(T->lChild);
// Create right subtree.
CreateBTree(T->rChild);
}
return 1;
}
void VisitBTNode(BTNode *BT)
{
cout << BT->data << endl;
}
void VisitQNode(QNode *Q)
{
VisitBTNode(Q->data);
}
void LevelTraverse(BTree T)
{
QElementType e;
LinkQueue Q;
InitQueue(Q);
EnQueue(Q,T);
while (!IsEmpty(Q))
{
VisitQNode(Q.front);
if(Q.front->data->lChild!=NULL)
{
EnQueue(Q,Q.front->data->lChild);
}
if(Q.front->data->rChild!=NULL)
{
EnQueue(Q,Q.front->data->rChild);
}
DeQueue(Q,e);
}
}
// ------------------------------------------
void main()
{
BTree T;
CreateBTree(T);
LevelTraverse(T);
}
// Output:
/*
Please input a character:
6
Please input a character:
g
Please input a character:
#
Please input a character:
7
Please input a character:
f
Please input a character:
g
Please input a character:
8
Please input a character:
9
Please input a character:
#
Please input a character:
$
Please input a character:
#
Please input a character:
#
Please input a character:
#
Please input a character:
#
Please input a character:
c
Please input a character:
#
Please input a character:
#
Please input a character:
#
Please input a character:
#
6
g
7
f
g
c
8
9
$
*/
分享到:
相关推荐
二叉树 非递归实现 数据结构 c++ 广度遍历,非递归实现广度遍历生成二叉树。 数据结构与算法上机作业。
5.1 数据对象和数据结构 5.2 线性表数据结构 5.2.1 抽象数据类型linearList 5.2.2 抽象类linearList 5.3 数组描述 5.3.1 描述 5.3.2 变长一维数组 5.3.3 类arrayList 5.3.4 C++迭代器 5.3.5 arrayList的一个迭代器 ...
2.1 C++的数据类型 2.2 常量 2.2.1 什么是常量 2.2.2 数值常量 2.2.3 字符常量 2.2.4 符号常量 2.3 变量 2.3.1 什么是变量 2.3.2 变量名规则 2.3.3 定义变量 2.3.4 为变量赋初值 2.3.5 常变量 2.4 C++的运算符 ...
<br> 实验六 二叉树(二) 实验目的: 通过实验掌握下列知识: 1、继续熟悉二叉树的存储结构和遍历算法; 2、熟悉二叉搜索树的应用,并做一个小型的课程设计; 内容及步骤: 1、 在前一个实验...
第一次实验: 题目1 单链表相关算法的实验验证。 [实验目的] 验证单链表及其上的基本操作。 [实验内容及要求] ...4)图的广度优先遍历算法。 第四次实验 折半插入排序,堆排序,快速排序 请阅读说明文档
C++实现的各种经典数据结构和算法,例如链表,队列,二叉树遍历,深度优先搜索,广度优先搜索,图,分治法,动态规划法等
2.1 C++的数据类型 2.2 常量 2.2.1 什么是常量 2.2.2 数值常量 2.2.3 字符常量 2.2.4 符号常量 2.3 变量 2.3.1 什么是变量 2.3.2 变量名规则 2.3.3 定义变量 2.3.4 为变量赋初值 2.3.5 常变量 2.4 C++的运算符 ...
这个是我亲手所做的数据结构课程设计,完成了: 实验一 单链表的定义和应用 实验要求: 1.用单链表存储结构定义线性表 2.实现单链表基本操作(5个基本操作:构造,销毁,插入,删除, 取指定数据元素) 3.用单链表...
此项目只是为了在毕业之前复习一下曾经学过的数据结构/算法,初衷是打算仅仅使用Python实现,但写了简单的排序/查找算法之后,觉得对于C/C++我还是应该学习一下,于是就附带了一些之前在BNU上的练习代码。...
这里面有一些常见问题: 大数相乘、多项式的计算、迷宫、最短路径、顺序表的创建、插入、删除和查找、八皇后问题、fei递归马、建立二叉排序树、最小...、广度优先遍历图、先序次序输入二叉树中结点的值中序遍历.......
数据结构算法演示(Windows版) 使 用 手 册 一、 功能简介 本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法...
数据结构与算法_C语言 01.swap.mp4 02.BubbleSort.mp4 03.SelecttionSort.mp4 04.顺序查找.mp4 05.C_DS_折半查找.mp4 06.递归.mp4 07递归算法_折半查找.mp4 08.Permutations.mp4 09.插入排序.mp4 10.快速...
◆ 重点是核心的数据结构,而不是非必要的C++语言语法 ◆ 说明了类和ADT在问题解决过程中的作用 ◆ 诠释了ADT的主要应用,如查找航班图、事件驱动的模拟和八皇后问题 ◆ 大部分章节中的例子都使用了标准模板库...
要求建立图的存储结构(邻接表或邻接矩阵),输入任意的一个图,显示图的广度优先搜索遍历路径。 查找 设计一个读入一串整数构成一颗二叉排序树的程序,从二叉排序树中删除一个结点,使该二叉树仍保持二叉排序树的...
深度优先遍历、广度优先遍历; 动态规划; 贪心算法; 回溯。 剑指 offer # 题目 难度 题解 面试题03 数组中重复的数字 easy 面试题04 二维数组中的查找 easy 面试题68-I 二叉搜索树的最近公共祖先 easy 面试题68-II...
2. 无向图G=(V,E),其中:V={ a,b,c,d,e,f} , E={(a,b),(a,e),(a,c),(b,e),(c,f),(f,d),(e,d)},对该图进行广度优先遍历,得到的顶点序列正确的是 A) a,b,e,c,d,f B) a,c,f,e,b,d C) a,e,b,c,f,d D) a,e,d,f,c,b ...
4.概要设计:给出每道题采用的数据结构,算法设计思想,算法的时间复杂度; 5.详细设计:给出每道题的源程序,并在必要的代码处给出注释; 6.功能测试:给出每道题的测试数据和结果; 7.完成情况:每道题完成部分和...