数据结构/树/CodeBlocks/debug报错rogram received signal SIGSEGV, Segmentation fault.

liexineyi 2017-12-24 03:25:00
题目是:给定一个二叉树,对于这个二叉树结点的值,同一个边上的值不能同时选取(即两个结点的关系为双亲和孩子的关系, 则两个结点在同一个边上, 那么这两个结点至多只能选取一个),将选取的结点的值相加, 求这个二叉树所能选取出的结点值的和的最大值。输入的严格按照二叉树的先序遍历构造二叉树。
我想的基本思路是想把数据按先序录入之后,把单数层求和、双数层求和,输出较大值。
如果显示有问题请看下面纯文本的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Bitree{
int data;
struct Bitree *lchild;
struct Bitree *rchild;
};

void CreateBitree(struct Bitree *bt){
int val;
scanf("%d",&val);
if(val==0) bt=NULL;
else{
bt = (struct Bitree*)malloc(sizeof(struct Bitree));
bt->data = val;
printf("%d",bt->data);
CreateBitree(bt->lchild);
CreateBitree(bt->rchild);
}
}
int PT1(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//下面这句报错
count=T->data+PT1(T->lchild->lchild)+PT1(T->lchild->rchild)+PT1(T->rchild->lchild)+PT1(T->rchild->rchild);
}
printf("%d",count);
return count;
}
int PT2(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//同理下面这句报错
count=T->lchild->data+T->rchild->data+PT2(T->lchild)+PT2(T->rchild);
}
printf("%d",count);
return count;
}
int main()
{
struct Bitree *T;
int max;
CreateBitree(T);
int cir1=PT1(T);
int cir2=PT2(T);
max = cir1>cir2?cir1:cir2;
printf("%d",max);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Bitree{
int data;
struct Bitree *lchild;
struct Bitree *rchild;
};

void CreateBitree(struct Bitree *bt){
int val;
scanf("%d",&val);
if(val==0) bt=NULL;
else{
bt = (struct Bitree*)malloc(sizeof(struct Bitree));
bt->data = val;
printf("%d",bt->data);
CreateBitree(bt->lchild);
CreateBitree(bt->rchild);
}
}
int PT1(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//下面这句报错
count=T->data+PT1(T->lchild->lchild)+PT1(T->lchild->rchild)+PT1(T->rchild->lchild)+PT1(T->rchild->rchild);
}
printf("%d",count);
return count;
}
int PT2(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//同理下面这句报错
count=T->lchild->data+T->rchild->data+PT2(T->lchild)+PT2(T->rchild);
}
printf("%d",count);
return count;
}
int main()
{
struct Bitree *T;
int max;
CreateBitree(T);
int cir1=PT1(T);
int cir2=PT2(T);
max = cir1>cir2?cir1:cir2;
printf("%d",max);
return 0;
}

...全文
320 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-12-26
  • 打赏
  • 举报
回复
仅供参考:
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct BiTNode {//二叉树结点
    char data;                      //数据
    struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int nn=0;
int CreateBiTree(BiTree *T) {//按先序序列创建二叉树
    char data;
    scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
    if (data == '#') {
        *T = NULL;
    } else {
        *T = (BiTree)malloc(sizeof(BiTNode)); nn++;
        (*T)->data = data;         //生成根结点
        CreateBiTree(&(*T)->lchild);//构造左子树
        CreateBiTree(&(*T)->rchild);//构造右子树
    }
    return 0;
}
void Visit(BiTree T) {//输出
    if (T->data != '#') {
        printf("%c ",T->data);
    }
}
void PreOrder(BiTree T) {//先序遍历
    if (T != NULL) {
        Visit(T);               //访问根节点
        PreOrder(T->lchild);    //访问左子结点
        PreOrder(T->rchild);    //访问右子结点
    }
}
void InOrder(BiTree T) {//中序遍历
    if (T != NULL) {
        InOrder(T->lchild);     //访问左子结点
        Visit(T);               //访问根节点
        InOrder(T->rchild);     //访问右子结点
    }
}
void PostOrder(BiTree T) {//后序遍历
    if (T != NULL) {
        PostOrder(T->lchild);   //访问左子结点
        PostOrder(T->rchild);   //访问右子结点
        Visit(T);               //访问根节点
    }
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
    BiTree *stack=(BiTree *)malloc(nn*sizeof(BiTree));
    int sp=0;
    BiTree p = T;//p是遍历指针
    while (p || sp) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack[sp]=p;sp++;       //存入栈中
            printf("%c ",p->data);  //访问根节点
            p = p->lchild;          //遍历左子树
        } else {
            sp--;p=stack[sp];       //退栈
            p = p->rchild;          //访问右子树
        }
    }
    free(stack);
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
    BiTree *stack=(BiTree *)malloc(nn*sizeof(BiTree));
    int sp=0;
    BiTree p = T;//p是遍历指针
    while (p || sp) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack[sp]=p;sp++;       //存入栈中
            p = p->lchild;          //遍历左子树
        } else {
            sp--;p=stack[sp];       //退栈
            printf("%c ",p->data);
            p = p->rchild;          //访问右子树
        }
    }
    free(stack);
}

typedef struct BiTNodePost{
    BiTree biTree;
    char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
    BiTreePost *stack=(BiTreePost *)malloc(nn*sizeof(BiTreePost));
    int sp=0;
    BiTree p = T;//p是遍历指针
    BiTreePost BT;
    while (p != NULL || sp) {//栈不空或者p不空时循环
        while (p != NULL) {//遍历左子树
            BT = (BiTreePost)malloc(sizeof(BiTNodePost));
            BT->biTree = p;
            BT->tag = 'L';//访问过左子树
            stack[sp]=BT;sp++;       //存入栈中
            p = p->lchild;
        }
        while (sp && (stack[sp-1])->tag == 'R') {//左右子树访问完毕访问根节点
            sp--;BT=stack[sp];        //退栈
            printf("%c ",BT->biTree->data);
            free(BT);
        }
        if (sp) {//遍历右子树
            BT=stack[sp-1];
            BT->tag = 'R';//访问过右子树
            p = BT->biTree;
            p = p->rchild;
        }
    }
    free(stack);
}
void LevelOrder(BiTree T) {//层次遍历
    BiTree p;
    BiTree *queue;
    int h=0,t=0,n=0;

    if (T == NULL) return;
    p=T;
    queue=(BiTree *)malloc(nn*sizeof(BiTree));
    queue[t]=p;t=(t+1)%10;n++;//根节点入队
    while (n) {    //队列不空循环
        p=queue[h];             //对头元素出队
        printf("%c ",p->data);  //访问p指向的结点
        h=(h+1)%10;n--;         //退出队列
        if (p->lchild != NULL) {//左子树不空,将左子树入队
            queue[t]=p->lchild;t=(t+1)%10;n++;
        }
        if (p->rchild != NULL) {//右子树不空,将右子树入队
            queue[t]=p->rchild;t=(t+1)%10;n++;
        }
    }
    free(queue);
}
int main() {
    BiTree T;

    setlocale(LC_ALL,"chs");
    CreateBiTree(&T);

    printf("先序遍历        :");PreOrder  (T);printf("\n");
    printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
                                               printf("\n");
    printf("中序遍历        :");InOrder   (T);printf("\n");
    printf("中序遍历(非递归):");InOrder2  (T);printf("\n");
                                               printf("\n");
    printf("后序遍历        :");PostOrder (T);printf("\n");
    printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
                                               printf("\n");
    printf("层次遍历        :");LevelOrder(T);printf("\n");

    return 0;
}
//ABC##DE#G##F###
//先序遍历        :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历        :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历        :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历        :A B C D E F G
//

///       A
///      /
///     B
///    / \
///   C   D
///      / \
///     E   F
///      \
///       G
赵4老师 2017-12-26
  • 打赏
  • 举报
回复
进程意外退出会在当前目录下产生‘core’文件或形如‘core.数字’的文件比如‘core.1234’ 使用命令 gdb 运行程序名 core或core.数字 进入gdb然后使用bt命令 可以查看进程意外退出前函数调用的堆栈,内容为从上到下列出对应从里层到外层的函数调用历史。 如果进程意外退出不产生core文件,参考“ulimit -c core文件最大块大小”命令 “给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
wodexiaojidan 2017-12-26
  • 打赏
  • 举报
回复
引用 楼主 liexineyi 的回复:
题目是:给定一个二叉树,对于这个二叉树结点的值,同一个边上的值不能同时选取(即两个结点的关系为双亲和孩子的关系, 则两个结点在同一个边上, 那么这两个结点至多只能选取一个),将选取的结点的值相加, 求这个二叉树所能选取出的结点值的和的最大值。输入的严格按照二叉树的先序遍历构造二叉树。 我想的基本思路是想把数据按先序录入之后,把单数层求和、双数层求和,输出较大值。 如果显示有问题请看下面纯文本的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Bitree{
int data;
struct Bitree *lchild;
struct Bitree *rchild;
};

void CreateBitree(struct Bitree *bt){
int val;
scanf("%d",&val);
if(val==0) bt=NULL;
else{
bt = (struct Bitree*)malloc(sizeof(struct Bitree));
bt->data = val;
printf("%d",bt->data);
CreateBitree(bt->lchild);
CreateBitree(bt->rchild);
}
}
int PT1(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//下面这句报错
count=T->data+PT1(T->lchild->lchild)+PT1(T->lchild->rchild)+PT1(T->rchild->lchild)+PT1(T->rchild->rchild);
}
printf("%d",count);
return count;
}
int PT2(struct Bitree *T)
{
int count;
if (T!=NULL)
{
//同理下面这句报错
count=T->lchild->data+T->rchild->data+PT2(T->lchild)+PT2(T->rchild);
}
printf("%d",count);
return count;
}
int main()
{
struct Bitree *T;
int max;
CreateBitree(T);
int cir1=PT1(T);
int cir2=PT2(T);
max = cir1>cir2?cir1:cir2;
printf("%d",max);
return 0;
}
#include <stdio.h> #include <stdlib.h> #include <string.h> struct Bitree{ int data; struct Bitree *lchild; struct Bitree *rchild; }; void CreateBitree(struct Bitree *bt){ int val; scanf("%d",&val); if(val==0) bt=NULL; else{ bt = (struct Bitree*)malloc(sizeof(struct Bitree)); bt->data = val; printf("%d",bt->data); CreateBitree(bt->lchild); CreateBitree(bt->rchild); } } int PT1(struct Bitree *T) { int count; if (T!=NULL) { //下面这句报错 count=T->data+PT1(T->lchild->lchild)+PT1(T->lchild->rchild)+PT1(T->rchild->lchild)+PT1(T->rchild->rchild); } printf("%d",count); return count; } int PT2(struct Bitree *T) { int count; if (T!=NULL) { //同理下面这句报错 count=T->lchild->data+T->rchild->data+PT2(T->lchild)+PT2(T->rchild); } printf("%d",count); return count; } int main() { struct Bitree *T; int max; CreateBitree(T); int cir1=PT1(T); int cir2=PT2(T); max = cir1>cir2?cir1:cir2; printf("%d",max); return 0; }
这是你递归创建二叉树挖的坑,当输入0的时候只是有一个子节点为NULL,你这样搞不段错误就奇了怪了,劝你不要沉迷递归,递归只是一个构思,实现用迭代
自信男孩 2017-12-26
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Bitree{
    int data;
    struct Bitree *lchild;
    struct Bitree *rchild;
};

void CreateBitree(struct Bitree **bt){
    int val;
    scanf("%d",&val);
    if(val==0)
        *bt = NULL;
    else{
        *bt = (struct Bitree *)malloc(sizeof(struct Bitree));
        (*bt)->data = val;
        printf("%d", (*bt)->data);
        CreateBitree(&(*bt)->lchild);
        CreateBitree(&(*bt)->rchild);
    }
}
int PT1(struct Bitree *T)
{
    //int count;
    if (T) {
        return T->data + PT1(T->rchild);
    } else {
        return 0;
    }
    /*
    if (T!=NULL)
    {
        //下面这句报错
        count=T->data+PT1(T->lchild) + PT1(T->rchild);
        printf("%d ",count);

    }
    printf("+++%d ",count);
    */
    //return count;
}
int PT2(struct Bitree *T)
{
    if (T) {
        return T->data + PT2(T->lchild);
    } else {
        return 0;
    }
    /*
    int count;
    if (T!=NULL)
    {
        //同理下面这句报错
        count = T->data+PT2(T->lchild)+PT2(T->rchild);
        printf("%d ",count);
    }
    printf("+++%d ",count);
    return count;
    */
}
int main()
{
    struct Bitree *T;
    int max;
    CreateBitree(&T);
    int cir1=PT1(T);
    printf("\ncir1 = %d\n", cir1);
    int cir2=PT2(T);
    printf("\ncir2 = %d\n", cir2);
    max = cir1>cir2?cir1:cir2;
    printf("\n%d\n",max);
    return 0;
}
参考一下吧,有问题提出来。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧