内存池技术就是创建一个内存池,内存池中保存着可以使用的内存,可以使用数组的形式实现,然后创建一个空闲列表,开始时将内存池中所有内存放入空闲列表中,表示空闲列表中所有内存都可以使用,当不需要某一内存时,将其放入空闲列表中,使内存可以循环使用。空闲列表可以用不定长数组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)的更多相关文章

  1. Netty精粹之轻量级内存池技术实现原理与应用

    摘要: 在Netty中,通常会有多个IO线程独立工作,基于NioEventLoop的实现,每个IO线程负责轮询单独的Selector实例来检索IO事件,当IO事件来临的时候,IO线程开始处理IO事件. ...

  2. Linux服务器内存池技术是如何实现的

    Linux服务器内存池技术是如何实现的

  3. UVA.122 Trees on the level(二叉树 BFS)

    UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...

  4. 常见C++内存池技术

    原文:http://www.cppblog.com/weiym/archive/2013/04/08/199238.html 总结下常见的C++内存池,以备以后查询.应该说没有一个内存池适合所有的情况 ...

  5. UVA 122 -- Trees on the level (二叉树 BFS)

     Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个rea ...

  6. boost::pool与内存池技术

      建议看这个链接的内容:http://cpp.winxgui.com/cn:mempool-example-boost-pool Pool分配是一种分配内存方法,用于快速分配同样大小的内存块,    ...

  7. 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 ...

  8. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  9. UVa 122 Trees on the level (动态建树 && 层序遍历二叉树)

    题意  :输入一棵二叉树,你的任务是按从上到下.从左到右的顺序输出各个结点的值.每个结 点都按照从根结点到它的移动序列给出(L表示左,R表示右).在输入中,每个结点的左 括号和右括号之间没有空格,相邻 ...

随机推荐

  1. static与全局变量区别

    作用域不同: 全局变量是不显式用static修饰的全局变量,但全局变量默认是动态的,作用域是整个工程,在一个文件内定义的全局变量,在另一个文件中,通过extern 全局变量名的声明,就可以使用全局变量 ...

  2. UVa 11825 - Hackers' Crackdown DP, 枚举子集substa = (substa - 1)&sta 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. Unity中物体碰撞后去掉相互之间的反弹力

    最近自制了一个的角色控制器(没有重力的角色)时发现,角色碰撞到墙壁之后会有一个小小的反弹力导致角色有一个微弱的反弹位移,这样给人一种不好的感觉.研究了一下,除了限制坐标轴( Rigidbody---C ...

  4. Java代理:静态代理、动态代理

    要理解动态代理,需要先理解反射(http://www.cnblogs.com/Donnnnnn/p/7729443.html) 通俗理解: 在很多底层框架中都会用得到,比如struts,Spring等 ...

  5. c# 十进制转二、八、十六进制

    一.十进制转二.八.十.十六进制字符串 Convert.ToString(int decNum,int toBase); decNum为十进制字符串, toBase可以为2.8.10.16 如果要转换 ...

  6. opencv3.0+vs2013安装记录

    为了能够更好的学习图像,我觉得opencv是一个必不可少的库,因此在以后的研究上使用opencv作为研究工具,与大家共同进步. 话归正题:先搭建opencv的环境. 1.下载安装包3.0 a,官网打开 ...

  7. leetcode python 004

    ##  已知l1,l2均为升序数组,##  在两数组l1,l2中寻找第n位数,##  两数组中位数中,前者大于后者,说明后者中位数以下的成员必定在真正中位数之下##  可以将其剔除,剔除a个元素后的两 ...

  8. centos7 安装jdk8 bash脚本 并配置环境变量

    #!/bin/bash #安装java脚本 if type -p java; then echo 'java已安装.' exit else echo '开始安装java...' wget --no-c ...

  9. 利用node,跑项目。

    (前提是已经安装了node) 一.简单介绍  Vue开发|文件目录结构部署 目录结构 ├── index.html 入口页面 ├── build 构建脚本目录 │ ├── build-server.j ...

  10. L267 How to save money

    When it comes to saving money, the struggle is all too real. It's like your bank account and your 20 ...