#include
#include
#define CHAR 1 /*字符型1 整型 0 */
#if CHAR
typedef char TElemType;
TElemType Nil=' ';/*字符型以空格符为空*/
#define form "%c" /*输入输出格式为%d*/
#else
typedef int TElemType;
TElemType Nil=0; /*整型以0为空*/
#define form "%d" /*输入输出格式为%d*/
#endif
typedef struct node /*结构类型定义*/
{
TElemType data;
struct node *left;
struct node *right;
}BiTNode,*BiTree;
BiTNode *initBiTree(BiTNode *bt)
{
bt=NULL;
return bt;
}
BiTNode *CreateBiTree(BiTNode *bt)
{/*按先序次序输入二叉树中结点的值(可为字符型或整型)*/
TElemType ch;
scanf(form,&ch);
if(ch==Nil)
bt=NULL;
else
{
bt=(BiTNode*)malloc(sizeof(BiTNode));/*生成根结点*/
if(!bt) exit(0);
bt->data=ch;
bt->left=CreateBiTree(bt->left);/*构造左子树*/
bt->right=CreateBiTree(bt->right);/*构造右子树*/
}
return bt;
}
void PrintTree(BiTNode *bt,int i)
{/*输出二叉树*/
if(bt!=NULL)
{
PrintTree(bt->right,i+5);/*递归调用右子树*/
#if CHAR
if(bt->data!=Nil)
{
printf("%*c\n",i,bt->data);/*i不输出,用来调节输出*/
}
#else
if(bt->data!=Nil)
{
printf("%*d\n",i,bt->data);
}
#endif
PrintTree(bt->left,i+5);
i=i-5;
}
}
int main()
{
int i;
BiTree bt;
bt=initBiTree(bt);
#if CHAR
printf("请先序输入二叉树(如:ab三个空格 a为根节点,b为左子树的二叉树)\n");
#else
printf("请先序输入二叉树(如:1 2 0 0 0 1为根节点,2为左子树的二叉树)\n");
#endif
bt=CreateBiTree(bt);
printf("输入建立的二叉树!!!\n");
PrintTree(bt,5);
}