二叉树的遍历(篇5)由中序和先序序列重建二叉树

2024-09-04 02:08

本文主要是介绍二叉树的遍历(篇5)由中序和先序序列重建二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

让我们考虑下面的遍历:

中序序列:DBEAFC
前序序列:ABDECF

在Preorder序列中,最左侧的元素是树的根。所以我们知道’A’是给定序列的根。通过在Inorder序列中搜索’A’,我们可以发现’A’左侧的所有元素都在左子树中,右侧的元素在右子树中。所以我们现在知道下面的结构。

                 A/   \/       \D B E     F C

我们递归地按照上面的步骤,得到以下树。

         A/   \
     /       \
    B         C/ \        //     \    /
D       E  F

算法:buildTree()

1)从Preorder中选择一个元素。preIndex++(下面的代码中的preIndex)在下一个递归调用中选择下一个元素。
2)用从Preorder中选的元素创建一个新的树节点tNode。
3)在Inorder中查找所选元素的索引。让索引为inIndex。
4)在inIndex之前调用元素的buildTree,并将构建的树作为tNode的左子树。
5)在inIndex之后调用元素的buildTree,并将构建的树作为tNode的右子树。
6return tNode。

代码


// Java program to construct a tree using inorder and preorder traversal/* A binary tree node has data, pointer to left childand a pointer to right child */
class Node 
{char data;Node left, right;Node(char item) {data = item;left = right = null;}
}class BinaryTree 
{Node root;static int preIndex = 0;/* Recursive function to construct binary of size len fromInorder traversal in[] and Preorder traversal pre[].Initial values of inStrt and inEnd should be 0 and len -1.  The function doesn't do any error checking for cases where inorder and preorder do not form a tree */Node buildTree(char in[], char pre[], int inStrt, int inEnd) {if (inStrt > inEnd) return null;/* Pick current node from Preorder traversal using preIndexand increment preIndex */Node tNode = new Node(pre[preIndex++]);/* If this node has no children then return */if (inStrt == inEnd)return tNode;/* Else find the index of this node in Inorder traversal */int inIndex = search(in, inStrt, inEnd, tNode.data);/* Using index in Inorder traversal, construct left andright subtress */tNode.left = buildTree(in, pre, inStrt, inIndex - 1);tNode.right = buildTree(in, pre, inIndex + 1, inEnd);return tNode;}/* UTILITY FUNCTIONS *//* Function to find index of value in arr[start...end]The function assumes that value is present in in[] */int search(char arr[], int strt, int end, char value) {int i;for (i = strt; i <= end; i++) {if (arr[i] == value)return i;}return i;}/* This funtcion is here just to test buildTree() */void printInorder(Node node) {if (node == null)return;/* first recur on left child */printInorder(node.left);/* then print the data of node */System.out.print(node.data + " ");/* now recur on right child */printInorder(node.right);}// driver program to test above functionspublic static void main(String args[]) {BinaryTree tree = new BinaryTree();char in[] = new char[]{'D', 'B', 'E', 'A', 'F', 'C'};char pre[] = new char[]{'A', 'B', 'D', 'E', 'C', 'F'};int len = in.length;Node root = tree.buildTree(in, pre, 0, len - 1);// building the tree by printing inorder traversalSystem.out.println("Inorder traversal of constructed tree is : ");tree.printInorder(root);}
}

这篇关于二叉树的遍历(篇5)由中序和先序序列重建二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134692

相关文章

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

利用Python实现时间序列动量策略

《利用Python实现时间序列动量策略》时间序列动量策略作为量化交易领域中最为持久且被深入研究的策略类型之一,其核心理念相对简明:对于显示上升趋势的资产建立多头头寸,对于呈现下降趋势的资产建立空头头寸... 目录引言传统策略面临的风险管理挑战波动率调整机制:实现风险标准化策略实施的技术细节波动率调整的战略价

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

PostgreSQL 序列(Sequence) 与 Oracle 序列对比差异分析

《PostgreSQL序列(Sequence)与Oracle序列对比差异分析》PostgreSQL和Oracle都提供了序列(Sequence)功能,但在实现细节和使用方式上存在一些重要差异,... 目录PostgreSQL 序列(Sequence) 与 oracle 序列对比一 基本语法对比1.1 创建序

Java遍历HashMap的6种常见方式

《Java遍历HashMap的6种常见方式》这篇文章主要给大家介绍了关于Java遍历HashMap的6种常见方式,方法包括使用keySet()、entrySet()、forEach()、迭代器以及分别... 目录1,使用 keySet() 遍历键,再通过键获取值2,使用 entrySet() 遍历键值对3,

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动

关于最长递增子序列问题概述

《关于最长递增子序列问题概述》本文详细介绍了最长递增子序列问题的定义及两种优化解法:贪心+二分查找和动态规划+状态压缩,贪心+二分查找时间复杂度为O(nlogn),通过维护一个有序的“尾巴”数组来高效... 一、最长递增子序列问题概述1. 问题定义给定一个整数序列,例如 nums = [10, 9, 2

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

uva 10131 最长子序列

题意: 给大象的体重和智商,求体重按从大到小,智商从高到低的最长子序列,并输出路径。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vect