内存池技术(UVa 122 Tree on the level)
内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时,将其放入空闲列表中,使内存可以循环使用。空闲列表可以用不定长数组vector定义,内存池和空闲列表的类型由使用内存的数据决定,如果使用内存的数据是用户自定义的结构体类型,则使用此类型。由于大部分情况下是使用指针进行内存调用,所以空闲列表中存储的是相应的指针。
具体实例如下:
题目:
Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. This problem involves building and traversing binary trees. Given a sequence of binary trees, you are to write a program that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes. In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k + 1. For example, a level order traversal of the tree on the right is: 5, 4, 8, 11, 13, 4, 7, 2, 1. In this problem a binary tree is specified by a sequence of pairs ‘(n,s)’ where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of ‘L’s and ‘R’s where ‘L’ indicates a left branch and ‘R’ indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.
Input
The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs ‘(n,s)’ as described above separated by whitespace. The last entry in each tree is ‘()’. No whitespace appears between left and right parentheses. All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.
Output
For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ‘not complete’ should be printed.
Sample Input
(11,LL) (7,LLL) (8,R) (5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
Sample Output
5 4 8 11 13 4 7 2 1
not complete
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn = + ;
struct Node///定义结点
{
bool have_value;///判断是否有值
int v;
Node* left, *right;
Node():have_value(false),left(NULL),right(NULL) {}
};
Node* root;
queue<Node*> freenodes; /// 空闲列表
Node node[maxn]; /// 内存池
void init()
{
for(int i = ; i < maxn; i++)
freenodes.push(&node[i]);///将内存池里面的结点全部放入空闲列表中,由于是初次使用,所以所有结点都可以使用
}
Node* newnode()///建立新结点
{
Node* u = freenodes.front();///从空闲列表中拿出一个使用过的结点
u->left = u->right = NULL;
u->have_value = false; /// 重新初始化该结点
freenodes.pop();
return u;
}
void deletenode(Node* u)
{
freenodes.push(u);///将结点重新放入空闲列表,下次使用时会初始化并且再次被利用
}
bool failed;
void addnode(int v, char* s)
{
int n = strlen(s);
Node* u = root;
for(int i = ; i < n; i++)
if(s[i] == 'L')
{
if(u->left == NULL) u->left = newnode();///如果左子树是空的,建立左子树
u = u->left;
}
else if(s[i] == 'R')
{
if(u->right == NULL) u->right = newnode();///如果左子树是空的,建立左子树
u = u->right;
}
if(u->have_value) failed = true;
u->v = v;
u->have_value = true;
}
void remove_tree(Node* u)///清除多余的树
{
if(u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
deletenode(u);
}
char s[maxn];
bool read_input()
{
failed = false;
remove_tree(root);///清除之前的树
root = newnode();///初始化根结点
for(;;)
{
if(scanf("%s", s) != ) return false;
if(!strcmp(s, "()")) break;///strcmp如果2个字符串相同则返回0
int v;
sscanf(&s[], "%d", &v);///读入节点值
addnode(v, strchr(s, ',')+);///strchr返回s中','所在位置
}
return true;
}
bool bfs(vector<int>& ans)
{
queue<Node*> q;///建立一个不定长数组存储结点
ans.clear();///初始化答案数组
q.push(root);
while(!q.empty())
{
Node* u = q.front();///将第一个结点的数据传给u指针
q.pop();
if(!u->have_value) return false;///当树不为空,但是没有对应的值,说明树建立失败
ans.push_back(u->v);
if(u->left != NULL) q.push(u->left);
if(u->right != NULL) q.push(u->right);///递归查找子树
}
return true;
}
int main()
{
vector<int> ans;
init();///初始化
while(read_input())
{
if(!bfs(ans)) failed = ;///递归失败,不存在相应的树
if(failed) printf("not complete\n");
else
{
for(int i = ; i < ans.size(); i++)
{
if(i != ) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
}
return ;
}
内存池技术(UVa 122 Tree on the level)的更多相关文章
- Netty精粹之轻量级内存池技术实现原理与应用
摘要: 在Netty中,通常会有多个IO线程独立工作,基于NioEventLoop的实现,每个IO线程负责轮询单独的Selector实例来检索IO事件,当IO事件来临的时候,IO线程开始处理IO事件. ...
- Linux服务器内存池技术是如何实现的
Linux服务器内存池技术是如何实现的
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- 常见C++内存池技术
原文:http://www.cppblog.com/weiym/archive/2013/04/08/199238.html 总结下常见的C++内存池,以备以后查询.应该说没有一个内存池适合所有的情况 ...
- UVA 122 -- Trees on the level (二叉树 BFS)
Trees on the level UVA - 122 解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...
- boost::pool与内存池技术
建议看这个链接的内容:http://cpp.winxgui.com/cn:mempool-example-boost-pool Pool分配是一种分配内存方法,用于快速分配同样大小的内存块, ...
- uva 122 trees on the level——yhx
题目如下:Given a sequence of binary trees, you are to write a program that prints a level-order traversa ...
- UVa 122 Trees on the level(二叉树层序遍历)
Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...
- UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)
题意 :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
随机推荐
- Linux下使用date命令查看和修改时间
1.查看系统时区和时间,执行命令date -R “-0500”代表西五区(America/New_York),北京的时区为“+0800”(东八区). 2.查看硬件时间,执行命令hwclock --sh ...
- JQuery对象和DOM对象的区别与转换
刚开始学习JQuery,经常分不清楚哪些是JQuery对象,哪些是DOM对象,了解它们之间的关系是很有必要的. 1.DOM对象和JQuery对象的区别 1) DOM对象 DOM是Document O ...
- css3 min-content,max-content,fit-content, fill属性
css3里有四个属性,用来实现以内容为主的尺寸计算方式,intrinsic sizing min-content max-content fit-content fill 其中 fill 关键字,需要 ...
- 【例子】log4j.properties例子讲解
log4j.rootLogger=info, ServerDailyRollingFile, stdout log4j.appender.ServerDailyRollingFile=org.apac ...
- c语言亲缘线程通过管道通信一些疑问
亲缘线程在使用管道时,发现第一次使用管道进行进行通信完全正常(./a.out 1),但当重新运行并使用新管道文件时候出现数据无法读取的问题(./a.out 2)(./a.out 3),甚至出现子线程部 ...
- 一款c语言实现的赛车游戏
博主学习c语言已经有一段时间了,出于对自己学习检验的目的,自制了一款c语言赛车游戏. 由于本质是检验和尝试,所以并没有注重游戏的界面.下文是开发文档,在博主的github网页可以下载源码,注意本项目使 ...
- day26 第二阶段共享
第二阶段共享--网络编程 一.C/S架构 : 客户端(client)/服务端(sever)框架 B/S架构: 浏览器(brower)/服务端(sever)架构 软件CS架构: 服务端连接到浏览器,QQ ...
- PHP socket以及http、socket、tcp、udp
一.TCP/UDP/Socket TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为 ...
- Oracle与MySQL的SQL语句区别
2 表 2.1 创建表(同) create table tableName( columnName1 int, columnName2 int ) 2.2 删除表(异) MySQL: drop tab ...
- 四:(之一和之二) docker架构和底层技术分析(C/S架构)
1.架构和底层技术 Docker Host提供了RESTUL api,使docker client可以通过这些命令调用dockerd. Registry是一个公用的存储镜像的容器,类似于github. ...