二叉树遍历

#include
#include

typedef struct BiNode
{
char data;
struct BiNode *lchild, *rchild;
}BiNode, *Btree;

/*Ç°Dò±éàú*/

void PreOrderTraverse(Btree T)
{
if (T == NULL)
return;
printf ("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}

/*ÖDDò*/

void InOrderTraverse(Btree T)
{
if (T == NULL)
return;
InOrderTraverse(T->lchild);
printf ("%c", T->data);
InOrderTraverse(T->rchild);
}

/*oóDò*/

void PostOrderTraverse(Btree T)
{
if (T == NULL)
return;
PostOrderTraverse(T->lchild);
PostOrderTraverse(T->rchild);
printf ("%c", T->data);
}

void CreateBtree(Btree *T)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
*T = NULL;
else
{
*T = (Btree)malloc(sizeof(BiNode));
(*T)->data = ch;
CreateBtree(&(*T)->lchild);
CreateBtree(&(*T)->rchild);
}
}

int tree_level(Btree T)   //2ãêy
{
int l_level;
int r_level;
if (T == NULL)
return 0;
else
{
l_level = tree_level(T->lchild);
r_level = tree_level(T->rchild);
return l_level>r_level? l_level + 1 : r_level + 1;
}
}

int main()
{
Btree t;
CreateBtree(&t);
printf ("Ç°Dò:");
PreOrderTraverse(t);
printf ("ÖDDò:");
InOrderTraverse(t);
printf ("oóDò:");
PostOrderTraverse(t);
printf ("\n");
printf ("%d\n", tree_level(t));
return 0;
}

• 博文量
55
• 访问量
78210