一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树。

 一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树。

       根据前序遍历的特点, 知前序序列(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
  1. //二叉链表表示二叉树  
  2. typedef struct BiNode  
  3. {  
  4.     char data;//节点数据  
  5.     struct BiNode * lchild;//左孩子  
  6.     struct BiNode * rchild;//右孩子  
  7. }BiNode, * BiTree;  
 

由前序遍历序列和中序遍历序列确定一颗唯一的二叉树的算法余下:

[cpp]  view plain  copy
  1. //由前序序列和中序序列建立二叉树的过程  
  2. void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列  
  3. {  
  4.    if(presequence.length()==0)  
  5.    {  
  6.          t=NULL;  
  7.          return ;  
  8.    }  
  9.    char rootNode=presequence[0];//根  
  10.    int index=insequence.find(rootNode);//根在中序序列中的位置  
  11.    string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列  
  12.    string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列  
  13.    int lchild_length=lchild_insequence.length();//左孩子的长度  
  14.    int rchild_length=rchild_insequence.length();//右孩子的长度  
  15.    string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列  
  16.    string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列  
  17.   
  18.    t=(BiTree)malloc(sizeof(BiNode));  
  19.    if(t!=NULL)  
  20.    {  
  21.        t->data=rootNode;  
  22.        CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子  
  23.        CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子  
  24.    }  
  25. }  

完整程序代码余下:

[cpp]  view plain  copy
  1. // 由前序序列和中序序列构造二叉树.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <string>  
  6. #include <iostream>  
  7.   
  8. using namespace std;  
  9.   
  10. //二叉链表表示二叉树  
  11. typedef struct BiNode  
  12. {  
  13.     char data;//节点数据  
  14.     struct BiNode * lchild;//左孩子  
  15.     struct BiNode * rchild;//右孩子  
  16. }BiNode, * BiTree;  
  17.   
  18. //由前序序列和中序序列建立二叉树的过程  
  19. void CreateBiTree(BiTree & t,string presequence,string insequence)//t为要建立的二叉树,presequence和insequence分别为前序和中序序列  
  20. {  
  21.    if(presequence.length()==0)  
  22.    {  
  23.          t=NULL;  
  24.          return ;  
  25.    }  
  26.    char rootNode=presequence[0];//根  
  27.    int index=insequence.find(rootNode);//根在中序序列中的位置  
  28.    string lchild_insequence=insequence.substr(0,index);//左孩子的中序序列  
  29.    string rchild_insequence=insequence.substr(index+1);//右孩子的中序序列  
  30.    int lchild_length=lchild_insequence.length();//左孩子的长度  
  31.    int rchild_length=rchild_insequence.length();//右孩子的长度  
  32.    string lchild_presequence=presequence.substr(1,lchild_length);//左孩子的前序序列  
  33.    string rchild_presequence=presequence.substr(1+lchild_length);//右孩子的前序序列  
  34.   
  35.    t=(BiTree)malloc(sizeof(BiNode));  
  36.    if(t!=NULL)  
  37.    {  
  38.        t->data=rootNode;  
  39.        CreateBiTree(t->lchild,lchild_presequence,lchild_insequence);//递归创建左孩子  
  40.        CreateBiTree(t->rchild,rchild_presequence,rchild_insequence);//递归创建右孩子  
  41.    }  
  42. }  
  43.   
  44. //检验是否创建成功   
  45. //递归前序遍历二叉树  
  46.  void PreOrderTraverse(BiTree & t)  
  47.  {  
  48.      if(t!=NULL)  
  49.      {  
  50.          cout<<t->data;  
  51.          PreOrderTraverse(t->lchild);  
  52.          PreOrderTraverse(t->rchild);  
  53.      }  
  54.  }  
  55.   
  56.  //递归中序序遍历二叉树  
  57. void InOrderTraverse(BiTree & t)  
  58.  {  
  59.      if(t!=NULL)  
  60.      {  
  61.            
  62.          InOrderTraverse(t->lchild);  
  63.          cout<<t->data;  
  64.          InOrderTraverse(t->rchild);  
  65.      }  
  66.  }  
  67.   
  68. int _tmain(int argc, _TCHAR* argv[])  
  69. {  
  70.     BiTree t;  
  71.     string presequence="ABCDEFG";  
  72.     string insequence ="CBEDAFG";  
  73.     CreateBiTree(t,presequence,insequence);  
  74.   
  75.     PreOrderTraverse(t);  
  76.     cout<<endl;  
  77.     InOrderTraverse(t);  
  78.     system("PAUSE");  
  79.     return 0;  
  80. }  

结果如下:

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值