122 - Trees on the level(模拟内存池解法)
来源:互联网 发布:安道尔微信暗语知乎 编辑:程序博客网 时间:2024/05/16 10:41
PS:题目和动态分配内存解法,可以查看:122 - Trees on the level(动态分配空间解法)
PS:模拟内存池的原理简单的说就是先初始化一个空闲队列,当需要资源时,直接从队首获取,当资源释放不用时,又将该资源加入空闲队列队尾。
#include <iostream>#include <queue>#include <string>#include <sstream>#include <vector>using namespace std;const int maxNum = 256 + 5;// 二叉树节点类型struct Node { // 是否被赋值 bool isAssign; // 值 int value; // 左子节点 Node *left; // 右子节点 Node *right;};// 根节点Node *root;bool failed;// 内存池// 空闲节点队列queue<Node *> freeNodes;// 内存池中的结点都是从该数组中获取的Node node[maxNum];// 初始化内存池void init() { for(int i = 0; i < maxNum; i++) { freeNodes.push(&node[i]); }}// 从内存池中取一个节点Node* newNode() { Node *p = freeNodes.front(); // 重新初始化该节点 p->left = p->right = NULL; p->isAssign = false; freeNodes.pop(); return p;}// 将使用完毕的节点重新加入这个内存池void deleteNode(Node *p) { freeNodes.push(p);}// 释放树结点void removeTree(Node *root) { if(root == NULL) { return; } // 释放左子树 if(root->left != NULL) { removeTree(root->left); } // 释放右子树 if(root->right != NULL) { removeTree(root->right); } // 因为使用内存池技术 // 所以会将结点回收再次放回内存池 deleteNode(root);}// 插入节点void insertNode(int value , string seq) { Node *p = root; for(int i = 0; i < seq.length(); i++) { // 插入左子树 if('L' == seq[i]) { if(p->left == NULL) { p->left = newNode(); } p = p->left; } else if('R' == seq[i]) { // 插入右子树 if(p->right == NULL) { p->right = newNode(); } p = p->right; } } // 插入重复节点 if(p->isAssign) { failed = true; } p->value = value; p->isAssign = true;}// 宽度优先遍历// 返回false表示有节点未赋值bool bfs(vector<int> &ans) { Node *p; // 入栈根节点 queue<Node*> q; q.push(root); // 清空上一次序列 ans.clear(); while(!q.empty()) { p = q.front(); q.pop(); if(!p->isAssign) { return false; } if(p->left != NULL) { // 入栈左子节点 q.push(p->left); } if(p->right != NULL) { // 入栈右子节点 q.push(p->right); } // 放入序列 ans.push_back(p->value); } return true;}int main() { string s; // 初始化内存池 init(); // 层次遍历序列容器 vector<int> ans; while(true) { // 清除上一棵树的结点 removeTree(root); // 初始化 failed = false; root = newNode(); while(true) { cin >> s; // 文件末尾,退出 if(cin.eof()) { return 0; } // ()情况,停止添加结点 if(')' == s[1]) { break; } // 找到','的位置 int pos = s.find(','); // 截取value int val; istringstream iss; iss.str(s.substr(1, pos - 1)); iss >> val; // 插入节点 insertNode(val , s.substr(pos + 1, s.length() - pos - 2)); } // 获取层次遍历序列 if(!bfs(ans)) { failed = true; } if(failed) { cout << "not complete" << endl; } else { for(int i = 0; i < ans.size(); i++) { if(i) { cout << " "; } cout << ans[i]; } cout << endl; } } return 0;}
0 0
- 122 - Trees on the level(模拟内存池解法)
- 122 - Trees on the level(动态分配空间解法)
- 122 - Trees on the level
- 122 - Trees on the level
- 122 - Trees on the level
- 122 Trees on the level
- TREES ON THE LEVEL
- Trees on the level
- Trees on the level
- Trees on the level
- UVa Problem 122 - Trees on the level
- UVaOJ 122 - Trees on the level
- UVA 122 - Trees on the level
- uva 122 - Trees on the level
- uva 122 Trees on the level
- UVA-122-Trees on the level
- UVA - 122 Trees on the level
- UVa 122 - Trees on the level
- JavaScript实现Java的List功能
- SQL查询语句通配符与ACCESS模糊查询like的解决方法
- Cannot read property 'add' of null
- 画饼状图一
- 在linux下制作静态库和动态链接库的方法
- 122 - Trees on the level(模拟内存池解法)
- JQuery的jquery.min.js与jquery.js
- POJ 3468 A Simple Problem with Integers
- c++开发ocx入门实践二
- 计算机原理 —— 主板与内存映射
- 摩托车继承自行车和机动车
- mysql 下phpmyadmin安装
- 关于recyclerview的横向布局的宽度以及item的点击事件的添加
- Android Studio快捷键