Java与算法(13)
2017-06-22 16:43
190 查看
Java与算法(13)
1.高效求解完全二叉树的节点个数
public class GetTreeNodeNumber { public static class Node { public int value; public Node left; public Node right; public Node(int data) { this.value = data; } } public static int nodeNum(Node head) { if (head == null) { return 0; } return bs(head, 1, mostLeftLevel(head, 1)); } public static int bs(Node node, int l, int h) { if (l == h) { return 1; } if (mostLeftLevel(node.right, l + 1) == h) { return (1 << (h - l)) + bs(node.right, l + 1, h); } else { return (1 << (h - l - 1)) + bs(node.left, l + 1, h); } } public static int mostLeftLevel(Node node, int level) { while (node != null) { level++; node = node.left; } return level - 1; } public static void main(String[] args) { Node head = new Node(1); head.left = new Node(2); head.right = new Node(3); head.left.left = new Node(4); head.left.right = new Node(5); head.right.left = new Node(6); System.out.println(nodeNum(head)); } }
2.通过先序和中序数组直接生成后序数组
public class PreAndInArrayToPosArray { public static int[] getPosArray(int[] pre, int[] in) { if (pre == null || in == null) { return null; } int len = pre.length; int[] pos = new int[len]; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < len; i++) { map.put(in[i], i); } setPos(pre, 0, len - 1, in, 0, len - 1, pos, len - 1, map); return pos; } public static int setPos(int[] p, int pi, int pj, int[] n, int ni, int nj, int[] s, int si, HashMap<Integer, Integer> map) { if (pi > pj) { return si; } s[si--] = p[pi]; int i = map.get(p[pi]); si = setPos(p, pj - nj + i + 1, pj, n, i + 1, nj, s, si, map); return setPos(p, pi + 1, pi + i - ni, n, ni, i - 1, s, si, map); } public static void main(String[] args) { int[] pre = { 1, 2, 4, 5, 3, 6, 7 }; int[] in = { 4, 2, 5, 1, 6, 3, 7 }; int[] pos = getPosArray(pre, in); for(int i=0;i<pos.length;i++) { System.out.println(pos[i]); } } }
3.先序,后序,中序两两结合重构二叉树
public class ErgtoTree { public static class Node { public int value; public Node left; public Node right; public Node(int data) { this.value = data; } } public static Node preintoTree(int[] pre,int[] in) { if (pre==null || in==null) { return null; } HashMap<Integer, Integer> hashMap = new HashMap<>(); for(int i=0;i<in.length;i++) { hashMap.put(in[i], i); } return prein(pre, 0, pre.length-1, in, 0, in.length-1, hashMap); } public static Node prein(int[] p,int pi,int pj,int[] n,int ni,int nj,HashMap<Integer, Integer> hashMap) { if (pi>pj) { return null; } Node root = new Node(p[pi]); int index = hashMap.get(p[pi]); root.left = prein(p, pi+1,pi+index-ni, n, ni,index-1, hashMap); root.right = prein(p, pi+index-ni+1, pj, n, index+1, nj, hashMap); return root; } public static void prePrint(Node root) { if (root==null) { return; } System.out.print(root.value+" "); prePrint(root.left); prePrint(root.right); } 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; } 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 main(String[] args) { 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 }; Node root1 = preintoTree(pre, in); prePrint(root1); System.out.println(); Node root2 = inPosToTree(in, pos); prePrint(root2); System.out.println(); Node root3 = prePosToTree(pre, pos); prePrint(root3); } }
相关文章推荐
- 用java写 java1,1,2,4,7,13,24,44算法
- Java与算法之(13) - 二叉搜索树
- java经典算法_018有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
- 13-02 Java 数组高级算法,Arrays类
- Java数据结构与算法(31) - ch13图(拓扑)
- java每日小算法(13)
- 在java中实现图形学中的椭圆算法
- 18位身份证号码校验码的算法(JAVA)
- 在java中实现图形学中的直线算法
- 在java语言计算数据库记录总数的简便算法
- java实现的18位身份证格式验证算法
- Java 算法
- RoyalAjax(13)------RequestWrapper.java
- 一个用Dijkstra算法实现的路由算法的java程序——1 GraphAdjList类
- 用java写二叉树的算法
- “摘要”算法,使用示例。(java, sha) [备忘]
- FIFO与LRU 算法实现(java)
- 24点算法的java代码
- 图像边缘检测(Canny 算法)的Java实现
- MD5 算法的java实现