构造二叉树函数及遍历

来源:百度知道 编辑:UC知道 时间:2024/09/25 22:17:45
//主要是验证下采用递推的算法对二叉树进行遍历:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//二叉树的表示:
#define error -1
#define ok 1

typedef struct BiTreeNode
{
//结点元素的数值:
char data;
struct BiTreeNode *lchild,*rchild;

}Bnode,*Btree;
//初始化一个二叉树,
int creatree(Btree T);
//对二叉树树结点的操作函数:
int print(char e);
//遍历二叉树结点:先序遍历法
int preoder(Btree T,int(*vist)(char));
//遍历二叉树结点:中序遍历法
int inorder(Btree T,int(*vist)(char));
//遍历二叉树结点:后序遍历法
int postorder(Btree T,int(*vist)(char));
//主函数:
int main(int argc,char *argv[])
{
Btree R;int i,j;R=(Btree)malloc(sizeof(Bnode));
i=creatree(R);
printf("if i=1;表示初始化成功:%d\n",i);
j=preoder(R,print);
printf("if j=1;表示初始化成功:%\n",j);
printf("\n");
inorder(R,print);
printf("\n");
postorder(R,print);
return 0;

程序写多了,以下那些是多余的,定义函数,你下面并没有具体程序。
//初始化一个二叉树,
int creatree(Btree T);
//对二叉树树结点的操作函数:
int print(char e);
//遍历二叉树结点:先序遍历法
int preoder(Btree T,int(*vist)(char));
//遍历二叉树结点:中序遍历法
int inorder(Btree T,int(*vist)(char));
//遍历二叉树结点:后序遍历法
int postorder(Btree T,int(*vist)(char));

而且注释部分是用 /* */ 来实现的,要不然系统认为你的注释也是程序的部分。

//初始化一个二叉树,
int creatree(Btree T);
//对二叉树树结点的操作函数:
int print(char e);
//遍历二叉树结点:先序遍历法
int preoder(Btree T,int(*vist)(char));
//遍历二叉树结点:中序遍历法
int inorder(Btree T,int(*vist)(char));
//遍历二叉树结点:后序遍历法
int postorder(Btree T,int(*vist)(char));