1、采用书上第 127 页定义的二叉树的二叉链表存储表示,编写递归算法实现二叉树的下列基本操作。
(1)构造二叉树 (2)复制二叉树 (3)销毁二叉树 (4)先序遍历二叉树
(5)中序遍历二叉树 (6)后序遍历二叉树 (7)求二叉树的深度 (8)求二叉树的结点数
(9)求二叉树的叶子结点数 (10)交换二叉树的左右子树
2、采用书上第 127 页定义的二叉树的二叉链表存储表示,编写非递归算法实现二叉树的下列基本操作。
(1)构造二叉树 (2)先序遍历二叉树 (3)中序遍历二叉树
(4)后序遍历二叉树
例题一:
#include <stdio.h>
#include <stdlib.h>
#define TURE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef char TElemType;
typedef struct BiTNode
{
TElemType data;
struct BiTNode* lchild, * rchild;
}BiTNode,*BiTree;
int Nodenum = 0;
int Count = 0;
Status CreateBiTree(BiTree& T) {
char ch;
scanf("%c", &ch);
if (ch == '#')T = NULL;
else {
if (!(T = (BiTNode*)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
return OK;
}
Status PreOrderTraverse(BiTree T) {
if (T) {
printf("%c", T->data);
if (PreOrderTraverse(T->lchild))
if (PreOrderTraverse(T->rchild))return OK;
return ERROR;
}
else return OK;
}
Status InOrderTraverse(BiTree T) {
int m = 0;
if (T) {
if (InOrderTraverse(T->lchild))m = 1;
printf("%c", T->data);
if (m)
if (InOrderTraverse(T->rchild))return OK;
return ERROR;
}
else return OK;
}
Status PostOrderTraverse(BiTree T) {
if (T) {
if (PostOrderTraverse(T->lchild))
if (PostOrderTraverse(T->rchild)) {
printf("%c", T->data); return OK;
}
return ERROR;
}
else return OK;
}
int Depth(BiTree T)
{
int leftDepth, rightDepth;
if (T == NULL)return 0;
else {
leftDepth = Depth(T->lchild);
rightDepth = Depth(T->rchild);
if (leftDepth >= rightDepth)return leftDepth + 1;
else return rightDepth + 1;
}
}
Status NodeCount(BiTree T) {
if (T) {
Nodenum++;
if (NodeCount(T->lchild))
if (NodeCount(T->rchild))return OK;
return ERROR;
}
else return OK;
}
Status LeafCount(BiTree T) {
if (T) {
if (T->lchild == NULL && T->rchild == NULL) {
printf("%c", T->data); Count++;
}
if (LeafCount(T->lchild))
if (LeafCount(T->rchild))return OK;
return ERROR;
}
else return OK;
}
Status ExchangeBiTree(BiTree& T) {
BiTree p;
if (T) {
if (T->lchild || T->rchild) {
p = T->lchild; T->lchild = T->rchild; T->rchild = p;
}
if (ExchangeBiTree(T->lchild))
if (ExchangeBiTree(T->rchild))return OK;
return ERROR;
}
else return OK;
}
Status CopyBiTree(BiTree T, BiTree& B) {
if (T == NULL)
B = NULL;
else {
if (!(B = (BiTNode*)malloc(sizeof(BiTNode)))) exit(OVERFLOW);
B->data = T->data;
CopyBiTree(T->lchild, B->lchild);
CopyBiTree(T->rchild, B->rchild);
}
return OK;
}
Status DestoryTree(BiTree& T) {
if (T) {
DestoryTree(T->lchild);
DestoryTree(T->rchild);
free(T);
}
return OK;
}
Status ClearTree(BiTree& T) {
if (T) {
T = NULL;
}
return OK;
}
int main()
{
BiTree T, B;
printf("创建二叉树,按先序次序输入二叉树中结点的值:\n");
CreateBiTree(T);
NodeCount(T);
printf("二叉树的结点个数为:%d\n", Nodenum);
printf("二叉树的深度为:%d\n", Depth(T));
printf("先序遍历二叉树,结果是:\n");
PreOrderTraverse(T);
printf("\n");
printf("后序遍历二叉树,结果是:\n");
PostOrderTraverse(T);
printf("\n");
printf("输出二叉树的叶子结点:\n");
LeafCount(T);
printf("\n");
printf("统计二叉树的叶子结点个数:%d\n", Count);
printf("交换二叉树中所有节点的左右子树!\n");
ExchangeBiTree(T);
if (CopyBiTree(T, B) == OK)printf("成功复制二叉树T到二叉树B!\n");
if (DestoryTree(T) == OK)printf("成功销毁二叉树T!\n");
if (ClearTree(T) == OK)printf("将二叉树T置为空树!\n");
printf("先序遍历二叉树B,结果是:\n");
PreOrderTraverse(B);
printf("\n");
printf("中序遍历二叉树B,结果是:\n");
InOrderTraverse(B);
printf("\n");
printf("后续遍历二叉树B,结果是:\n");
PostOrderTraverse(B);
printf("\n");
}
例题二:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCERMENT 10
typedef int Status;
typedef char TElemType;
typedef int QElemType;
int Nodenum = 0;
typedef struct BiNode
{
TElemType data;
struct BiNode* lchild, * rchild;
}BiNode,*BiTree;
typedef struct SqStack
{
BiTree* base;
BiTree* top;
int stacksize;
}SqStack;
typedef struct QNode
{
BiTree data;
struct QNode* next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue& Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if (!Q.front) exit(OVERFLOW);
Q.front->next = NULL;
return OK;
}
Status QueueEmpty(LinkQueue Q)
{
if (Q.front == Q.rear) return TRUE;
else return FALSE;
}
Status EnQueue(LinkQueue& Q, BiTree e)
{
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if (!p) exit(OVERFLOW);
p->data = e; p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return OK;
}
Status DeQueue(LinkQueue& Q, BiTree& e)
{
QueuePtr p;
if (Q.front == Q.rear) return OK;
p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if (Q.rear == p) Q.front = Q.rear;
free(p);
return OK;
}
Status InitStack(SqStack& S)
{
S.base = (BiTree*)malloc(STACK_INIT_SIZE * sizeof(BiTree));
if (!S.base) exit(OVERFLOW);
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack& S, BiTree& T)
{
if (S.top == S.base) return OK;
T = *(S.top - 1);
return OK;
}
Status Push(SqStack& S, BiTree& T)
{
if (S.top - S.base > S.stacksize)
{
if (!(S.base = (BiTree*)realloc(S.base, (S.stacksize + STACKINCERMENT) * sizeof(BiTree)))) exit(OVERFLOW);
if (!S.base) exit(OVERFLOW);
S.top = S.base + STACK_INIT_SIZE;
S.stacksize += STACKINCERMENT;
}
*S.top++ = T;
return OK;
}
Status Pop(SqStack& S, BiTree& T)
{
if (S.top == S.base) return ERROR;
T = *--S.top;
return OK;
}
Status StackEmpty(SqStack& S)
{
if (S.top == S.base)
return TRUE;
else
return FALSE;
}
Status CreateBiTree(BiTree& T)
{
BiTree p;
struct TFlag{
int LTag=0;
int RTag=0;
}Tr[600];
SqStack Q; InitStack(Q);
int i = 0;
char ch[600];
scanf("%s", &ch);
T = (BiTree)malloc(sizeof(BiNode));
T->data = ch[0];
Push(Q, T);
int num = 1;
while (ch[num] != '\0')
{
if (ch[num] == '#')
{
while (!StackEmpty(Q))
{
GetTop(Q, p);
if (Tr[i].LTag == 0)
{
p->lchild = NULL; Tr[i].LTag = 1; break;
}
else if (Tr[i].RTag == 0)
{
p->rchild = NULL; Tr[i].RTag = 1; break;
}
else
{
Pop(Q, p); Tr[i].LTag = 0; Tr[i].RTag = 0; i--;
}
}
}
else
{
BiTree m = (BiTree)malloc(sizeof(BiNode));
m->data = ch[num];
while (!StackEmpty(Q))
{
GetTop(Q, p);
if (Tr[i].LTag == 0)
{
p->lchild = m; Tr[i].LTag = 1; break;
}
else if (Tr[i].RTag == 0)
{
p->rchild = m; Tr[i].RTag = 1; break;
}
else
{
Pop(Q, p); Tr[i].LTag = 0; Tr[i].RTag = 0; i--;
}
}
Push(Q, m);
i++;
Tr[i].LTag = 0;
Tr[i].RTag = 0;
}
num++;
}
return OK;
}
int Depth(BiTree T) {
int first = -1, last = -1, flag = 0, level = 0;
BiTree Dueue[STACK_INIT_SIZE], Temp;
Dueue[++last] = T;
while (first < last) {
Temp = Dueue[++first];
if (Temp->lchild)
{
Dueue[++last] = Temp->lchild;
}
if (Temp->rchild)
{
Dueue[++last] = Temp->rchild;
}
if (first == flag)
{
level++;
flag = last;
}
}
return level;
}
Status InOrderTraverse(BiTree T)
{
SqStack S; InitStack(S); Push(S, T); BiTree p;
while (!StackEmpty(S))
{
while (GetTop(S, p) && p) Push(S, p->lchild);
Pop(S, p);
if (!StackEmpty(S))
{
Pop(S, p); printf("%c", p->data);
Push(S, p->rchild);
}
}
printf("\n");
return 0;
}
Status PreOrderTraverse(BiTree T)
{
SqStack S; InitStack(S); Push(S, T); BiTree p;
while (!StackEmpty(S))
{
while (GetTop(S, p) && p) { printf("%c", p->data); Push(S, p->lchild); }
Pop(S, p);
if (!StackEmpty(S))
{
Pop(S, p);
Push(S, p->rchild);
}
}
printf("\n");
return 0;
}
Status PostOrderTraverse(BiTree T)
{
SqStack S; InitStack(S); BiTree p = T, q, r=NULL;
while (p || !StackEmpty(S))
{
if (p)
{
Push(S, p); p = p->lchild;
}
else
{
GetTop(S, q);
if (q->rchild == NULL || q->rchild == r)
{
Pop(S, r);
printf("%c", r->data);
}
else p = q->rchild;
}
}
printf("\n");
return 0;
}
Status ExchangeBiTree(BiTree& T)
{
LinkQueue Q; InitQueue(Q); BiTree Temp, p;
EnQueue(Q, T);
while (!QueueEmpty(Q))
{
DeQueue(Q, p);
if (p->lchild!=NULL || p->rchild!=NULL)
{
Temp = p->lchild; p->lchild = p->rchild; p->rchild = Temp;
}
if (p->lchild!=NULL) EnQueue(Q, p->lchild);
if (p->rchild!=NULL) EnQueue(Q, p->rchild);
}
return OK;
}
void levelTraverse(BiTree T)
{
BiTree a[600];
BiTree b[600];
a[0] = T; a[1] = NULL;
b[0] = T; b[1] = NULL;
printf("%c", T->data);
int num = 1;
while (num != 0)
{
num = 0;
for (int i = 0; a[i] != NULL; i++)
{
if (a[i]->lchild != NULL) b[num++] = a[i]->lchild;
if (a[i]->rchild != NULL) b[num++] = a[i]->rchild;
if (a[i]->lchild == NULL && a[i]->rchild == NULL) Nodenum++;
}
for (int i = 0; i < num; i++)
{
a[i] = b[i]; printf("%c", a[i]->data);
}
a[num] = NULL;
}
}
int main()
{
BiTree T;
printf("创建二叉树,按先序次序输入二叉树中结点的值:\n");
CreateBiTree(T);
printf("二叉树的深度为:%d\n", Depth(T));
printf("先序遍历二叉树,结果是:\n");
PreOrderTraverse(T);
printf("中序遍历二叉树,结果是:\n");
InOrderTraverse(T);
printf("后序遍历二叉树,结果是:\n");
PostOrderTraverse(T);
printf("层序遍历二叉树,结果是:\n");
levelTraverse(T);
printf("统计二叉树的叶子结点的个数:%d\n",Nodenum );
printf("交换二叉树中所有结点的左右子树!\n");
ExchangeBiTree(T);
printf("先序遍历二叉树,结果是:\n");
PreOrderTraverse(T);
printf("中序遍历二叉树,结果是:\n");
InOrderTraverse(T);
printf("后序遍历二叉树,结果是:\n");
PostOrderTraverse(T);
printf("层序遍历二叉树,结果是:\n");
levelTraverse(T);
}