import java.util.*;
//通过先序、中序和后序数组两两结合重构二叉树
public class ConstrctTree{
//二叉树节点的定义
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data)
{
this.value=data;
}
}
//************************preInToTree*****************************
//先序、中序结合
public static 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 static Node preIn(int[]p,int pi,int pj,int[]n,int ni,int nj,HashMap<Integer,Integer>map)
{
if(pi>pj)
{
return null;
}
Node head=new Node(p[pi]); //构造头节点
int index=map.get(p[pi]);
//递归调用构造左右子树
head.left=preIn(p,pi+1,pi+index-ni,n,ni,index-1,map);
head.right=preIn(p,pi+index-ni+1,pj,n,index+1,nj,map);
return head;
}
//***************************************************
//****************************************************
//中序、后序结合
public static 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 static Node inPos(int[] n, int ni, int nj, int[] s, int si, int sj,
HashMap<Integer, Integer> map) {
if (si > sj) {
return null;
}
Node head = new Node(s[sj]);
int index = map.get(s[sj]);
head.left = inPos(n, ni, index - 1, s, si, si + index - ni - 1, map);
head.right = inPos(n, index + 1, nj, s, si + index - ni, sj - 1, map);
return head;
}
//*****************************************************
//(先序和后序结合)每个节点的孩子数都为0或2的二叉树才能被先序与后序重构出来
public static 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 static Node prePos(int[] p, int pi, int pj, int[] s, int si, int sj,
HashMap<Integer, Integer> map) {
Node head = new Node(s[sj--]);
if (pi == pj) {
return head;
}
int index = map.get(p[++pi]);
head.left = prePos(p, pi, pi + index - si, s, si, index, map);
head.right = prePos(p, pi + index - si + 1, pj, s, index + 1, sj, map);
return head;
}
// ******************直观地打印二叉树*******************
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static 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 static 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)
{
/*构造的二叉树
1
2 3
4 5 6 7
8 9
*/
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 }; //后序
System.out.println("先中序重构二叉树:");
printTree(preInToTree(pre,in));
System.out.println("中后序重构二叉树:");
printTree(inPosToTree(in,pos));
System.out.println("先后序重构二叉树:");
printTree(prePosToTree(pre,pos));
}
}
通过先序、中序和后序数组两两结合重构二叉树
最新推荐文章于 2024-04-23 06:06:09 发布