先序、中序和后序数组两两结合重构二叉树
2017-11-07 20:41
316 查看
import java.util.HashMap; /** * Created by lxw, liwei4939@126.com on 2017/11/7. * 先序、中序和后序数组两两结合重构二叉树 * 给定二叉树的先序、中序和后序数组,从三个数组中任意选择两个 * 重构原来的二叉树,并返回二叉树的头结点 */ public class preIoPos { public static class Node{ public int value; public Node left; public Node right; public Node(int data){ this.value = data; } } // 先序、中序结合重构二叉树 public Node preInToTree(int[] pre, int[] in){ if(pre == null || in == null){ return null; } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i=0; i< in.length; i++){ map.put(in[i], i); } return preIn(pre, 0, pre.length-1, in, 0, in.length-1, map); } public Node preIn(int[] pre, int pi, int pj,int[] in, int ni, int nj, HashMap<Integer,Integer> map){ if(pi > pj){ return null; } Node head = new Node(pre[pi]); int index = map.get(pre[pi]); head.left = preIn(pre, pi+1, pi+index-ni, in, ni, index-1, map); head.right = preIn(pre, pi+index-ni+1, pj, in, index+1, nj, map); return head; } //中序、后序数组重构二叉树 public Node inPosToTree(int[] in, int[] pos){ if(in == null || pos == null){ return null; } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i=0; i<in.length; i++){ map.put(in[i], i); } return inPos(in, 0, in.length-1, pos, 0, pos.length-1, map); } public Node inPos(int[] in, int ni, int nj, int[] pos, int pi, int pj, HashMap<Integer,Integer> map){ if(pi > pj){ return null; } Node head = new Node(pos[pj]); int index = map.get(pos[pj]); head.left = inPos(in, ni, index-1, pos, pi, pi+index-1-ni,map); head.right = inPos(in, index+1, nj, pos, pi+index-ni, pj-1, map); return head; } // 先序、后序数组重构二叉树 // 每个节点的孩子数都为0或2的二叉树才能被先序和后序重构出来 public Node prePosToTree(int[] pre, int[] pos){ if(pre == null || pos == null){ return null; } HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i=0; i<pos.length; i++){ map.put(pos[i], i); } return prePos(pre, 0, pre.length-1, pos, 0, pos.length-1, map); } public Node prePos(int[] pre, int pi, int pj, int[] pos, int si, int sj, HashMap<Integer,Integer> map){ Node head = new Node(pos[sj--]); if(pi == pj){ return head; } int index = map.get(pre[++pi]); head.left = prePos(pre, pi, pi+index-si, pos, si, index, map); head.right = prePos(pre, pi+index-si+1, pj, pos, index+1, sj, map); return head; } // for test -- print tree public void printTree(Node head) { System.out.println("Binary Tree:"); printInOrder(head, 0, "H", 17); System.out.println(); } public void printInOrder(Node head, int height, String to, int len) { if (head == null) { return; } printInOrder(head.right, height + 1, "v", len); String val = to + head.value + to; int lenM = val.length(); int lenL = (len - lenM) / 2; int lenR = len - lenM - lenL; val = getSpace(lenL) + val + getSpace(lenR); System.out.println(getSpace(height * len) + val); printInOrder(head.left, height + 1, "^", len); } public String getSpace(int num) { String space = " "; StringBuffer buf = new StringBuffer(""); for (int i = 0; i < num; i++) { buf.append(space); } return buf.toString(); } public static void main(String[] args) { preIoPos tmp = new preIoPos(); int[] pre = {1, 2, 4, 5, 8, 9, 3, 6, 7}; int[] in = {4, 2, 8, 5, 9, 1, 6, 3, 7}; int[] pos = {4, 8, 9, 5, 2, 6, 7, 3, 1}; tmp.printTree(tmp.preInToTree(pre, in)); tmp.printTree(tmp.inPosToTree(in, pos)); tmp.printTree(tmp.prePosToTree(pre, pos)); } }
相关文章推荐
- 通过先序、中序和后序数组两两结合重构二叉树
- 利用先序、后序、中序两两组合重构二叉树(以Java为例)
- 二叉树问题---先序,中序,和后序数组两两结合重构二叉树
- uva 548 - Tree 二叉树重构(中序和后序)+dfs
- 二叉树的前中后层次遍历(递归+非递归)、创建树(数组、前序+中序、中序加后序)
- 二叉树的实现及先序、中序、后序遍历
- 已知二叉树的前序遍历和中序遍历,如何得到它的后序遍历?
- (1)建立二叉树的二叉链表。 (2)写出对用二叉链表存储的二叉树进行先序、中序和后序遍历的递归和非递归算法。 (3)写出对用二叉链表存储的二叉树进行层次遍历算法。 (4)求二叉树的所有叶子及结点总数。
- 根据后序数组重建搜索二叉树
- 二叉树的建立以及先序、中序、后序遍历C语言实现
- 前序和中序、中序和后序确定二叉树
- 二叉树先序、中序,后序遍历的非递归实现
- 二叉树的先序、中序、后序遍历的递归和非递归实现
- 二叉树前序、中序、后序遍历相互求法
- 二叉树:根据二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列
- 二叉树前序、中序、后序非递归遍历
- 根据二叉树前序遍历和中序遍历的结果,重建出该二叉树并后序遍历之 java代码
- UVA 548 根据中序和后序建立二叉树并求根到叶的最短路
- 前序创建二叉树+ 前序/中序/后序遍历二叉树
- 对二叉树进行前序、中序、后序遍历