实现二叉树的各种遍历算法
头文件+函数
#include <stdio.h>
#include <malloc.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef char ElemType;
typedef struct node
{
ElemType data;
struct node *lchild;
struct node *rchild;
}BTNode;
/*-------------由括号表示串str创建二叉链b-----------------*/
static void create_btree(BTNode *&b, char *str)
{
BTNode *p;
BTNode *St[MAX_SIZE];
int k;
int j = 0;
int top = -1;
char ch;
b = NULL;
ch = str[j];
while(ch != '\0')
{
switch(ch)
{
case '(':
top++;
St[top] = p;
k = 1;
break;
case ')':
top--;
break;
case ',':
k = 2;
break;
default:
p = (BTNode *)malloc(sizeof(BTNode));
p->data = ch;
p->lchild = p->rchild = NULL;
if(b == NULL)
b = p;
else
{
switch(k)
{
case 1:
St[top]->lchild = p;
break;
case 2:
St[top]->rchild = p;
break;
}
}
break;
}
j++;
ch = str[j];
}
}
/*--------------------------以括号表示法输出二叉树b----------------------*/
// "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"
static void disp_btree(BTNode *b)
{
if(b != NULL)
{
printf("%c", b->data);
if(b->lchild != NULL || b->rchild != NULL)
{
printf("(");
disp_btree(b->lchild);
if(b->rchild != NULL)
printf(",");
disp_btree(b->rchild);
printf(")");
}
}
}
/*--------------------------释放二叉树b的所有结点----------------------*/
static void destroy_btree(BTNode *&b)
{
if(b != NULL)
{
destroy_btree(b->lchild);
destroy_btree(b->rchild);
free(b);
}
}
/*--------------------------二叉树b的层次遍历算法----------------------*/
static void trav_level(BTNode *b)
{
BTNode *que[MAX_SIZE];
int que_front, que_rear;
que_front = que_rear = 0;
if(b != NULL)
printf("%c ", b->data);
que_rear++;
que[que_rear] = b;
while(que_rear != que_front)
{
que_front = (que_front + 1) % MAX_SIZE;
b = que[que_front];
if(b->lchild != NULL)
{
printf("%c ", b->lchild->data);
que_rear = (que_rear + 1) % MAX_SIZE;
que[que_rear] = b->lchild;
}
if(b->rchild != NULL)
{
printf("%c ", b->rchild->data);
que_rear = (que_rear + 1) % MAX_SIZE;
que[que_rear] = b->rchild;
}
}
printf("\n");
}
/*--------------------------二叉树b的先序遍历递归算法----------------------*/
static void pre_order(BTNode *b)
{
if(b == NULL)
return;
printf("%c ", b->data);
pre_order(b->lchild);
pre_order(b->rchild);
}
/*--------------------------二叉树b的先序遍历非递归算法----------------------*/
static void pre_order1(BTNode *b)
{
BTNode *p;
BTNode *st[MAX_SIZE];
int top = -1;
if(b == NULL)
return;
top++;
st[top] = b;
while(top > -1)
{
p = st[top];
top--;
printf("%c ", p->data);
if(p->rchild != NULL)
{
top++;
st[top] = p->rchild;
}
if(p->lchild != NULL)
{
top++;
st[top] = p->lchild;
}
}
printf("\n");
}
/*--------------------------二叉树b的中序遍历递归算法----------------------*/
static void in_order(BTNode *b)
{
if(b == NULL)
return;
in_order(b->lchild);
printf("%c ", b->data);
in_order(b->rchild);
}
/*--------------------------二叉树b的中序遍历非递归算法----------------------*/
// A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))
static void in_order1(BTNode *b)
{
BTNode *p;
BTNode *st[MAX_SIZE];
int top = -1;
if(b == NULL)
return;
p = b;
while(top > -1 || p != NULL)
{
while(p != NULL)
{
top++;
st[top] = p;
p = p->lchild;
}
if(top > -1)
{
p = st[top];
top--;
printf("%c ", p->data);
p = p->rchild;
}
}
printf("\n");
}
/*--------------------------二叉树b的后序遍历递归算法----------------------*/
static void post_order(BTNode *b)
{
if(b == NULL)
return;
post_order(b->lchild);
post_order(b->rchild);
printf("%c ", b->data);
}
/*--------------------------二叉树b的后序遍历非递归算法----------------------*/
static void post_order1(BTNode *b)
{
int top = -1;
BTNode *st[MAX_SIZE];
BTNode *p;
bool flag;
if(b == NULL)
return;
do
{
while(b != NULL)
{
top++;
st[top] = b;
b = b->lchild;
}
p = NULL;
flag = true;
while(top != -1 && flag)
{
b = st[top];
if(b->rchild == p)
{
printf("%c ", b->data);
top--;
p = b;
}
else
{
b = b->rchild;
flag = false;
}
}
}while(top != -1);
printf("\n");
}
主函数
int main(int argc, char *argv[])
{
BTNode *b;
create_btree(b, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
printf("二叉树b:");
disp_btree(b);
printf("\n");
printf("先序遍历序列:\n");
printf(" 递归算法:");
pre_order(b);
printf("\n");
printf(" 非递归算法:");
pre_order1(b);
printf("\n");
printf("中序遍历序列:\n");
printf(" 递归算法:");
in_order(b);
printf("\n");
printf(" 非递归算法:");
in_order1(b);
printf("\n");
printf("后序遍历序列:\n");
printf(" 递归算法:");
post_order(b);
printf("\n");
printf(" 非递归算法:");
post_order1(b);
printf("\n");
printf("层次遍历序列:");
trav_level(b);
destroy_btree(b);
return 0;
}
运行结果: