标签:
#include<iostream>
using namespace std;
typedef char ELEMTYPE;
//二叉链表
typedef struct BiTNode
{
ELEMTYPE data;
struct BiTNode *lChild;
struct BiTNode *rChild;
}BiTNode,*BiTPtr;
//创建二叉树,使用前序遍历的方式输入数据
void CreateBiTree(BiTPtr *T)
{
char c;
scanf("%c",&c);
if(‘ ‘ == c)
{
*T = NULL;
}
else
{
*T = (BiTPtr)malloc(sizeof(BiTNode));
(*T)->data = c;
CreateBiTree(&(*T)->lChild);
CreateBiTree(&(*T)->rChild);
}
}
//访问二叉树的具体操作
void visit(ELEMTYPE e,int level)
{
printf("%c在第%d层\n",e,level);
}
//遍历二叉树
void preOrderTraverse(BiTPtr T,int level)
{
if(T)
{
visit(T->data,level);
preOrderTraverse(T->lChild,level+1);
preOrderTraverse(T->rChild,level+1);
}
}
void main()
{
BiTPtr p = NULL;
int level =1;
CreateBiTree(&p);
preOrderTraverse(p,level);
}
标签:
原文地址:http://www.cnblogs.com/chay/p/5808457.html