一、Projects.h文件
该文件主要书写必要的声明、结构体和函数声明
#define ERROR NULL;
typedef int ElementType;
typedef struct BTNode *BinTree;
struct BTNode{
int Data;
BinTree Left;
BinTree Right;
};
BinTree Insert(BinTree BST,ElementType X);
BinTree Output(BinTree BST);
二、Projects.c文件
该文件连接Projects.h,主要书写函数的具体内容
#include<stdio.h>
#include "Projects.h" //连接Projects.h文件
BinTree Insert(BinTree BST,ElementType X)
{
if(!BST){ //若原树为空
BST=(BinTree)malloc(sizeof(struct BTNode));
BST->Data=X;
BST->Left=BST->Right=NULL;
}else{
if(X<BST->Data){
BST->Left=Insert(BST->Left,X);
}else{
BST->Right=Insert(BST->Right,X);
}
}
return BST;
}
BinTree Output(BinTree BST)
{
if(BST){ //若原树不为空
printf("%d ",BST->Data);
Output(BST->Left);
Output(BST->Right);
}
return BST;
}
三、ProjectsMain.c文件
该文件连接Projects.c,主要书写main函数
#include<stdio.h>
#include<stdlib.h>
#include “Projects.c” //连接Projects.c文件
int main()
{
int X;
scanf("%d",&X);
BinTree BST;
Insert(BST,X);
return 0;
}
(三个文件要放在一个文件夹里)