数据结构-二叉树-翻转二叉树
问题描述
翻转一棵二叉树
示例
输入
4
/ \
2 7
/ \ / \
1 3 6 9
输出
4
/ \
7 2
/ \ / \
9 6 3 1
分析
适用中序遍历即可解决这个问题
核心代码
BiTree FlipBTree(BiTree *T) {
BiTree Tl;
BiTree Tr;
InitBiTree(&Tl);
InitBiTree(&Tr);
/*递归出口*/
if ((*T)==NULL)
{
return NULL; //返回空结点
}
/*递归调用*/
Tl = FlipBTree(&(*T)->lchild);
Tr = FlipBTree(&(*T)->rchild);
/*核心操作*/
(*T)->lchild = Tr;
(*T)->rchild = Tl;
return *T;
}
可运行代码(经VS2015、DecC++编译运行通过)
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "io.h"
#include "math.h"
#include "time.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 100 /* 存储空间初始分配量 */
typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
int count = 1;
/* 用于构造二叉树********************************** */
int index = 1;
int index1 = 1;
typedef char String[100]; /* 0号单元存放串的长度 */
String str, str1;
/*分配字符串*/
Status StrAssign(String T, char *chars)
{
int i;
/*strlen计算字符串的长度*/
if (strlen(chars)>MAXSIZE)
return ERROR;
else
{
T[0] = strlen(chars);
for (i = 1; i <= T[0]; i++)
T[i] = *(chars + i - 1);
return OK;
}
}
/* ************************************************ */
typedef char TElemType;
TElemType Nil = ' '; /* 字符型以空格符为空 */
/*访问结点*/
Status visit(TElemType e)
{
printf("%c ", e);
return OK;
}
typedef struct BiTNode /* 结点结构 */
{
TElemType data; /* 结点数据 */
struct BiTNode *lchild, *rchild; /* 左右孩子指针 */
}BiTNode, *BiTree;
/* 构造空二叉树T */
Status InitBiTree(BiTree *T)
{
*T = NULL;
return OK;
}
/* 初始条件: 二叉树T存在。操作结果: 销毁二叉树T */
void DestroyBiTree(BiTree *T)
{
if (*T)
{
if ((*T)->lchild) /* 有左孩子 */
DestroyBiTree(&(*T)->lchild); /* 销毁左孩子子树 */
if ((*T)->rchild) /* 有右孩子 */
DestroyBiTree(&(*T)->rchild); /* 销毁右孩子子树 */
free(*T); /* 释放根结点 */
*T = NULL; /* 空指针赋0 */
}
}
/* 按前序输入二叉树中结点的值(一个字符) */
/* #表示空树,构造二叉链表表示二叉树T。 */
void CreateBiTree(BiTree *T)
{
TElemType ch;
/* scanf("%c",&ch); */
ch = str[index++];
/*左右子树切换*/
if (ch == '#')
*T = NULL;
else
{
/*生成树结点*/
*T = (BiTree)malloc(sizeof(BiTNode));
/*if (!*T)
exit(OVERFLOW);*/
(*T)->data = ch; /* 给结点赋值 */
/*递归创建左右子树*/
CreateBiTree(&(*T)->lchild); /* 构造左子树 */
CreateBiTree(&(*T)->rchild); /* 构造右子树 */
}
}
/****输出二叉树****
*其过程是对于非空二叉树T,先输出结点b的结点值,当它存在孩子时输出一个符号‘(’,然后递归处理左子树;
*当存在右孩子时,输出一个‘.’符号,再递归处理右子树,最后输出一个')'表示回退
*/
void DispBtree(BiTree b) {
if (b != NULL) {
printf("%c", b->data);
/*有孩子结点的情况下输出( */
if (b->lchild != NULL || b->rchild != NULL) {
printf("(");
/*递归处理左子树*/
DispBtree(b->lchild);
/*有右孩子结点时输出 . */
if (b->rchild != NULL)
printf(".");
/*递归处理右子树*/
DispBtree(b->rchild);
/*有孩子结点的情况下输出*/
printf(")");
}
}
}
/*中序遍历的应用
->
一定要中序遍历*/
BiTree FlipBTree(BiTree *T) {
BiTree Tl;
BiTree Tr;
InitBiTree(&Tl);
InitBiTree(&Tr);
/*递归出口*/
if ((*T)==NULL)
{
return NULL; //返回空结点
}
/*递归调用*/
Tl = FlipBTree(&(*T)->lchild);
Tr = FlipBTree(&(*T)->rchild);
/*核心操作*/
(*T)->lchild = Tr;
(*T)->rchild = Tl;
return *T;
}
int main()
{
int i;
BiTree T;
TElemType e1;
/*初始化二叉树*/
InitBiTree(&T);
/*分配字符串*str=string,目的利用字符串给二叉树赋值*/
/*前序输入,若不按规则输入,将报异常*/
/*#代表空结点*/
//StrAssign(str, "ABC##D##BD##C##");
// StrAssign(str, "ABCD####E##");
//StrAssign(str, "ABCA###DA###BDAA###B##C#A##");
StrAssign(str, "ABD##F##C#E##");
/*创建二叉树*/
CreateBiTree(&T);
FlipBTree(&T);
DispBtree(T);
//DispBtree(T);
/*销毁二叉树*/
//DestroyBiTree(&T);
system("pause");
}