Trees on the level HDU

来源:互联网 发布:http的默认端口 编辑:程序博客网 时间:2024/05/18 00:56
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.
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

        
                       

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

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.

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 1

not complete


#include<queue>#include<vector>#include<stdio.h>#include<string.h>using namespace std;// 二叉树节点类型struct node{    int val; // 值    int have_val; // 是否被赋值    node * left;  // 左子节点    node * right;  // 右子节点    // 构造方法  一定要注意将have_val初始化    node():have_val(0),left(NULL),right(NULL) {}};int flag; // 标记node * root; // 根节点vector<int>res; //存储二叉树的先序遍历结果// 递归释放二叉树void removetree(node *p){    if(p==NULL)        return ;    removetree(p->left); // 删除左子树    removetree(p->right); // 删除右子树    delete(p); //删除此节点}// v为插入节点的值,str为插入节点的序列void addnode(int v,char *str){    int n=strlen(str);    node *u=root;    // 循环读取插入结点序列,包含了根节点的情况    for(int i=0; i<n; i++)        if(str[i]=='L') // 往左子树走        {            if(u->left==NULL) // 左子节点不存在                u->left = new node(); // 创建一个新节点,并挂在u的左子节点域上            u=u->left; // 指向该节点        }        else if(str[i]=='R')        {            if(u->right==NULL)                u->right=new node(); // 创建一个新节点,并挂在u的右子节点域上            u=u->right; // 指向该节点        }    if(u->have_val) // 已经赋值过了,则表示输入有误        flag=1;    u->have_val=1; // 标记,表示已赋值    u->val=v;}// 读取输入int input(){    char str[300];    flag=0; // 初始化    root=new node();    for(;;)    {        if(scanf("%s",str)!=1)            return 0;        if(!strcmp(str,"()")) // ()退出输入节点            break;        int v;        sscanf(&str[1],"%d",&v); // 字符串转数字        addnode(v,strchr(str,',')+1);  // 插入节点    }    return 1;}// 广度优先遍历,返回值为是否某一节点未被赋值int bfs(){    queue<node *> Q; // 广度优先遍历所需要的队列    Q.push(root); // 初始时只有一个根节点    while(!Q.empty())    {        node *u=Q.front();        Q.pop();        if(!u->have_val) // 如果有节点未被赋值            return 0;        res.push_back(u->val); // 将序列加入结果容器        if(u->left!=NULL)            Q.push(u->left); // 左子节点入栈        if(u->right!=NULL)            Q.push(u->right); // 右子节点入栈    }    return 1;}void output(){    if(flag)        printf("not complete\n");    else    {        for(int i=0; i<res.size(); i++)            if(i==0)                printf("%d",res[i]);            else                printf(" %d",res[i]);        printf("\n");    }}int main(){    while(input())    {        res.clear(); // 清除上一次的序列        if(!bfs())            flag=1;        output();        removetree(root);    }    return 0;}


原创粉丝点击