第10週目プロジェクト2——二叉木遍歴の再帰アルゴリズム
2204 ワード
問題およびコード:
ヘッダファイル:
ソースファイル:
実行結果:
/*
*Copyright(c++)2015,
*All rights reserved.
* :CPP1.cpp
* :
* :2015 11 02
* :v1.0
*
* : 、 、 ,
”A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))” 。
*/
ヘッダファイル:
#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED
#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); //
#endif // BTREE_H_INCLUDED
ソースファイル:
#include <stdio.h>
#include "btree.h"
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;
}
実行結果: