什么是遍历
-
单链表的遍历是指从第一个结点开始( 下标为0的结点) , 按照某种次序依次访问每一个结点。
-
二叉树的遍历是指从根结点开始,按照某种次序依次访问二叉树中的所有结点。
思考:
单链表可以有正序遍历和逆序遍历两种方式,那二叉树有几种遍历方式呢?
前序遍历
中序遍历
后序遍历
层次遍历
遍历算法的实现
#include <stdio.h>
#include <stdlib.h>
#include "BTree.h"
#include "LinkQueue.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Node
{
BTreeNode header;
char v;
};
void printf_data(BTreeNode* node)
{
if( node != NULL )
{
printf("%c", ((struct Node*)node)->v);
}
}
void pre_order_traversal(BTreeNode* root)//前序遍历
{
if( root != NULL )
{
printf("%c, ", ((struct Node*)root)->v);//取出根节点
pre_order_traversal(root->left);//前序遍历左子树
pre_order_traversal(root->right);//前序遍历右子树
}
}
void middle_order_traversal(BTreeNode* root)//中序遍历
{
if( root != NULL )
{
middle_order_traversal(root->left);
printf("%c, ", ((struct Node*)root)->v);
middle_order_traversal(root->right);
}
}
void post_order_traversal(BTreeNode* root)//后序遍历
{
if( root != NULL )
{
post_order_traversal(root->left);
post_order_traversal(root->right);
printf("%c, ", ((struct Node*)root)->v);
}
}
void level_order_traversal(BTreeNode* root)//层次遍历
{
if( root != NULL )
{
LinkQueue* queue = LinkQueue_Create();
if( queue != NULL )
{
LinkQueue_Append(queue, root);//第一层压入链表
while( LinkQueue_Length(queue) > 0 )//队列为空吗?不为空就进入循环
{
struct Node* node = (struct Node*)LinkQueue_Retrieve(queue);//得到队头节点
printf("%c, ", node->v);//访问节点
LinkQueue_Append(queue, node->header.left);//把孩子节点压入到队列中
LinkQueue_Append(queue, node->header.right);
}
}
LinkQueue_Destroy(queue);
}
}
int main(int argc, char *argv[])
{
BTree* tree = BTree_Create();
struct Node n1 = {{NULL, NULL}, 'A'};
struct Node n2 = {{NULL, NULL}, 'B'};
struct Node n3 = {{NULL, NULL}, 'C'};
struct Node n4 = {{NULL, NULL}, 'D'};
struct Node n5 = {{NULL, NULL}, 'E'};
struct Node n6 = {{NULL, NULL}, 'F'};
BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);
BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);
BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);
BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);
BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);
BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);
printf("Full Tree: \n");
BTree_Display(tree, printf_data, 4, '-');
printf("Pre Order Traversal:\n");
pre_order_traversal(BTree_Root(tree));
printf("\n");
printf("Middle Order Traversal:\n");
middle_order_traversal(BTree_Root(tree));
printf("\n");
printf("Post Order Traversal:\n");
post_order_traversal(BTree_Root(tree));
printf("\n");
printf("Level Order Traversal:\n");
level_order_traversal(BTree_Root(tree));
printf("\n");
BTree_Destroy(tree);
return 0;
}
小结
二叉树仅仅比单链表多了一个指针域,但其遍历算法的种类却增加了很多