求一份C++程序代码!

来源:百度知道 编辑:UC知道 时间:2024/06/14 08:35:35
二叉树实验
要求:实现二叉树的相关操作,大致包括如下内容:1 建树 2 各种遍历:前序 中序 后序 层次遍历 3 搜索 4 插入 5 删除 6 显示等。
谢谢各位啊!!

//头文件
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
//预定义宏常量
#define OK 1
#define ERROR -1
#define ENDFLAG '#'
typedef char TelemType;
typedef int status;
//二叉树的存储结构
typedef struct BiTNode{
TelemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//全局变量,表示叶子个数
int m=0;
//二叉树的创建
status CreateBiTree(BiTree *T)
{// 先序创建
TelemType ch;
scanf("%c",&ch);
if(ch==ENDFLAG) *T=NULL;
else {
if(!(*T=(BiTNode *)malloc(sizeof(BiTNode))))
{
printf("\nOut of space.");
getch();
exit(0);
}
(*T)->data=ch; //生成根结点
CreateBiTree(&((*T)->lchild));//左子树
CreateBiTree(&((*T)->rchild));//右子树
}
return OK;
}
//先序遍历
status PreOrderTraverse(BiTree T)
{
if(T)
{
printf