UVA122二叉树BFS广搜入门

来源:互联网 发布:37传奇霸业羽毛数据 编辑:程序博客网 时间:2024/05/17 09:30


1)AC代码

#include <iostream>#include <string.h>#include <bits/stdc++.h>#include <queue>#include <vector>using namespace std;const int maxn=3000;char str[maxn];bool failed;vector <int> ans;struct Node{    Node *left,*right;    int v;    bool have_valued;    Node():have_valued(false),left(NULL),right(NULL){}//构造函数使结构体内成员初始化};Node* root;//?????  *,根节点需要单独先定义,建立树时定义一个节点等于根节点然后往下走同时将空的根节点进行了赋值,遍历树时也是从根节点让下走。Node* newnode()//将new申请内存空间,封装到函数里,该函数返回一个指向Node类型的指针{    return new Node();}void addnode(int num,string s){    int len=s.length();    Node *u=root;//建立一个u,用u往下走    for(int i=0;i<len-1;i++){        if(s[i]=='L'){           if(u->left==NULL)                u->left=newnode();           u=u->left;//用u往下走        }        else if(s[i]=='R'){            if(u->right==NULL)                u->right=newnode();            u=u->right;        }    }       if(u->have_valued){            failed=true;        }        u->have_valued=true;        u->v=num;}bool input(){    //memset(str,0,sizeof(str));    failed=false;    root=newnode();    while(1){        if(scanf("%s",str)!=1) return 0;//返回0是不是一样        if(!strcmp(str,"()")) break;        int num;        sscanf(&str[1],"%d",&num);//这里注意格式不要写错        //if(!addnode(num,strchr(str,',')+1)) return 2;!!注意,添加节点失败并不意味马上结束跳出?        addnode(num,strchr(str,',')+1);    }    return 1;}bool bfs(){    queue <Node*> q;    ans.clear();    q.push(root);    while(!q.empty()){        Node* u=q.front();q.pop();        if(!u->have_valued) return 0;//缺节点        ans.push_back(u->v);        if(u->left!=NULL) //if(u->left->have_valued)???            q.push(u->left);        if(u->right!=NULL)            q.push(u->right);    }    return 1;}void pri_out(){    for(int i=0;i<ans.size();i++){        if(i) cout<<" ";        cout<<ans[i];    }    cout<<endl;}int main(){    while(input()){           int j=bfs();        if(failed||!j){//前者是创建树时出错,后者是输出编号出错            cout<<"not complete"<<endl;        }        else{            pri_out();        }    }    return 0;}

2)WA代码,只改了最后的判断条件,有什么情况漏掉了!!??

#include <iostream>#include <string.h>#include <queue>#include <vector>#include <bits/stdc++.h>using namespace std;const int maxn=3000;char str[maxn];bool failed;vector <int> ans;struct Node{    Node *left,*right;    int v;    bool have_valued;    Node():have_valued(false),left(NULL),right(NULL){}//构造函数使结构体内成员初始化};Node* root;//?????  *,根节点需要单独先定义,建立树时定义一个节点等于根节点然后往下走同时将空的根节点进行了赋值,遍历树时也是从根节点让下走。Node* newnode()//将new申请内存空间,封装到函数里,该函数返回一个指向Node类型的指针{    return new Node();}bool addnode(int num,string s){    int len=s.length();    Node *u=root;//建立一个u,用u往下走    for(int i=0;i<len-1;i++){        if(s[i]=='L'){           if(u->left==NULL)                u->left=newnode();           u=u->left;//用u往下走        }        else if(s[i]=='R'){            if(u->right==NULL)                u->right=newnode();            u=u->right;        }    }       if(u->have_valued){            failed=true;            return 0;        }        u->have_valued=true;        u->v=num;        return 1;}int input(){    //memset(str,0,sizeof(str));    failed=false;    root=newnode();    while(1){        if(scanf("%s",str)!=1) return 0;//返回0是不是一样        if(!strcmp(str,"()")) break;        int num;        sscanf(&str[1],"%d",&num);//这里注意格式不要写错        //if(!addnode(num,strchr(str,',')+1)) return 2;!!注意,添加节点失败并不意味马上结束跳出?        if(!addnode(num,strchr(str,',')+1)) return 2;    }    return 1;}bool bfs(){    queue <Node*> q;    ans.clear();    q.push(root);    while(!q.empty()){        Node* u=q.front();q.pop();        if(!u->have_valued) return 0;        ans.push_back(u->v);        if(u->left!=NULL) //if(u->left->have_valued)???            q.push(u->left);        if(u->right!=NULL)            q.push(u->right);    }    return 1;}void pri_out(){    for(int i=0;i<ans.size();i++){        if(i) cout<<" ";        cout<<ans[i];    }    cout<<endl;}int main(){    while(1){        int k=input();        if(k==0)            break;        int j=bfs();        if(k==2||!j){//前者是创建树时出错,后者是输出编号出错            cout<<"not complete"<<endl;        }        else{            pri_out();        }    }    return 0;}

3)

Trees on the level
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

Background

Trees are fundamental in many branches of computer science. Current state-of-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.

The Problem

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

picture28

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 becompletely specified if every node on all root-to-node paths in the tree is given a value exactly once.

The 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.

The 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 1not complete

0 0
原创粉丝点击