[c]代码库
#include <stdio.h>
#include <stdlib.h>
typedef char ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
BinTree CreatBinTree(); /* 实现细节忽略 */
void InorderTraversal( BinTree BT )
{
if(BT)
{
InorderTraversal(BT->Left);
printf(" %c",BT->Data);
InorderTraversal(BT->Right);
}
}
void PreorderTraversal( BinTree BT )
{
if(BT)
{
printf(" %c",BT->Data);
PreorderTraversal(BT->Left);
PreorderTraversal(BT->Right);
}
}
void PostorderTraversal( BinTree BT )
{
if(BT)
{
PostorderTraversal(BT->Left);
PostorderTraversal(BT->Right);
printf(" %c",BT->Data);
}
}
void LevelorderTraversal( BinTree BT )
{
if(BT)
{
int i,j;
struct TNode *q[200],*p; /*q[200]用于模拟队列,存储入队的结点*/
i=0;
q[i]=BT;
j=1;/*i为队首位置,j为队尾位置*/
while(i!=j)
{
p=q[i];
printf(" %c",p->Data); /*访问队首元素*/
if(p->Left)
q[j++]=p->Left;/*若队首元素左链域不为空,则将其入队列*/
if(p->Right)
q[j++]=p->Right;/*若队首元素右链域不为空,则将其入队列*/
i++; /*将队首移到下一个位置*/
}
}
}
int main()
{
BinTree BT = CreatBinTree();
printf("Inorder:"); InorderTraversal(BT); printf("\n");
printf("Preorder:"); PreorderTraversal(BT); printf("\n");
printf("Postorder:"); PostorderTraversal(BT); printf("\n");
printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
return 0;
}
初级程序员
by: 追梦不息 发表于:2017-11-07 16:36:43 顶(0) | 踩(0) 回复
很有用,谢谢
网友回复
回复芙蓉妹妹 : *-*
顶(0) 踩(0) 2017-11-13 08:31:57
回复评论