vc建立便利二叉树

来源:百度知道 编辑:UC知道 时间:2024/06/05 08:21:43
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void CreateBiTree();
typedef struct Node
{
int data;
struct Node * LChild;
struct Node * RChild;
}BiTNode, *BiTree;

void CreateBiTree(BiTree *T) //建立二叉树
{
int ch;
scanf("%d",&ch);
if(ch=='*')
*T=NULL;
else
{
(*T)=(BiTNode *)malloc(sizeof(BiTNode));
(*T)->data=ch;
CreateBiTree(&((*T)->LChild)); //左子树
CreateBiTree(&((*T)->RChild)); //右子树
}
}

void PreOrder(BiTree root) //先序遍历二叉树, root为指向二叉树(或某一子树)根结点的指针
{
if (root!=NULL)
{
printf("%d",root ->data); //访问根结点
PreOrder(root ->LChild); //先序遍历左子树
PreOrder(root ->RChild); //先序遍历右子树
}
}

int main()
{
BiT

声明时写了CreateBiTree,调用时写了CreateBitree,C和C++是区分大小写的,统一一下吧。

CreateBiTree()函数里面应该是这么写BiTree已经是指针类型了 BiTree *T就成二级指针了

void CreateBiTree(BiTree &T) //建立二叉树
{
int ch;
scanf("%d",&ch);
if(ch=='*')
T=NULL;
else
{
T=(BiTNode *)malloc(sizeof(BiTNode));
T->data=ch;
CreateBiTree(T->LChild); //左子树
CreateBiTree(T->RChild); //右子树
}
}