//这个题目挺有意思的,很喜欢,你看看我这个咋样啊?
#include
#include
typedef char ElemType ;
typedef struct node
{
ElemType data ;
struct node *lchild ;
struct node *rchild ;
}BTree,*pBTree ;
//先序创建树
void CreateBTree(pBTree *T)
//此处参数应该用指针的指针,应给它要改变指向二叉树根的那个指针
{
char ch ;
ch=getchar();
getchar(); //得到回车按那个字符
if(ch ==' ') //输入空字符时要打空格
{
(*T) = NULL ;
return ;
}
else
{
if( !( (*T) = (pBTree) malloc(sizeof(BTree)) ) ) return ;
(*T)->data = ch ;
CreateBTree( &(*T)->lchild );
CreateBTree( &(*T)->rchild );
}
}
void BTreePrint(BTree *Tr,int n)
//逆时针旋转90°打印二叉树,n为缩进层数,初始值为0
{
int i;
if(Tr == NULL) return;
BTreePrint(Tr->rchild,n+1);
for(i = 0;i
if(n >= 0)
{
printf("--");
printf("%c\n",Tr->data);
}
BTreePrint(Tr->lchild,n+1);
}
void main()
{
pBTree bTree ;
CreateBTree(&bTree);
BTreePrint(bTree,0);
}
输入举例:建立以A为根B、C分别为左右子树的二叉树!输入格式为:
A 回车!
B 回车!
空格 回车!
空格 回车!
C 回车!
空格 回车!
空格 回车!