一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树。
2018-03-21 21:53
627 查看
一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树。 根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root), 然后在中序序列(InSequence)中查找此根(root), 根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列, 后边的序列为根的右子树的中序遍历序列。 设在中序遍历序列(InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。 二叉树的定义于下:[cpp] view plain copy//二叉链表表示二叉树
typedef struct BiNode
{
char data;//节点数据
struct BiNode * lchild;//左孩子
struct BiNode * rchild;//右孩子
}BiNode, * BiTree;
由前序遍历序列和中序遍历序列确定一颗唯一的二叉树的算法余下:[cpp] view plain copy//由前序序列和中序序列建立二叉树的过程
void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列
{
if(presequence.length()==0)
{
t=NULL;
return ;
}
char rootNode=presequence[0];//根
int index=insequence.find(rootNode);//根在中序序列中的位置
string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列
string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列
int lchild_length=lchild_insequence.length();//左孩子的长度
int rchild_length=rchild_insequence.length();//右孩子的长度
string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列
string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列
t=(BiTree)malloc(sizeof(BiNode));
if(t!=NULL)
{
t->data=rootNode;
CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子
CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子
}
}
完整程序代码余下:[cpp] view plain copy// 由前序序列和中序序列构造二叉树.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//二叉链表表示二叉树
typedef struct BiNode
{
char data;//节点数据
struct BiNode * lchild;//左孩子
struct BiNode * rchild;//右孩子
}BiNode, * BiTree;
//由前序序列和中序序列建立二叉树的过程
void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列
{
if(presequence.length()==0)
{
t=NULL;
return ;
}
char rootNode=presequence[0];//根
int index=insequence.find(rootNode);//根在中序序列中的位置
string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列
string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列
int lchild_length=lchild_insequence.length();//左孩子的长度
int rchild_length=rchild_insequence.length();//右孩子的长度
string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列
string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列
t=(BiTree)malloc(sizeof(BiNode));
if(t!=NULL)
{
t->data=rootNode;
CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子
CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子
}
}
//检验是否创建成功
//递归前序遍历二叉树
void PreOrderTraverse(BiTree & t)
{
if(t!=NULL)
{
cout<<t->data;
PreOrderTraverse(t->lchild);
PreOrderTraverse(t->rchild);
}
}
//递归中序序遍历二叉树
void InOrderTraverse(BiTree & t)
{
if(t!=NULL)
{
InOrderTraverse(t->lchild);
cout<<t->data;
InOrderTraverse(t->rchild);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BiTree t;
string presequence="ABCDEFG";
string insequence ="CBEDAFG";
CreateBiTree(t,presequence,insequence);
PreOrderTraverse(t);
cout<<endl;
InOrderTraverse(t);
system("PAUSE");
return 0;
}
结果如下:
typedef struct BiNode
{
char data;//节点数据
struct BiNode * lchild;//左孩子
struct BiNode * rchild;//右孩子
}BiNode, * BiTree;
由前序遍历序列和中序遍历序列确定一颗唯一的二叉树的算法余下:[cpp] view plain copy//由前序序列和中序序列建立二叉树的过程
void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列
{
if(presequence.length()==0)
{
t=NULL;
return ;
}
char rootNode=presequence[0];//根
int index=insequence.find(rootNode);//根在中序序列中的位置
string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列
string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列
int lchild_length=lchild_insequence.length();//左孩子的长度
int rchild_length=rchild_insequence.length();//右孩子的长度
string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列
string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列
t=(BiTree)malloc(sizeof(BiNode));
if(t!=NULL)
{
t->data=rootNode;
CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子
CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子
}
}
完整程序代码余下:[cpp] view plain copy// 由前序序列和中序序列构造二叉树.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
//二叉链表表示二叉树
typedef struct BiNode
{
char data;//节点数据
struct BiNode * lchild;//左孩子
struct BiNode * rchild;//右孩子
}BiNode, * BiTree;
//由前序序列和中序序列建立二叉树的过程
void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列
{
if(presequence.length()==0)
{
t=NULL;
return ;
}
char rootNode=presequence[0];//根
int index=insequence.find(rootNode);//根在中序序列中的位置
string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列
string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列
int lchild_length=lchild_insequence.length();//左孩子的长度
int rchild_length=rchild_insequence.length();//右孩子的长度
string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列
string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列
t=(BiTree)malloc(sizeof(BiNode));
if(t!=NULL)
{
t->data=rootNode;
CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子
CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子
}
}
//检验是否创建成功
//递归前序遍历二叉树
void PreOrderTraverse(BiTree & t)
{
if(t!=NULL)
{
cout<<t->data;
PreOrderTraverse(t->lchild);
PreOrderTraverse(t->rchild);
}
}
//递归中序序遍历二叉树
void InOrderTraverse(BiTree & t)
{
if(t!=NULL)
{
InOrderTraverse(t->lchild);
cout<<t->data;
InOrderTraverse(t->rchild);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
BiTree t;
string presequence="ABCDEFG";
string insequence ="CBEDAFG";
CreateBiTree(t,presequence,insequence);
PreOrderTraverse(t);
cout<<endl;
InOrderTraverse(t);
system("PAUSE");
return 0;
}
结果如下:
相关文章推荐
- 前序遍历和中序遍历序列能确定唯一的一棵二叉树
- C++ 根据前序遍历序列和中序遍历序列可以构造唯一的二叉树
- 遍历序列唯一确定二叉树的问题 此博文包含图片http://blog.sina.com.cn/s/blog_a49d9ef801014b7u.html
- 遍历序列唯一确定二叉树的问题
- 已知一颗二叉树S的前序遍历和中序遍历 序列,请编程输出二叉树S的后续遍历序列.
- 二叉树根据前序遍历和中序遍历重构确定唯一二叉树
- 什么样的遍历序列组合可以唯一地创建一棵二叉树
- 已知一颗二叉树S的前序遍历和中序遍历序列,请编程输出二叉树S的后续遍历序列。
- 遍历序列唯一确定二叉树的问题
- 根据前序遍历(或者是后序遍历)和中序遍历得到的序列可以重建二叉树
- 二叉树 二叉树带虚结点表示的先序遍历可以确定唯一一颗二叉树
- 如何根据前序遍历序列和中序遍历序列确定二叉树
- 如何根据前序遍历序列和中序遍历序列确定二叉树
- 已知一颗二叉树的前序和中序序列,唯一的确定一颗二叉树
- 《剑指offer》:[24]判断一个序列是否为二叉树的后序遍历序列
- 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5
- 由先序+后序遍历确定序列是否唯一并输出一个中序序列
- 利用二叉树的中序遍历和后序遍历序列构造一个二叉树Search results for Construct Binary Tree from Inorder and Postorder Traversa
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal (利用中序遍历和后续遍历确定一颗二叉树)
- 剑指Offer_06 根据前序遍历和中序遍历序列 重建二叉树