实现了二叉树的前序遍历,中序遍历,后序遍历,逐层遍历。
其中逐层遍历,使用了TreeQueue.h里面定义的队列Queue。
因此,程序由两个文件组成,BinaryTree.c和TreeQueue.h。
程序输出:
$ ./BinaryTree
*****Tree*****
+
* /
a b c d
**************
PreOrder:
+ * a b / c d
InOrder:
a * b + c / d
PostOrder:
a b * c d / +
LevelOrder:
+ * / a b c d
BinaryTree.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TreeQueue.h"
static void visit(struct tree_node *n);
static struct tree_node * create_node(char *data);
static void pre_order(struct tree_node *n);
static void in_order(struct tree_node *n);
static void post_order(struct tree_node *n);
static void level_order(struct tree_node *n);
static void visit(struct tree_node *n)
{
if(NULL == n || NULL == n->data)
return;
printf("%s ", n-&