首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于二叉树的遍历,

2012-04-15 
关于二叉树的遍历,求助!用递归来对二叉树进行排序代码如下:C/C++ code/*BinaryTree.c*/#includestdio.h#

关于二叉树的遍历,求助!
用递归来对二叉树进行排序代码如下:

C/C++ code
/*BinaryTree.c*/#include<stdio.h>#include<stdlib.h>#include"BinaryTree.h"struct BinaryTree *initBinaryTree(BTree T){    char ch =NULL;    scanf("%c",&ch);    if(' '==ch) T=NULL;    else{        if(!(T=(struct BinaryTree *)malloc(Len))) exit(1);        T->ch=ch;        initBinaryTree(T->leftchild);        initBinaryTree(T->rightchild);    }    return T;}void Visit_Tree_RootFirst(BTree T){    BTree p;    if(NULL == T)    {        printf("\nCannot visit this Tree,because of empty!\n");        exit(1);    }    p=T;    printf(" %c ",p->ch);    Visit_Tree_RootFirst(p->leftchild);    Visit_Tree_RootFirst(p->rightchild);}void Visit_Tree_RootSecond(BTree T){    BTree p;    if(NULL ==  T)    {        printf("\nCannot visit this Tree,because of empty!\n");        exit(1);    }    p=T;    Visit_Tree_RootFirst(p-leftchild);    printf(" %c ",p->ch);    Visit_Tree_RootFirst(p->rightchild);}void Visit_Tree_RootLast(BTree T){    BTree p;    if(NULL == T)    {        printf("\nCannot visit this Tree, because of empty!\n");        exit(1);    }    p=T;    Visit_Tree_RootFirst(p->leftchild);    Visit_Tree_RootFirst(p->rightchild);    printf("% c ",p->ch);}

C/C++ code
/* BinaryTree.h*/#ifndef BINARYTREE_H#define BINARYTREE_H#include<stdio.h>#include<stdlib.h>#define  Len sizeof(struct BinaryTree)typedef struct BinaryTree{    char ch;    struct BinaryTree *leftchild;    struct BinaryTree *rightchild;}*BTree;struct BinaryTree *initBinaryTree(BTree T);void Visit_Tree_RootFirst(BTree T);void Visit_Tree_RootSecond(BTree T;void Visit_Tree_RootLast(BTree T);#endif

C/C++ code
/*main.c*/#include<stdio.h>#include<stdlib.h>#include"BinaryTree.h"int main(){    BTree head =NULL;    head =initBinaryTree(head);    printf("Visit the tree by the root first!\n");    Visit_Tree_RootFirst(head);    printf("Visit the tree by the root second!\n");    Visit_Tree_RootSecond(head);    printf("Visit the tree by the root last!\n");    Visit_Tree_RootLast(head);    return 0;}

makefile文件如下
Assembly code
main: main.o BinaryTree.o    gcc -g -Wall -c main main.o BinaryTree.omain.o: main.c BinaryTree.h    gcc -g -Wall -c main.c -o main.oBinaryTree.o: BinaryTree.c BinaryTree.h    gcc -g -Wall -c BinaryTree.c -o BinaryTree.o

但是在最后的编译中出现了错误阿:
Assembly code
[zhangjie@localhost BinaryTree]$ makegcc -g -Wall -c main.c -o main.omain.c:6:1: error: expected ‘;’, ‘,’ or ‘)’ before ‘{’ tokenmake: *** [main.o] Error 1

最烦这种错误,一点头绪都没有。。希望各位帮忙看看,错在那了,谢谢拉。没有分可以送了,十分抱歉阿。

[解决办法]
找到些打字错误
C/C++ code
/*BinaryTree.c*/#include<stdio.h>#include<stdlib.h>#include"BinaryTree.h"struct BinaryTree *initBinaryTree(BTree T){    char ch =NULL;    scanf("%c",&ch);    if(' '==ch) T=NULL;    else{        if(!(T=(struct BinaryTree *)malloc(Len))) exit(1);        T->ch=ch;        initBinaryTree(T->leftchild);        initBinaryTree(T->rightchild);    }    return T;}void Visit_Tree_RootFirst(BTree T){    BTree p;    if(NULL == T)    {        printf("\nCannot visit this Tree,because of empty!\n");        exit(1);    }    p=T;    printf(" %c ",p->ch);    Visit_Tree_RootFirst(p->leftchild);    Visit_Tree_RootFirst(p->rightchild);}void Visit_Tree_RootSecond(BTree T){    BTree p;    if(NULL ==  T)    {        printf("\nCannot visit this Tree,because of empty!\n");        exit(1);    }    p=T;    Visit_Tree_RootFirst(p->leftchild);    printf(" %c ",p->ch);    Visit_Tree_RootFirst(p->rightchild);}void Visit_Tree_RootLast(BTree T){    BTree p;    if(NULL == T)    {        printf("\nCannot visit this Tree, because of empty!\n");        exit(1);    }    p=T;    Visit_Tree_RootFirst(p->leftchild);    Visit_Tree_RootFirst(p->rightchild);    printf("% c ",p->ch);} 


[解决办法]

C/C++ code
/* BinaryTree.h*/#ifndef BINARYTREE_H#define BINARYTREE_H#include<stdio.h>#include<stdlib.h>#define  Len sizeof(struct BinaryTree)typedef struct BinaryTree{    char ch;    struct BinaryTree *leftchild;    struct BinaryTree *rightchild;}*BTree;struct BinaryTree *initBinaryTree(BTree T);void Visit_Tree_RootFirst(BTree T);void Visit_Tree_RootSecond(BTree T;//少了个括号void Visit_Tree_RootLast(BTree T);#endif
[解决办法]
还有scanf()函数不能输入空格。 把空格替换成# 或者其他特殊字符,或者改用getchar函数。
另外,Travel函数中不能用exit()函数。
C/C++ code
void Visit_Tree_RootFirst(BTree T){    BTree p;    if(NULL == T)    {        //printf("\nCannot visit this Tree,because of empty!\n");        //exit(1);        return;    }    //p=T;    //printf(" %c ",p->ch);    printf("%c ",T->ch);    Visit_Tree_RootFirst(T->leftchild);    Visit_Tree_RootFirst(T->rightchild);} 

热点排行