内存池技术(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表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...
随机推荐
- IntelliJ Idea 快捷键列表
最重要:1Ctrl+Alt+Shift+T:查找类2重构3提取父类 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift ...
- js string对象方法
substr(start,length) substring(start,end) 返回子串,原字符串不改变.
- Ubuntu 12.04 Desktop下vncserver配置:Unity以及Xfce4桌面环境
将gnome改成xfce xfce-session 即可 2013-01-30 14:45:34| 分类: Ubuntu | 标签:ubuntu12.04 unity vncserver s ...
- 逆袭之旅DAY17.东软实训.Oracle.存储过程
2018-07-13 09:08:36
- php 处理ftp常用操作与方法
原文地址:https://www.cnblogs.com/longfeiPHP/p/5420632.html $ftp_conn = ftp_connect("192.168.1.230&q ...
- UVALive 3401 - Colored Cubes 旋转 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- java⑾
1.数组: 01.一组 相同数据类型的集合! 02.数组在内存中会 开辟一串连续的空间来保存数据! ***存储30名学生的姓名! 01.姓名 应该用什么数据类型保存??? String02.难道需要创 ...
- Mysql 数据库意向锁意义
锁:对 “某种范围” 的数据上 “某种锁”1.“某种范围”:行.表 2.“某种锁”2.1 共享锁Shared Locks(S锁)1.兼容性:加了S锁的记录,允许其他事务再加S锁,不允许其他事务再加X锁 ...
- bootstrap table 分页显示问题
1.bootstrap-table客户端分页 客户端分页的数据源可以是后台服务器端传递过来(一次性获取,即获取所有你需要的数据),点击页码不再请求后台,利用页面缓存分页;cache : true, / ...
- codeforce150A(简单的求质数问题)
A. Win or Freeze time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...