一、实验目的
掌握二叉链表学会建立二叉树以及对递归和非递归的掌握。
二、实验题目
树与二叉树
三、问题分析
1.数据分析
数据应保证为合理范围内的数据类型
2.运算分析
用二叉链表作为存储结构,建立二叉树,声明遍历函数,最后输出遍历结果。
3.主要算法分析
用二叉链表作为存储结构,定义函数createBiTree(BiTree* bi_tree)建立二叉树,声明遍历函数
4.测试数据分析
使用题目给的数据可以很好的诠释实验要求。
四、源程序
递归方法
#include<stdio.h>
#include<stdlib.h>
#define BITREE_NODE_TYPE_ELEMENT char
typedef struct bi_tree_node
{
BITREE_NODE_TYPE_ELEMENT data;
struct bi_tree_node* LChild;
struct bi_tree_node* RChild;
}BiTree_Node, * BiTree;
void postOrder(BiTree root);
void inOrder(BiTree root);
void preOrder(BiTree root);
void createBiTree(BiTree* bi_tree);
void visit(BITREE_NODE_TYPE_ELEMENT data);
int main()
{
BiTree bi_tree = NULL;
puts("请按先序序列输入一颗二叉树的结点数据,以'*'来代表空值:");
createBiTree(&bi_tree);
printf("\n先序序列:");
preOrder(bi_tree);
printf("\n中序序列:");
inOrder(bi_tree);
printf("\n后序序列:");
postOrder(bi_tree);
putchar('\n');
return 0;
}
void visit(BITREE_NODE_TYPE_ELEMENT data)
{
putchar(data);
}
void createBiTree(BiTree* bi_tree)
{
char ch;
ch = getchar();
if (ch == '*')
*bi_tree = NULL;
else
{
*bi_tree = (BiTree)malloc(sizeof(BiTree_Node));
(*bi_tree)->data = ch;
createBiTree(&((*bi_tree)->LChild));
createBiTree(&((*bi_tree)->RChild));
}
}
void preOrder(BiTree root)
{
if (root != NULL)
{
visit(root->data);
preOrder(root->LChild);
preOrder(root->RChild);
}
}
void inOrder(BiTree root)
{
if (root != NULL)
{
inOrder(root->LChild);
visit(root->data);
inOrder(root->RChild);
}
}
void postOrder(BiTree root)
{
if (root != NULL)
{
postOrder(root->LChild);
postOrder(root->RChild);
visit(root->data);
}
}
非递归方法
#include <stdio.h>
#include <stdlib.h>
#define BITREE_NODE_TYPE_ELEMENT char
#define TRUE 1
#define FALSE 0
#define MAX_SIZE 50
#define StackElementType BiTree
typedef struct bi_tree_node
{
BITREE_NODE_TYPE_ELEMENT data;
struct bi_tree_node *LChild;
struct bi_tree_node *RChild;
} BiTree_Node, *BiTree;
typedef struct
{
StackElementType elem[MAX_SIZE];
int top;
} SeqStack;
void createBiTree(BiTree *bi_tree);
void postOrder(BiTree root);
void inOrder(BiTree root);
void preOrder(BiTree root);
void visit(BITREE_NODE_TYPE_ELEMENT data);
void InitStack(SeqStack *S);
int IsEmpty(SeqStack S);
int IsFull(SeqStack S);
int Push(SeqStack *S, StackElementType x);
int Pop(SeqStack *S, StackElementType *x);
int GetTop(SeqStack S, StackElementType *x);
int main()
{
BiTree bi_tree = NULL;
puts("请按先序序列输入一颗二叉树的结点数据,以'*'来代表空值:");
createBiTree(&bi_tree);
printf("\n先序序列:");
preOrder(bi_tree);
printf("\n中序序列:");
inOrder(bi_tree);
printf("\n后序序列:");
postOrder(bi_tree);
putchar('\n');
system("pause");
return 0;
}
void visit(BITREE_NODE_TYPE_ELEMENT data)
{
putchar(data);
}
void createBiTree(BiTree *bi_tree)
{
char ch;
ch = getchar();
if (ch == '*')
*bi_tree = NULL;
else
{
*bi_tree = (BiTree)malloc(sizeof(BiTree_Node));
(*bi_tree)->data = ch;
createBiTree(&((*bi_tree)->LChild));
createBiTree(&((*bi_tree)->RChild));
}
}
void preOrder(BiTree root)
{
SeqStack s;
BiTree temp_tree = root;
InitStack(&s);
while (temp_tree != NULL || IsEmpty(s) != TRUE)
{
while (temp_tree != NULL)
{
visit(temp_tree->data);
if (IsFull(s) == TRUE)
return;
Push(&s, temp_tree);
temp_tree = temp_tree->LChild;
}
if (IsEmpty(s) != TRUE)
{
Pop(&s, &temp_tree);
temp_tree = temp_tree->RChild;
}
}
}
void inOrder(BiTree root)
{
SeqStack s;
BiTree temp_tree = root;
InitStack(&s);
while (temp_tree != NULL || IsEmpty(s) != TRUE)
{
while (temp_tree != NULL)
{
if (IsFull(s) == TRUE)
return;
Push(&s, temp_tree);
temp_tree = temp_tree->LChild;
}
if (IsEmpty(s) != TRUE)
{
Pop(&s, &temp_tree);
visit(temp_tree->data);
temp_tree = temp_tree->RChild;
}
}
}
void postOrder(BiTree root)
{
SeqStack s;
BiTree temp_tree = root, flag_tree = NULL;
InitStack(&s);
while (temp_tree != NULL || IsEmpty(s) != TRUE)
{
while (temp_tree != NULL)
{
if (IsFull(s) == TRUE)
return;
Push(&s, temp_tree);
temp_tree = temp_tree->LChild;
}
if (IsEmpty(s) != TRUE)
{
GetTop(s, &temp_tree);
if (temp_tree->RChild == NULL || temp_tree->RChild == flag_tree)
{
Pop(&s, &temp_tree);
visit(temp_tree->data);
flag_tree = temp_tree;
temp_tree = NULL;
}
else
temp_tree = temp_tree->RChild;
}
}
}
void InitStack(SeqStack *S)
{
S->top = -1;
}
int IsEmpty(SeqStack S)
{
return (S.top == -1 ? TRUE : FALSE);
}
int IsFull(SeqStack S)
{
return (S.top == MAX_SIZE - 1 ? TRUE : FALSE);
}
int Push(SeqStack *S, StackElementType x)
{
if (S->top == MAX_SIZE - 1)
return (FALSE);
S->top++;
S->elem[S->top] = x;
return (TRUE);
}
int Pop(SeqStack *S, StackElementType *x)
{
if (S->top == -1)
return (FALSE);
else
{
*x = S->elem[S->top];
S->top--;
return (TRUE);
}
}
int GetTop(SeqStack S, StackElementType *x)
{
if (S.top == -1)
return (FALSE);
else
{
*x = S.elem[S.top];
return (TRUE);
}
}