#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct btnode
{
char val ;
struct btnode *lchild ,*rchild ;
}BTNODE, *BiTree;
// 二叉链表--排序树
void Creat( BiTree &bt )
{
char x;
scanf("%c", &x );
if( x == '#' )
{
bt = NULL;
}
else
{
bt = ( BiTree )malloc( sizeof( BTNODE ) );
bt ->val = x;
Creat( bt ->lchild );
Creat( bt ->rchild );
}
}
void visit (char val ,int level)
{
printf("%c是%d层\n" ,val ,level);
}
void preorder (BTNODE* node ,int level)
{
BTNODE* p ;
p=node;
if(p)
{
visit(p->val , level );
preorder(p->lchild ,level+1);
preorder(p->rchild ,level+1);
}
}
int main ()
{
int level=0 ;
BTNODE *head = NULL;
Creat( head );
preorder(head ,level);
return 0;
}
|