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

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

#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;
}

输出如下:

时间: 2024-08-22 17:27:44

根据先序中序遍历建树【模板】的相关文章

c语言-大神求教C语言,知道二叉树先序中序遍历序列,求后序遍历序列。

问题描述 大神求教C语言,知道二叉树先序中序遍历序列,求后序遍历序列. #include#include#include using namespace std; typedef struct Btree{ struct Btree *left; struct Btree *right; char data;}Node; void Create_Btree(Node *tree char *pre int pre_low int pre_high char *middle int middle_

代码-根据二叉树的先序中序重构二叉树的算法问题

问题描述 根据二叉树的先序中序重构二叉树的算法问题 代码编译没有问题,就是输出结果不对 这是我的代码 #include #include using namespace std; class TreeNode { private: int data; public: TreeNode* LeftChild; TreeNode* RightChild; TreeNode():data(0),LeftChild(NULL),RightChild(NULL) { } TreeNode(int& rec

二叉树的应用-先序遍历中序遍历还原二叉树

二叉树的一些应用 还是大实验要求的 还有已知先序遍历 中序遍历求后续遍历 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define MAX 100 //节点个数 #define Null 0 typedef int Elemtype; class node { public: Elemtype data; no

[算法系列之三]二叉树中序前序序列(或后序)求解树

[思路] 这种题一般有二种形式,共同点是都已知中序序列.如果没有中序序列,是无法唯一确定一棵树的. <1>已知二叉树的前序序列和中序序列,求解树. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元素就是右子树.若根节点左边或右边为空,则该方向子树为空:若根节点 边和右边都为空,则根节点已经为叶子节点. 3.递归求解树.将左子树和右子树分别看成一棵二叉树,重复1.2.3步,直到所有的

知道二叉树的中序和后序建立二叉树的代码哪里有问题?为什么只能建立3个结点?

问题描述 知道二叉树的中序和后序建立二叉树的代码哪里有问题?为什么只能建立3个结点? #include #include #include char zs[100]; char hs[100]; struct node { char data; struct node l; struct node *r; }; int treedepth(struct node *TT) { int i=0,j=0; if(!TT) return 0; i=treedepth(TT->l); j=treedep

UVa 548:Tree 二叉树的重建——中序遍历与后续遍历进行建树

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 题目类型: 数据结构, 二叉树 题目大意: 给两个组数字,都是在同一棵二叉树上的,第一组是按中序遍历(inorder)顺序输出的,第二组是按后序遍历(postorder)输出的, 根据这两组数据构建出原来的二叉树,然后计算从根结点到每个叶子结

UVa 548 Tree:中序遍历&amp;amp;后序遍历&amp;amp;DFS

548 - Tree Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=104&page=show_problem&problem=489 You are to determine the value of the leaf node in a given binary tree that is the termina

数据结构 排序二叉树(BST) 插入删除查询 中序遍历 销毁(后序遍历)

结构概念如下: 二叉排序树(binary sort tree): 1.也叫做二叉查找树 2.如果他的左子树不为空,则左子树上所有结点的 值均小于他的根结构的值 3.如果他的右子树不为空,则右子树上所有结点的 值均大于他的根结构的值 4.他的左右子树分别为二叉排序树 5.按照二叉树中序遍历其返回结果树有序的 下面就是一颗典型的二叉树,只是是字母,可以将字母换位字母的顺序进行审视,但是它不是平衡二叉树 排序二叉树(BST)的优点在于进行数据查找比较方便,因为他是按照数据来到的顺序进行如上的规则进行的

二叉树的非递归先序,中序,后序遍历

#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<stack> using namespace std; struct Tree{ int x; Tree *lchild, *rchild; Tree(){ lchild = rchild = NULL; } }; typedef Tree* pT; void buildT(pT &