关于二叉树的遍历,求助!
用递归来对二叉树进行排序代码如下:
/*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);}/* 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/*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;}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
[zhangjie@localhost BinaryTree]$ makegcc -g -Wall -c main.c -o main.omain.c:6:1: error: expected ‘;’, ‘,’ or ‘)’ before ‘{’ tokenmake: *** [main.o] Error 1/*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);}
[解决办法]
/* 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()函数。
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);}