交换二叉树中所有结点左右孩子的递归算法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
{
char ch;
ch = getchar();
if(ch == '.') *bt = NULL;
else
{
*bt = (BitTree)malloc(sizeof(BitNode));
(*bt)->data = ch;
CreatBiTree(&((*bt)->LChild));
CreatBiTree(&((*bt)->RChild));
}
}
void Visit(char ch)
{
printf("%c ",ch);
}
void PreOrder(BitTree root)
{
if (root!=NULL)
{
Visit(root ->data);
PreOrder(root ->LChild);
PreOrder(root ->RChild);
#include<stdio.h>
#include <malloc.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node *LChild;
struct Node *RChild;
}BitNode,*BitTree;
void CreatBiTree(BitTree *bt)
}
}
void jiaohuan(BitTree *root)
{
BitTree m;
//m = (BitTree)malloc(sizeof(BitNode));
if(*root != NULL)
{
//*bt = (BitTree)malloc(sizeof(BitNode));
//(*bt)->data = ch;
printf("请输入二叉树中的元素(以扩展先序遍历序列输入,其中.代表空子树):\n");
CreatBiTree(&T);
printf("先序遍历序列为:\n");
PreOrder(T);
printf("交换后的二叉树是:\n");
jiaohuan(&T);
PreOrd百度文库r(T);
}
m = (*root)->LChild;
(*root)->LChild = (*root)->RChild;
(*root)->RChild = m;
jiaohuan(&((*root)->LChild));
jiaohuan(&((*root)->RChild));
}
}
void main()
{
BitTree T;
相关文档
最新文档