10週目--データ構造--ツリー遍歴の再帰アルゴリズム
2347 ワード
/*10週目--データ構造--ツリーループの再帰アルゴリズム
*Copyright(c)2015煙台大学コンピュータと制御工学院*All right reserved.*ファイル名:tree.cpp*writer:羅海員*date:2015年11月18日*バージョン:V 1.0.1*オペレーティングシステム:windows 8*実行環境:VC 6.0*問題説明:*プログラム出力:*/
*Copyright(c)2015煙台大学コンピュータと制御工学院*All right reserved.*ファイル名:tree.cpp*writer:羅海員*date:2015年11月18日*バージョン:V 1.0.1*オペレーティングシステム:windows 8*実行環境:VC 6.0*問題説明:*プログラム出力:*/
#include <stdio.h>
#define MaxSize 100
typedef char ElemType;
typedef struct node
{
ElemType data; //
struct node *lchild; //
struct node *rchild; //
} BTNode;
void CreateBTNode(BTNode *&b,char *str); // str
BTNode *FindNode(BTNode *b,ElemType x); // data x
BTNode *LchildNode(BTNode *p); // *p
BTNode *RchildNode(BTNode *p); // *p
int BTNodeDepth(BTNode *b); // b
void DispBTNode(BTNode *b); //
void DestroyBTNode(BTNode *&b); //
void PreOrder(BTNode *b);
void InOrder(BTNode *b);
void PostOrder(BTNode *b);
void PreOrder(BTNode *b) //
{
if (b!=NULL)
{
printf("%c ",b->data); //
PreOrder(b->lchild); //
PreOrder(b->rchild); //
}
}
void InOrder(BTNode *b) //
{
if (b!=NULL)
{
InOrder(b->lchild); //
printf("%c ",b->data); //
InOrder(b->rchild); //
}
}
void PostOrder(BTNode *b) //
{
if (b!=NULL)
{
PostOrder(b->lchild); //
PostOrder(b->rchild); //
printf("%c ",b->data); //
}
}
int main()
{
BTNode *b;
CreateBTNode(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf(" b:");
DispBTNode(b);
printf("
");
printf(" :
");
PreOrder(b);
printf("
");
printf(" :
");
InOrder(b);
printf("
");
printf(" :
");
PostOrder(b);
printf("
");
DestroyBTNode(b);
return 0;
}