主题:二叉树的问题
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
int k=-1;/*全局变量,为create()递归取值,通过Get()依次取得str[MAXSIZE]中的值*/
typedef struct binode
{
char data;
struct binode *lchild;
struct binode *rchild;
}Binode;
char Get()/*从磁盘文件中获取字符*/
{
char ch,str[MAXSIZE];
int i=0;
FILE *fp;
fp=fopen("b.txt","r");
if(fp==0){
printf("cannot open this file\n");
exit(0);
}
else{ch=fgetc(fp);
while(ch!=EOF){
str[i]=ch;i++;ch=fgetc(fp);
}
fclose(fp);
k++;/*每调用一次Get(),k加一*/
return(str[k]);
}
}
Binode* create()/*先序构造一颗二叉树*/
{
char ch;
Binode* t;
ch=Get();
if(ch==' ')
t=NULL;
else
{
t=(Binode *)malloc(sizeof(Binode));
t->data=ch;
t->lchild=create();
t->rchild=create();
}
return t;
}
char putcharacter(char ch)
{/*输入字符函数*/
return(putchar(ch));
}
void Preorder(Binode * t,char(* visit)(char))
{/*先序遍历二叉树*/
if(t){
visit(t->data);
Preorder(t->lchild,visit);
Preorder(t->rchild,visit);
}
}
void Inorder(Binode *t,char(* visit)(char))
{/*中序遍历二叉树*/
if(t){
Inorder(t->lchild,visit);
visit(t->data);
Inorder(t->rchild,visit);
}
}
void Postorder(Binode *t,char(* visit)(char))
{/*后序遍历二叉树*/
if(t){
Postorder(t->lchild,visit);
Postorder(t->rchild,visit);
visit(t->data);
}
}
void main()
{
char ch;
Binode* t=0;
/*以下为函数声明*/
char Get();
Binode* create();
char putcharacter(char ch);
void Preorder(Binode * t,char(* visit)(char));
void Inorder(Binode *t,char(* visit)(char));
void Postorder(Binode *t,char(* visit)(char));
printf("\nto create bintree...\n\n");
t=create();
printf("Preorder visit bintree:\n");
Preorder(t,putcharacter);
printf("\n");
printf("Inorder visit bintree:\n");
Inorder(t,putcharacter);
printf("\n");
printf("Postorder visit bintree:\n");
Postorder(t,putcharacter);
getchar();
}
这是二叉树的遍历问题
错误倒没有
可得不到自己想要的结果
要求是:
创建一棵二叉树,二叉树的节点值可以:从文件中读入
但结果是不能打开文件
哪位大侠能帮帮我
不胜感激!
#include<stdlib.h>
#define MAXSIZE 100
int k=-1;/*全局变量,为create()递归取值,通过Get()依次取得str[MAXSIZE]中的值*/
typedef struct binode
{
char data;
struct binode *lchild;
struct binode *rchild;
}Binode;
char Get()/*从磁盘文件中获取字符*/
{
char ch,str[MAXSIZE];
int i=0;
FILE *fp;
fp=fopen("b.txt","r");
if(fp==0){
printf("cannot open this file\n");
exit(0);
}
else{ch=fgetc(fp);
while(ch!=EOF){
str[i]=ch;i++;ch=fgetc(fp);
}
fclose(fp);
k++;/*每调用一次Get(),k加一*/
return(str[k]);
}
}
Binode* create()/*先序构造一颗二叉树*/
{
char ch;
Binode* t;
ch=Get();
if(ch==' ')
t=NULL;
else
{
t=(Binode *)malloc(sizeof(Binode));
t->data=ch;
t->lchild=create();
t->rchild=create();
}
return t;
}
char putcharacter(char ch)
{/*输入字符函数*/
return(putchar(ch));
}
void Preorder(Binode * t,char(* visit)(char))
{/*先序遍历二叉树*/
if(t){
visit(t->data);
Preorder(t->lchild,visit);
Preorder(t->rchild,visit);
}
}
void Inorder(Binode *t,char(* visit)(char))
{/*中序遍历二叉树*/
if(t){
Inorder(t->lchild,visit);
visit(t->data);
Inorder(t->rchild,visit);
}
}
void Postorder(Binode *t,char(* visit)(char))
{/*后序遍历二叉树*/
if(t){
Postorder(t->lchild,visit);
Postorder(t->rchild,visit);
visit(t->data);
}
}
void main()
{
char ch;
Binode* t=0;
/*以下为函数声明*/
char Get();
Binode* create();
char putcharacter(char ch);
void Preorder(Binode * t,char(* visit)(char));
void Inorder(Binode *t,char(* visit)(char));
void Postorder(Binode *t,char(* visit)(char));
printf("\nto create bintree...\n\n");
t=create();
printf("Preorder visit bintree:\n");
Preorder(t,putcharacter);
printf("\n");
printf("Inorder visit bintree:\n");
Inorder(t,putcharacter);
printf("\n");
printf("Postorder visit bintree:\n");
Postorder(t,putcharacter);
getchar();
}
这是二叉树的遍历问题
错误倒没有
可得不到自己想要的结果
要求是:
创建一棵二叉树,二叉树的节点值可以:从文件中读入
但结果是不能打开文件
哪位大侠能帮帮我
不胜感激!