UVA122 trees on the level(BFS)
来源:互联网 发布:光翼进阶数据 编辑:程序博客网 时间:2024/05/23 19:23
写了n遍总算是写对了,不能停止地相信自己。
#include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;const int maxn = 300;struct Node{ bool have_value;//标记是否赋值 int v;//值 Node *left, *right;//左节点和右节点 Node():have_value(false),left(NULL),right(NULL){}//成员函数,初始化结构体变量};Node* root = NULL;//根节点rootchar s[maxn];//存输入的字符数组bool failed;//标记正确错误的变量Node* newnode()//把new Node()封装到newnode()里{ return new Node();}void addnode(int v, char * s)//创建链表,形成二叉树{ int n = strlen(s);//s的长度 Node* u = root;//把根节点赋给u for(int i = 0; i < n; i++)//便利LR字符串 { 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;//v赋给当前节点的v u->have_value = true;//复制成功,标记为已有值}void remove_tree(Node* u)//释放内存{ if(u == NULL) return;//当为0时 停止释放 remove_tree(u->left);//先释放左节点 remove_tree(u->right);//再释放右节点 delete u;//删除u}bool read_input()//读入二叉树{ failed = false;//开始标记failed为false remove_tree(root);//释放上一个二叉树的内存 root = newnode();//初始化root的值 for(;;)//读取本次二叉树所用的循环 { if(scanf("%s", s) != 1) return false;//EOF输入结束 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;//定义Node*类型的队列 ans.clear();//清空不定长数组ans q.push(root);//根节点(根节点本来就是指向结构体的指针)进栈 while(!q.empty())//只要q不为空,便利所有不为空的节点 { Node* u = q.front();//u为队首结构体指针 q.pop();//赋值后出栈 if(!u->have_value) return false;//如果他没有值,错误了 ans.push_back(u->v);//想不定长数组尾添加当前结构体的v if(u->left != NULL) q.push(u->left);//先向左便利,后向右便利 if(u->right != NULL) q.push(u->right); } return true;}int main(){ vector<int> ans;//定义的不定长数组,存入是每个二叉树的值 while(read_input())//读入,返回FALSE时结束 { if(!bfs(ans)) failed = true;//当bfs返回为false,出现错误 if(failed) printf("not complete\n"); else {//输出每个结构体的值 for(int i= 0 ; i < ans.size(); i++) { if(i != 0) printf(" "); printf("%d", ans[i]); } printf("\n"); } } return 0;}
注释都为自己写的,如有纰漏,请大神指正。
0 0
- UVA122 trees on the level(BFS)
- UVa122 Trees on the level
- uva122-Trees on the level
- uva122 trees on the level
- UVA122 - Trees on the level
- UVA122 Trees on the level
- UVa122:Trees on the level
- [UVA122]Trees on the level
- UVA122 Trees on the level【二叉树】【BFS】
- 例题6-7 UVa122 Trees on the level(树&&队列BFS&&sscanf字符串转整数)
- Uva122 Trees on the level【指针建立树+bfs遍历树】【例题6-7】
- uva122 Trees on the level(锻炼指针建一棵二叉树)
- Trees on the level(UVA122,二叉树的建立)
- Uva122 Trees on the level 二叉树的数组实现
- UVA122 Trees on the level(二叉树建立与遍历)
- uva122 Trees on the level (数组,指针两种实现方式)
- TREES ON THE LEVEL
- Trees on the level
- 约翰霍普金斯大学数据科学系列课程——R语言:数据类型
- 试用Windows Azure 感受
- scala学习笔记:集合
- 四大法宝应付主板常见故障
- VS2008宏解读
- UVA122 trees on the level(BFS)
- NSArray和NSString的互相转换
- dtree的下载与使用
- SSHSecureShellClient(ssh client)在windows下的使用
- startActivityForResult()的用法
- 上行数据加速技术与移动新应用
- 冬令营第十二天
- 有线无线一体化网络设计原则
- Java访问Domino(Java,Domino,diiop,远程访问)