根据前序遍历序列和中序遍历序列构造二叉树

转载地址:http://blog.csdn.net/yunzhongguwu005/article/details/9270085

感谢作者!!!

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

       根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(root),  根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列,  后边的序列为根的右子树的中序遍历序列。 设在中序遍历序列(InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。


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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值