您的位置:首页 > 其它

UVa122 二叉树的层次遍历

2017-01-09 11:03 453 查看
主要是将两种内存存储方式写一写,还有使用队列实现的广度优先遍历BFS

动态分配内存分配

缺点:容易引起内存泄漏

Node* newNode(){ return new Node(); }//使用new的时候就会调用构造函数,给新创建的节点初始化值


内存池(队列实现)

queue<Node*> freenodes;
Node node[maxn];

void memory_pool_init()
{
for (int i = 0; i < maxn; i++)
{
freenodes.push(&node[i]);
}
}

Node* newNode_static()
{
Node *u = freenodes.front(); freenodes.pop();
u->have_value = false; u->left = NULL; u->right = NULL;
return u;
}

void deleteNode(Node*u)
{
freenodes.push(u);//将节点还回去就是释放了节点
}

void remove_tree_static(Node* u)
{
if (u == NULL) return;
remove_tree_static(u->left);
remove_tree_static(u->right);
deleteNode(u);
}


首先在全局数据区声明一个Node型数组,然后将数组中的每一个结构体类型的头指针进行入队操作,当要使用的时候,进行出队操作,得到结构体的头指针。

释放内存也很简单,就是将内存重新入队操作,添加到队列中。

在释放整个树的内存的时候使用了递归的思想

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>

using namespace std;
const int maxn = 256 + 10;

struct Node
{
bool have_value;
int v;
struct Node* left;
struct Node* right;
Node() :have_value(false), left(NULL), right(NULL){}
};

Node root_node = Node();
Node* root = &root_node;//根节点

/*
*使用内存池方式分配二叉树节点内存
*/
queue<Node*> freenodes; Node node[maxn]; void memory_pool_init() { for (int i = 0; i < maxn; i++) { freenodes.push(&node[i]); } } Node* newNode_static() { Node *u = freenodes.front(); freenodes.pop(); u->have_value = false; u->left = NULL; u->right = NULL; return u; } void deleteNode(Node*u) { freenodes.push(u);//将节点还回去就是释放了节点 } void remove_tree_static(Node* u) { if (u == NULL) return; remove_tree_static(u->left); remove_tree_static(u->right); deleteNode(u); }

/*
*使用动态内存分配的方式分配二叉树所使用的内存
*/
Node* newNode(){ return new Node(); }//使用new的时候就会调用构造函数,给新创建的节点初始化值

//使用递归的方法释放树节点
void remove_tree(Node* u)
{
if (u == NULL) return;
remove_tree(u->left);
remove_tree(u->right);
delete u;
}

bool failed;

void addnode(int v, char* s)
{
int n = strlen(s);
Node* u = root;
for (int i = 0; i < n; i++)
{
if (s[i] == 'L')
{
if (u->left == NULL) { u->left = newNode_static(); }//节点不存在创建新的节点
u = u->left;//向左移动一下
}
else if (s[i] == 'R')
{
if (u->right == NULL) { u->right = newNode_static(); }
u = u->right;
}
}
if (u->have_value) failed = true;//判断节点是否已经赋过值,如果已经赋过值表明结果有误
u->v = v;
u->have_value = true;
}

char s[maxn];

bool read_input()
{
failed = false;
while (1)
{
if (scanf("%s", &s) != 1) return false;
if (!strcmp(s, "()")) break; //读到“()”结束,退出循环
int v;
sscanf(&s[1], "%d", &v);
addnode(v, strchr(s, ',') + 1);
}
return true;
}

bool BFS(vector<int>& ans)
{
queue<Node*> q;
ans.clear();
q.push(root); //初始时只有一个根节点
while (!q.empty())
{
Node* u = q.front(); 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()
{
memory_pool_init();
vector<int> ans;
while (read_input())
{
if (!BFS(ans)) failed = true;
if (failed) printf("not complete\n");
else for (auto i : ans) cout << i << " ";
cout << endl;
}

remove_tree_static(root);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: