一个建树的程序
来源:互联网 发布:类似于知乎 编辑:程序博客网 时间:2024/05/16 05:28
在网上看到微软的一个面试题,自己写了一遍,虽然结果正确,但代码写的不太简洁,仅供参考:
一个TXT文件,每一行代表一个树的节点,格式为:
“/Node1/Node2/Num3/.../NodeN”,其中NodeN的格式为{KeyN, ValueN},Key和Value都是32位十进制的整形数字。如/{0, 100}/{2, 200}/{4, 400}
写程序读取该文件,并建立树的数据结构。注意:
1. 要验证树是不是合法的。
2. 如没有时间按,文件读取函数可以省略,可以用getline。
最后我用BSF(广度优先搜索),按层遍历输,下面是一个测试用例:
Input: tree.txt
{0,1}
{0,1}/{2,100}
{0,1}/{3,200}
{0,1}/{2,100}/{4,300}
{0,1}/{3,200}/{5,300}/{6,100}
{0,1}/{8,800}
{0,1}/{2,100}/{9,100}
Output: result.txt
{0,1}
{2,100}\{3,200}\{8,800}
{4,300}\{9,100}\{5,300}
{6,100}
//Tree.cpp
#include<iostream>#include<string> #include<vector> using namespace std;typedef struct node* point; struct node{ node(int k1, int v1) {k = k1; v =v1; c.clear();} node(){k = 0; v = 0; c.clear();} int k; int v; vector<point> c; };struct nodeinf{ nodeinf(int k, int v){key = k; value = v;} int key; int value; }; int main(){ freopen("tree.txt","r",stdin); freopen("result.txt","w",stdout); //build a tree with node p; point root=0; string s; int kbegin, kend, vbegin, vend; string skey; string svalue; int ikey, ivalue; vector<nodeinf> line; while(getline(cin,s)) //cout<<s<<endl; { int i=0; while(i < s.length()) { switch (s[i]) { case '\{': kbegin=++i; break; case '\,': kend=i-1; vbegin=i+1; skey.assign(s, kbegin, kend); ikey=atoi(skey.c_str()); i++; break; case '\}': vend=i-1; svalue.assign(s, vbegin, vend); ivalue=atoi(svalue.c_str()); line.push_back(nodeinf(ikey, ivalue)); i++; break; default: i++; break; } } //for(int i=0; i < line.size(); i++) // cout<<line[i].key<<" "<<line[i].value<<endl; if(!root) { int i=0; root=new node; (*root).k=line[i].key; (*root).v=line[i].value; point curr=root; //cout<<(*curr).k<<endl; for(i=1; i<line.size(); i++) { point q=new node; (*q).k=line[i].key; (*q).v=line[i].value; ((*curr).c).push_back(q); curr=*(((*curr).c).begin()); //cout<<(*curr).k<<endl; } } else { point curr=root; for(int i=1; i < line.size(); i++) { if(((*curr).c).empty()) { point q=new node; (*q).k=line[i].key; (*q).v=line[i].value; ((*curr).c).push_back(q); curr=*(((*curr).c).begin()); //cout<<(*curr).k<<endl; } else { vector<point>::iterator it; for(it=((*curr).c).begin(); it < ((*curr).c).end(); it++) { if((*(*it)).k == line[i].key) { curr=*it; break; } } if(it == ((*curr).c).end()) { point q=new node; (*q).k=line[i].key; (*q).v=line[i].value; ((*curr).c).push_back(q); curr=*(((*curr).c).end()-1); //cout<<(*curr).k<<endl; } } //it=find(((*p)->c).begin(),((*p)->c).end(),line[i].key); //curr=*it; //cout<<(*curr).k<<endl; } } line.clear(); s.clear(); //skey.clear(); //svalue.clear(); } //BFS vector<node> queue; queue.clear(); int a=0;//left(head) int b=-1;//right(rear) point p=root; queue.push_back(*p); b++; int c=a,d=b; while( a <= b ) { cout<< queue[a].k <<" "; if(!queue[a].c.empty()) { for(int i=0; i < (queue[a].c).size(); i++) { queue.push_back(*(queue[a].c)[i]); b++; } } if(c == d) { cout<<endl; //c=a; d=b; } a++; c++; } return 0; }
- 一个建树的程序
- perl的建树算法
- 线段树的建树
- 建树的方法
- 论建树的方法
- 建树
- 二叉树的建树方法
- 动态建树之——一个单词是否在其他单词中以前缀的形式存在
- 1398 建树和叶子节点的个数
- 建树和叶子节点的个数 1398
- 线段树的两种建树模型
- The Falling Leaves 简单的遍历+建树
- HDU 5444-二叉树的遍历+建树
- 二叉搜索建树及相应的函数
- 主席树的不同建树方式
- pat 1004---简单的建树+层次遍历
- 数据结构——二叉树的建立和遍历(递归建树&层序遍历建树)
- 一个程序的演变
- linux c语言 select函数用法
- Android开发之SlidingDrawer(一)
- 使用JLINK GDBSERVER调试U-BOOT的方法
- 五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O
- 版本管理的选择——Git
- 一个建树的程序
- 把二元查找树转变成排序的双向链表
- SetTimer(1,1000,ShowText2);
- rt3070驱动移植及测试
- 【应用实例】之按照出现率显示数据--【叶子】
- 反向 Ajax,第 2 部分: WebSockets
- 晶体二极管
- android学习笔记(四)
- JAVA基础第九天学习日记_内部类、匿名内部类、异常