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

    package com.zhi.test; public class PathTest { public static void main(String[] args) { System.out.pr ...

  2. oracle 如何查看当前用户的表空间名称

    如何查询当前用户的表空间名称?因为oracle建立索引,需要知道当前用户的表空间,查找了一下资料 --查询语法-- select default_tablespace from dba_users w ...

  3. isScroll 插件在iPhone 5s 和以上版本

    才加入这个移动项目组三天,解决一个同事(请假),解决一个切换头部tab 选型时,下拉数据,再次切换到另外一个选项时,出现滚动条距离顶部有些距离,当频繁操作会出现距离顶部距离加大问题(第二天衍生出其他b ...

  4. Linux系统从零到高手的进阶心得

    初次了解到Linux系统还是在我初中的时候,那时候正是在一个中二年龄,喜欢看小说,对于小说中出现的明显的非现实场景感到十分钦佩.羡慕,并常常幻想自己也有小说主人公那样的本领.那正是在这样一个充满幻想的 ...

  5. Unity中DOTween插件的DOTweenPath轨迹移动

    先来看一下DOTweenPath组件的截图 1.Scene View Commands (1)SHIFT+CTRL:add a waypoint        加一个轨迹点 (2)SHIFT+ALT: ...

  6. 用c++写一个数据库

    [cpp] view plain copy 第一步:构建一个头文件(**.h) [cpp] view plain copy #include<iostream> #include<i ...

  7. HP Instant Information

    HP Instant Information before HP-UX 11i v3 <管理系统和工作组:HP-UX系统管理员指南> After HP-UX 11i v3 <HP-U ...

  8. leetcode python 005

    ##  给定字符串,寻找最长回文子串##  单回文,双回文 def findh(s):    ## 单回文    ld,l=[],len(s)    if len(s)<3:        re ...

  9. css文件 如何使背景图片大小适应div的大小

    对背景图片设置属性:background-size:cover;可以实现背景图片适应div的大小. background-size有3个属性: auto:当使用该属性的时候,背景图片将保持100% 的 ...

  10. 谷歌浏览器慎用有道词典插件(<audio></audio>) (转载)

    谷歌浏览器慎用有道词典插件(<audio></audio>)  原文 :http://blog.csdn.net/u010556394/article/details/7112 ...