【C/C++】【用队列结构实现二叉树的层次遍历】
来源:互联网 发布:音量放大器软件 编辑:程序博客网 时间:2024/05/01 00:11
#include "OJ.h"
#include <string.h>
#include <stdio.h>
/*
Description
要求按照指定要求打印二叉树
Prototype
int BiNodePrint(BiNode &head, char *result)
Input Param
head 需要打印的二叉树头结点
Output Param
result 打印出的字符串
Return Value
0 成功
-1 失败或其他异常
*/
int BiNodePrint(BiNode &head, char *result)
{
/*在这里实现功能*/
struct strqueue s;
BiNode bn;
int idx = 0, have_err = 0;
if (!result)
return -1;
initqueue(&s);
enqueue( &s, &head );
memset(result, 0x00, MAXSIZE);
while( s.num>0 )
{
if ( !dequeue(&s, &bn ) )
{
have_err = 1;;
break;
}
memcpy( result+idx, &(bn.data), sizeof(bn.data) );
idx += sizeof(bn.data);
if (bn.left)
{
enqueue( &s, bn.left );
}
if (bn.right)
{
enqueue( &s, bn.right );
}
}
if (have_err)
return -1;
result[idx] = 0;
//printf("result=[%s]\n", result);
return 0;
}
bool initqueue(struct strqueue *s) /* 初始化队列,返回0表示失败,返回1表示成功 */
{
bool result = 0;
int i = 0;
/* 代码在这里实现 */
result = 0;
if ( !s )
return result;
memset( s->queue, 0x00, sizeof(BiNode)*MAXSIZE );
s->head = -1;
s->tail = -1;
s->num = 0;
result = 1;
return result;
}
bool enqueue(struct strqueue *s, BiNode *x) /* 进队列,返回0表示失败,返回1表示成功 */
{
bool result = 0;
/* 代码在这里实现 */
result = 0;
if ( !s )
return result;
if( s->num >= MAXSIZE )
return result;
memcpy( &(s->queue[s->tail = (s->tail+1) % MAXSIZE]), x, sizeof(BiNode) );
s->num++;
if(-1 == s->head)
s->head = 0;
result = 1;
return result;
}
bool dequeue(struct strqueue *s, BiNode *x) /* 出队列,返回0表示失败,返回1表示成功 */
{
bool result = 0;
/* 代码在这里实现 */
result = 0;
if (!s)
return result;
if( s->num < 1 )
return result;
memcpy( x, &(s->queue[(s->head) % MAXSIZE]), sizeof(BiNode) );
//s->queue[(s->head) % MAXSIZE] = 0;
s->head = (s->head + 1) % MAXSIZE;
//printf( "top =[%d], s->head=[%d]\n", *x, s->head );
if ( 0 == (s->num = s->num - 1) )
{
s->head = -1;
s->tail = -1;
}
result = 1;
return result;
}
#ifndef __OJ_H__
#define __OJ_H__
#define MAXSIZE 100
typedef struct tagBiNode
{
char data;
struct tagBiNode *left;
struct tagBiNode *right;
} BiNode;
struct strqueue
{
BiNode queue[MAXSIZE];
int head; /* 队头 */
int tail; /* 队尾 */
int num; /* 队元素个数 */
};
bool initqueue(struct strqueue *s); /* 初始化队列,返回0表示失败,返回1表示成功 */
bool enqueue(struct strqueue *s, BiNode *x); /* 进队列,返回0表示失败,返回1表示成功 */
bool dequeue(struct strqueue *s, BiNode *x); /* 出队列,返回0表示失败,返回1表示成功 */
int BiNodePrint(BiNode &head, char *result);
#endif
- 【C/C++】【用队列结构实现二叉树的层次遍历】
- 使用队列实现二叉树按层次遍历(C++)
- c语言实现二叉树层次遍历(借助队列实现)
- 二叉树按层次遍历--队列实现
- 二叉树按层次遍历--队列实现
- 二叉树按层次遍历--队列实现
- 二叉树层次遍历(借助队列实现)
- 队列实现按层次遍历二叉树
- 二叉树利用队列实现层次遍历
- 利用队列实现二叉树的层次遍历
- 用队列实现树的层次遍历
- 二叉树层次遍历(C语言实现)
- 二叉树的层次遍历 队列
- 数据结构 c语言实现 二叉树的层次遍历(linux下实现)
- <数据结构>二叉树的递归、非递归以及层次遍历算法C语言实现
- 数据结构-----层次遍历二叉树算法(利用队列实现)
- [二叉树专题]:广度优先:按层次遍历二叉树的非递归实现||使用队列实现层次遍历二叉树
- 二叉树的层次遍历----java实现
- 第五周算法作业, 反向图
- 第一个boost程序---timer的使用
- POJ 2586:Y2K Accounting Bug:贪心法
- myeclipse不能编译的解决方法
- 中文字符及其中文标点符号正则表达式匹配
- 【C/C++】【用队列结构实现二叉树的层次遍历】
- 干货分享 9款精挑细选的HTML5应用
- 试试发表博文
- ad test
- Eclipse does not recognize added Android library projects 添加库项目失败的解决办法
- [c#]获取数据库中所有表的表名
- 扣压机,扣管机,压管机,锁管机价格,厂家直销
- SqlServer_在SQL Server 2005修改存储过程、执行;
- C++的 warning C4250