| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1614 人关注过本帖
标题:段错误!!!
只看楼主 加入收藏
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:8 
段错误!!!
#include <cstdio>  
#include <queue>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
const int maxn = 100;   
bool Isnodroot[maxn] = {false};
struct Node{
//    int data;c
    int lchild;
    int rchild;
}node[maxn];  
int n, num = 0;
void print(int index) {
   
    printf("%d",index);
    num++;
    if(num < n) printf(" ");
    else printf("\n");
   
}
void inorder(int root) {
    if(root == -1)   return;
    inorder(node[root].lchild);
    print(root);
    inorder(node[root].rchild);
}

void bfs(int root){
    queue<int> q;
    q.push(root);
    while( !q.empty() ){
        int front = q.front();
        
        q.pop();
        print(front);
        if(node[front].lchild != -1)  {
            q.push(node[front].lchild);
        }
        if(node[front].rchild != -1)  
        {
            q.push(node[front].rchild);
        }
    }
   
}
void postInvert(int root){
    if(root == -1)  return;
    postInvert(node[root].lchild);
    postInvert(node[root].rchild);
    swap(node[root].lchild, node[root].rchild);
}
int nodroot(){
    for(int i = 0; i < n; i++) {
        if(Isnodroot[i] == false) return i;
    }
}
int str2Num(char c) {
    if(c == '-') return -1;
    else{
        Isnodroot[c - '0'] = true;
        return c - '0';
    }
}

int main()
{
    freopen("1.txt","r",stdin);
    string a;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        
        
        scanf("%s",&a);
        getchar();
        if(a[0] != '-') {
            node[i].lchild = a[0] - '0';
            Isnodroot[node[i].lchild] = true;
        }else node[i].lchild = -1;
        if(a[1] != '-') {
            node[i].rchild = a[1] - '0';
            Isnodroot[node[i].rchild] = true;
        }else node[i].rchild = -1;
    }
   int root = nodroot();
   postInvert(root);
   bfs(root);
   num = 0;
   inorder(root);
   return 0;
}
大致意思是将一棵二叉树每一个节点的左右子树颠倒之后,输出它的层序遍历序列和中序遍历序列。
输入格式:
二叉树总节点个数 N (二叉树各节点的编号是0 ~ N-1)
各节点的左右子树编号 (若为空,则用 '-'表示)

代码运行出现了段错误,不知道错在哪了!! 求助

1.txt 中的内容:

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
搜索更多相关主题的帖子: include int Node return 节点 
2018-09-05 17:55
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
string a;
……
scanf("%s",&a);
这是什么玩意儿?
2018-09-06 09:13
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
二叉树总节点个数 N (二叉树各节点的编号是0 ~ N-1)
各节点的左右子树编号 (若为空,则用 '-'表示)

8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6

完全看不懂,在尊驾肯贴出原题目之前,爱莫能助


2018-09-06 09:20
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
收藏
得分:0 
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N−1. Then N lines follow, each corresponds to a node from 0 to N−1, and gives the indices of the left and right children of the node. If the child does not exist, a - will be put at the position. Any pair of children are separated by a space.

Output Specification:
For each test case, print in the first line the level-order, and then in the second line the in-order traversal sequences of the inverted tree. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
3 7 2 6 4 0 5 1
6 5 7 4 3 2 0 1
2018-09-08 21:55
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
收藏
得分:0 
以上是原题
2018-09-08 21:56
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
收藏
得分:0 
一直没找到在哪可以回复评论,就把帖子给结了
2018-09-08 21:59
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
收藏
得分:0 
回复 2楼 rjsp
sorry,没系统学过c++,出现如此低级的错误。
但是我改成 “cin>> a;”之后还是段错误,导致段错误的原因不在这,是吗。
2018-09-08 22:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
你的代码是这样么?
        cin>> a;
        getchar();
        if(a[0] != '-') {
            node[i].lchild = a[0] - '0';
            Isnodroot[node[i].lchild] = true;
        }else node[i].lchild = -1;
        if(a[1] != '-') {
            node[i].rchild = a[1] - '0';
            Isnodroot[node[i].rchild] = true;
        }else node[i].rchild = -1;
我猜你想要的是
程序代码:
    for(int i = 0; i < n; i++)
    {
        char a[2];
        cin >> a[0] >> a[1];

        if(a[0] != '-') {
            node[i].lchild = a[0] - '0';
            Isnodroot[node[i].lchild] = true;
        }else node[i].lchild = -1;
        if(a[1] != '-') {
            node[i].rchild = a[1] - '0';
            Isnodroot[node[i].rchild] = true;
        }else node[i].rchild = -1;
    }

你代码的最大问题是不排版,且变量混杂。
程序代码:
#include <cstdio>
#include <queue>

void level_order( const char a[][2], unsigned n, unsigned root )
{
    std::queue<unsigned> qe;
    qe.push( root );

    while( !qe.empty() )
    {
        unsigned f = qe.front();
        qe.pop();

        if( a[f][1] != '-' )
            qe.push( a[f][1]-'0' );

        if( a[f][0] != '-' )
            qe.push( a[f][0]-'0' );

        printf( "%u", f );
        if( !qe.empty() )
            putchar( ' ' );
    }
}

void in_order( const char a[][2], unsigned n, unsigned root )
{
    if( a[root][1] != '-' )
    {
        in_order( a, n, a[root][1]-'0' );
        putchar( ' ' );
    }

    printf( "%u", root );

    if( a[root][0] != '-' )
    {
        putchar( ' ' );
        in_order( a, n, a[root][0]-'0' );
    }
}

int main( void )
{
    // 输入
    unsigned n;
    char a[10][2];
    {
        scanf( "%u", &n );
        for( unsigned i=0; i!=n; ++i )
            scanf( " %c %c", &a[i][0], &a[i][1] );
    }

    // 获得root
    unsigned root;
    {
        root = n*(n-1)/2;
        for( unsigned i=0; i!=n; ++i )
            root -= (a[i][0]=='-'?0:a[i][0]-'0') + (a[i][1]=='-'?0:a[i][1]-'0');
    }

    // 层序遍历
    level_order( a, n, root );
    putchar( '\n' );

    // 中序遍历
    in_order( a, n, root );
    putchar( '\n' );
}


2018-09-10 12:15
zyuanlin
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2018-5-1
收藏
得分:0 
回复 8楼 rjsp
谢谢你
2018-09-11 13:22
快速回复:段错误!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022814 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved