请大家帮我看看我的二叉树,谢谢!
来源:百度知道 编辑:UC知道 时间:2024/06/17 16:33:53
#include<stdio.h>
#include<stdlib.h>
typedef struct BTNode *tree_pointer;
typedef struct BTNode
{
int data;
tree_pointer left_child, right_child;
};
//初始化
void IntializeBT(tree_pointer bt)
{
bt = NULL;
return bt;
}
//建立二叉树
void CreateBT(tree_pointer bt, int val)
{
bt = malloc(sizeof(BTNode));
bt ->data = val;
CreateBT(bt ->left_child, val);
CreateBT(bt ->right_child, val);
}
//前序遍历
void PreOrder(tree_pointer bt)
{
if(bt)
{
printf("%d", bt ->data);
PreOrder(bt ->left_child);
PreOrder(bt ->right_child);
}
}
//中序遍历
void InOrder(tree_pointer bt)
{
if(bt)
{
InOrder(bt ->left_child);
printf("%d",bt ->data);
InOrder(bt ->right_child);
}
}
//后序遍历
void
#include<stdlib.h>
typedef struct BTNode *tree_pointer;
typedef struct BTNode
{
int data;
tree_pointer left_child, right_child;
};
//初始化
void IntializeBT(tree_pointer bt)
{
bt = NULL;
return bt;
}
//建立二叉树
void CreateBT(tree_pointer bt, int val)
{
bt = malloc(sizeof(BTNode));
bt ->data = val;
CreateBT(bt ->left_child, val);
CreateBT(bt ->right_child, val);
}
//前序遍历
void PreOrder(tree_pointer bt)
{
if(bt)
{
printf("%d", bt ->data);
PreOrder(bt ->left_child);
PreOrder(bt ->right_child);
}
}
//中序遍历
void InOrder(tree_pointer bt)
{
if(bt)
{
InOrder(bt ->left_child);
printf("%d",bt ->data);
InOrder(bt ->right_child);
}
}
//后序遍历
void
(1) 应该先定义结构体然后再,typedef struct BTNode *tree_pointer;
(2) bt = malloc(sizeof(BTNode));应该改为 bt=(tree_pointer)malloc(sizeof(BTNode));
(3)CreateBT(bt ->left_child, val); 这句每次递归调用的时候你输入的 val都相同,应该是不同的val
第一个问题:bt=(tree_pointer)malloc(sizeof(BTNode));