c 언어 이 진 트 리 의 구축


#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
",i,bt->data);/*i , */
}
#else
if(bt->data!=Nil)
{
printf("%*d
",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 )
");
#else
printf(" ( :1 2 0 0 0 1 ,2 )
");
#endif
bt=CreateBiTree(bt);
printf(" !!!
");
PrintTree(bt,5);
}

좋은 웹페이지 즐겨찾기