内存池技术(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 ;
}
上一篇:不看好运维竖井产品模式,优云打造融合化运维PaaS平台


下一篇:UVa 1394: And Then There Was One