二叉树的遍历
题目描述:
本题要求给定二叉树的4种遍历。
函数接口定义:
void InorderTraversal( BinTree BT );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );
其中BinTree结构定义如下
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
ElementType Data;
BinTree Left;
BinTree Right;
};
要求4个函数分别按照访问顺序打印出结点的内容,格式为一个空格跟着一个字符。
裁判测试程序样例:
#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 );
void PreorderTraversal( BinTree BT );
void PostorderTraversal( BinTree BT );
void LevelorderTraversal( BinTree BT );
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;
}
代码:
代码(一):
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);
}
}
typedef struct QNode
{
BinTree data;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct LinkQueue
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
LinkQueue InitQueue(LinkQueue Q)
{
Q.rear = Q.front = (QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(-2);
Q.front->next = NULL;
return Q;
}
LinkQueue EnQueue(LinkQueue Q,BinTree e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p)exit(-2);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return Q;
}
LinkQueue DeQueue(LinkQueue Q)
{
QueuePtr p = Q.front->next;
Q.front->next = p->next;
if(Q.rear == p)Q.rear = Q.front;
free(p);
return Q;
}
int EmptyQueue(LinkQueue Q)
{
if(Q.rear == Q.front)
return 1;
else
return 0;
}
BinTree GetHead(LinkQueue Q)
{
QueuePtr p = Q.front->next;
return p->data;
}
void LevelorderTraversal( BinTree BT )
{
LinkQueue Q;
Q = InitQueue(Q);
if(BT)
Q = EnQueue(Q,BT);
while(EmptyQueue(Q) == 0)
{
BinTree B = GetHead(Q);
Q = DeQueue(Q);
printf(" %c",B->Data);
if(B->Left)
Q = EnQueue(Q,B->Left);
if(B->Right)
Q = EnQueue(Q,B->Right);
}
}
代码(二):
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)
{
BinTree bt[16];
int front = 0;
int rear = 0;
if(BT)
{
bt[front] = BT;
front++;
}
while(rear != front)
{
printf(" %c",bt[rear]->Data);
if(bt[rear]->Left)
{
bt[front] = bt[rear]->Left;
front++;
front %= 16;
}
if(bt[rear]->Right)
{
bt[front] = bt[rear]->Right;
front++;
front %= 16;
}
rear++;
rear %= 16;
}
}
代码分析:
先序遍历:先根后左右分支;中序遍历:先左分支后根右分支;后序遍历:先左右分支后根。这三种遍历只要掌握了递归的方法都很容易。递归的核心思想:首先,考虑如何将大的问题按照一定的方法逐步分解为很小的问题;其次,考虑问题的边界;最后,将代码写出来,如果问题很难,在纸上写一下。
层次遍历:一般借助队列来实现,在知道树的高度时,也可以用递归来实现。相比较于递归,借助对列可以更好的、更快的输出,只是需要占据一定的空间。