根据先序中序遍历建树【模板】

简介:

主要就是通过先序找到当前子树根节点,再用中序遍历分子树,不断递归下去。

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <cassert>
#include <time.h>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <sstream>
#define INF 0x3f3f3f3f

using namespace std;

template <class Type>
Type stringToNum(const string& str)
{
    istringstream iss(str);
    Type num;
    iss >> num;
    return num;    
}

//======================================================

typedef struct node
{
    int m_key;
    node * m_pLeft,*m_pRight;
}BinaryTreeNode;

//BinaryTreeNode* constructTree(int* preOrder,int preLen,int* sMidOrder, int midLen){
BinaryTreeNode* constructRoot(int* preOrder, int* midOrder, int len){

    //先根遍历(前序遍历)的第一个值就是根节点的key
    int rootKey=preOrder[0];
    BinaryTreeNode* root=new BinaryTreeNode;
    root->m_key=rootKey;
    root->m_pLeft=root->m_pRight=NULL;
    if(len==1 && *preOrder==*midOrder)//只有一个节点
        return root;

    //在中根遍历(中序遍历)中找到根节点
    int* rootMidOrder=midOrder;
    int leftLen=0; //左子树节点数
    while(*rootMidOrder!=rootKey&&rootMidOrder<=(midOrder+len-1)){ 
        ++rootMidOrder;
        ++leftLen;
    }
    if(*rootMidOrder!=rootKey)//在中根序列未找到根节点,输入错误
        return NULL;

    if(leftLen>0){ //构建左子树
        root->m_pLeft=constructRoot(preOrder+1,midOrder,leftLen);
    }
    if(len-leftLen-1>0){ //构建右子树
        root->m_pRight=constructRoot(preOrder+leftLen+1,rootMidOrder+1,len-leftLen-1);
    }
    return root;
}

BinaryTreeNode* construct(int* preOrder,int* midOrder,int len){
    if(preOrder==NULL||midOrder==NULL||len<=0)
        return NULL;
    return constructRoot(preOrder,midOrder,len);
}

//先根遍历
void preOrderRecursionPrint(BinaryTreeNode* root){
    if(root==NULL)
        return;
    cout<<root->m_key<<endl;   //visit
    preOrderRecursionPrint(root->m_pLeft);
    preOrderRecursionPrint(root->m_pRight);
}

int main()
{
    //freopen("input.txt","r",stdin);

    //先根序列
    int preOrder[8]={1,2,4,7,3,5,6,8};
    //中根序列
    int midOrder[8]={4,7,2,1,5,3,8,6};

    BinaryTreeNode * treeRoot = construct(preOrder,midOrder,8);

    preOrderRecursionPrint(treeRoot);

    return 0;
}

输出如下:

这里写图片描述

相关文章
|
存储 机器学习/深度学习 人工智能
数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)
数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)
数据结构学习笔记——图的存储结构(邻接矩阵和邻接表)
|
算法 安全
二叉树的基本操作(如何计算二叉树的结点个数,二叉树的高度)
二叉树的基本操作(如何计算二叉树的结点个数,二叉树的高度)
708 0
|
前端开发 Java 应用服务中间件
基于JavaWeb实现网上图书商城系统
基于JavaWeb实现网上图书商城系统
458 0
基于JavaWeb实现网上图书商城系统
|
存储 Java
数据结构:图文详解顺序表的各种操作(新增元素,查找元素,删除元素,给指定位置元素赋值)
数据结构:图文详解顺序表的各种操作(新增元素,查找元素,删除元素,给指定位置元素赋值)
445 0
|
机器学习/深度学习 C++ 容器
二叉树创建和遍历(C++实现)
树(Tree)是n(n≥0)个节点的有限集。在任意一棵树中有且仅有一个特定的称为根(Root)的节点;当n>1时,其余节点可分m(m>0)为个互不相交的有限集T1,T2,...,Tm;其中每一个集合本身又是一棵树,并且称为根的子树(SubTree)。 二叉树(Binary Tree)是一种特殊的有序树型结构,所有节点最多只有2棵子树。
1072 0
|
机器学习/深度学习 存储 人工智能
【排序算法】排序算法介绍及插入排序 ( 直接插入排序 && 希尔排序 )
【排序算法】排序算法介绍及插入排序 ( 直接插入排序 && 希尔排序 )
|
机器学习/深度学习 自然语言处理 算法
2018 NLPCC Chinese Grammatical Error Correction 论文小结
这一段时间,笔者一直在研究语音识别后的文本纠错,而就在八月26-30日,CCF的自然语言处理和中文计算会议召开了,笔者也从师兄那里拿到了新鲜出炉的会议论文集,其中重点看的自然是其shared task2:grammatical error correction的overview以及优胜团队的论文。
11916 0