#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*结构体定义*/
typedef struct tree
{
char data; //数据域
struct tree *lchild; //左孩子
struct tree *rchild; //右孩子
struct tree *next; //用于构造队列
}bintree;
//二叉树的建立
bintree *create()
{
char ch;
bintree * t;
ch=getche(); //依次读入字符
if(ch==' ') t=NULL; // 表示空结点
else
{
t=(bintree *)malloc(sizeof(bintree)); //非空则构造新结点
t->data=ch; //新结点数据域即为读入字符
t->next=NULL; //新结点next域暂时不用,赋空
t->lchild=create(); //建立左子树
t->rchild=create(); //建立右子树
}
return(t);
}
//凹入表示
void indentation(bintree *root,int n)
/*凹入表示时,遍历顺序完全同于先序遍历,故每访问一个结点,将其按一定的格式输出格式为每行一个结点,,第k层次结点前输出K-1个空格,空格数记为n。采用递归算法*/
{
bintree *t=root;
int i;
if(NULL!=t)
{
for(i=0;i<n;++i)
printf(" "); //输出n个空格
printf("%c\n",t->data); //访问根结点
n++; //层次加深一层,n也相应自增一
indentation(t->lchild,n); //先序遍历左子树
indentation(t->rchild,n); //先序遍历右子树
}
}
//先序遍历
void preorder(bintree *root)
{
bintree *t=root;
if(NULL!=t)
{
printf("%c ",t->data); //访问根结点
preorder(t->lchild); //先序遍历左子树
preorder(t->rchild); //先序遍历右子树
}
}