二叉树构建(一)
来源:互联网 发布:php磁力链接解析接口 编辑:程序博客网 时间:2024/04/29 00:00
说明:
1.根据先序遍历和中序遍历或者后序遍历和中序遍历可以构建一棵二叉树;
2.构建以后序遍历和中序遍历为例,结点数据域以整形为例。
定义二叉树类
核心是constractPostCore函数。
class BinaryTree{private BinaryTreeNode root;static class BinaryTreeNode{ int value; BinaryTreeNode left; BinaryTreeNode right; BinaryTreeNode(int value){ this(value, null, null); } BinaryTreeNode(int value, BinaryTreeNode left, BinaryTreeNode right){ this.value = value; this.left = left; this.right = right; }}public BinaryTree(int[] postOrder, int[] inOrder){int postLen = 0;int inLen = 0;if(postOrder == null ||(postLen = postOrder.length) == 0){throw new IllegalArgumentException();}if(inOrder == null || (inLen = inOrder.length) == 0){throw new IllegalArgumentException();}this.root = this.constractPostCore(postOrder, 0, postLen -1, inOrder, 0, inLen - 1);}private BinaryTreeNode constractPostCore(int[] postOrder, int postBegin, int postEnd, int[] inOrder, int inBegin, int inEnd){ // may be leftLen <= 0 or rightLen <= 0 if(postBegin > postEnd || inBegin > inEnd){ return null; } // this a leaf node or may be throw exception boolean isLeafOrException = ((postBegin == postEnd) && (inBegin == inEnd)); if(isLeafOrException){ boolean isException = (postOrder[postBegin] != inOrder[inBegin]); if(isException) throw new IllegalArgumentException(); return new BinaryTreeNode(postOrder[postEnd],null,null); } // other situation postBegin < postEnd && inBegin < inEnd int rootValue = postOrder[postEnd]; BinaryTreeNode currentRoot = new BinaryTreeNode(rootValue, null, null); int rootPositionInOrder = this.getRootPositionInOrder(inOrder, inBegin, inEnd, rootValue); boolean isFound = (rootPositionInOrder != -1); if(isFound){ // leftTreeLen may be negative but do not need to //check because that at the begin will be checked int leftTreeLen = rootPositionInOrder - inBegin; // rightTreeLen is the same as leftTreeLen may be negative int rightTreeLen = inEnd - rootPositionInOrder; currentRoot.left = this.constractPostCore(postOrder, postBegin, postBegin + leftTreeLen - 1, inOrder, inBegin, rootPositionInOrder - 1); currentRoot.right = this.constractPostCore(postOrder, postEnd - rightTreeLen, postEnd - 1, inOrder, rootPositionInOrder + 1, inEnd); }else{ throw new IllegalArgumentException("currentValue not found in inOrder" + rootValue); } return currentRoot; }private int getRootPositionInOrder(int[] inOrder, int inBegin , int inEnd, int value){ for(int i = inBegin; i <= inEnd; i++){ if(inOrder[i] == value){ return i; } } return -1;}}
0 0
- 二叉树构建(一)
- 构建一棵二叉树的镜像树
- Java构建一颗二叉树
- 构建二叉树(java)
- 构建一棵二叉树并按照层次遍历输出
- 【c++】构建一棵简单的二叉树
- poj1577FallingTree(二叉树,构建是难点)
- 二叉树的构建(Java)
- 如何构建二叉树
- 如何构建二叉树
- 构建二叉树实例
- 构建二叉树
- leetcode:构建二叉树
- 二叉树的构建
- C++构建二叉树
- 构建二叉树
- 二叉查找树构建
- 构建二叉树
- An internal error occurred during: "Importing Maven projects". 错误
- android.permission.WRITE_SECURE_SETTINGS权限报错
- SGU 180 - Inversions(逆序数)
- Android异步双屏显示
- poj-3286-How many 0's?
- 二叉树构建(一)
- 登陆密码加密
- 三句话影响人的一生
- Java IO
- 给图片增加水印
- 互联网时代的社会语言学:基于SNS的文本数据挖掘
- 2014新年计划
- Ubuntu9.10用apt-get配置apache+php+mysql
- Android中使用POI加载与显示word文档