【c语言数据结构】遍历二叉树

代码一运行就停止工作,求助大神,看看我的问题是什么

#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode//定义
{
    int data;
    struct treeNode *left;
    struct treeNode *right;
}treenode,*TreeNode;
void pre(TreeNode node)//前序遍历
{

    if(node==NULL)
        return ;
    printf("%d ", node->data);
    pre(node->left);
    pre(node->right);
}
void mid(TreeNode node)//中序遍历
{

    if(node==NULL)
        return ;
    mid(node->left);
    printf("%d ", node->data);
    mid(node->right);
}
void beh(TreeNode node)//后序遍历
{

    if(node==NULL)
        return ;
    beh(node->left);
    beh(node->right);
    printf("%d ", node->data);
}
void tree(TreeNode one)//定义一个现成的二叉树
{
    one=(TreeNode*)malloc(sizeof(treenode));
    one->left=(TreeNode*)malloc(sizeof(treenode));
    one->right=(TreeNode*)malloc(sizeof(treenode));
    one->right->left=(TreeNode*)malloc(sizeof(treenode));
    one->right->right=(TreeNode*)malloc(sizeof(treenode));
    one->left->left=(TreeNode*)malloc(sizeof(treenode));
    one->left->right=(TreeNode*)malloc(sizeof(treenode));
    one->right->left->left=(TreeNode*)malloc(sizeof(treenode));
    one->right->left->right=(TreeNode*)malloc(sizeof(treenode));
    one->right->right->left=(TreeNode*)malloc(sizeof(treenode));
    one->right->right->right=(TreeNode*)malloc(sizeof(treenode));

    one->data=3;
    one->left->data=9;
    one->right->data=20;
    one->right->left->data=15;
    one->right->right->data=7;
}
void main()//主方法
{
    TreeNode  one;
    tree(one);
    printf("该二叉树的前序遍历为:\n");
    pre(one);
    printf("该二叉树的中序遍历为:\n");
    mid(one);
    printf("该二叉树的后序遍历为:\n");
    beh(one);
}


1个回答

// Q1054302.cpp : Defines the entry point for the console application.
//

#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode//定义
{
    int data;
    treeNode *left;
    treeNode *right;
} treenode, *TreeNode;
void pre(TreeNode node)//前序遍历
{

    if(node==NULL)
        return ;
    printf("%d ", node->data);
    pre(node->left);
    pre(node->right);
}
void mid(TreeNode node)//中序遍历
{

    if(node==NULL)
        return ;
    mid(node->left);
    printf("%d ", node->data);
    mid(node->right);
}
void beh(TreeNode node)//后序遍历
{

    if(node==NULL)
        return ;
    beh(node->left);
    beh(node->right);
    printf("%d ", node->data);
}
void tree(TreeNode one)//定义一个现成的二叉树
{
    one->data=3;
    one->left=(treenode*)malloc(sizeof(treenode));
        one->left->data=9;
        one->left->left=NULL;
        one->left->right=NULL;
    one->right=(treenode*)malloc(sizeof(treenode));
        one->right->data=20;
        one->right->left=(treenode*)malloc(sizeof(treenode));
            one->right->left->data=15;
            one->right->left->left=NULL;
            one->right->left->right=NULL;
        one->right->right=(treenode*)malloc(sizeof(treenode));
            one->right->right->data=7;
            one->right->right->left=NULL;
            one->right->right->right=NULL;
}
int main()//主方法
{
    TreeNode  one;
    one=(treenode*)malloc(sizeof(treenode));
    tree(one);
    printf("该二叉树的前序遍历为:\n");
    pre(one);
    printf("\n该二叉树的中序遍历为:\n");
    mid(one);
    printf("\n该二叉树的后序遍历为:\n");
    beh(one);
}

该二叉树的前序遍历为:
3 9 20 15 7
该二叉树的中序遍历为:
9 3 15 20 7
该二叉树的后序遍历为:
9 15 7 20 3 Press any key to continue . . .

caozhy
每个人都有一个梦才不会孤单的说话就有天堂 回复惊鸿之羽: 你代码根本不能编译,指针也没有初始化
大约 2 个月之前 回复
yuhuajinghong
惊鸿之羽 谢谢,但是能不能请问一下是什么导致我这个代码一运行就停止工作的?
大约 2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问