博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java与算法(13)
阅读量:2441 次
发布时间:2019-05-10

本文共 4271 字,大约阅读时间需要 14 分钟。

 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
map = new HashMap
(); 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
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

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
hashMap = new HashMap<>(); for(int i=0;i
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
map = new HashMap
(); 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
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
map = new HashMap
(); 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
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); }}

转载地址:http://pvcqb.baihongyu.com/

你可能感兴趣的文章
安全使用RedHat Linux系统(转)
查看>>
RedHat Enterprise AS4硬盘安装步骤(转)
查看>>
全国第一个高校Linux培训考试中心建立(转)
查看>>
Linux黑客大曝光:Linux安全机密与解决方案(转)
查看>>
关于Kerberos安装的几个问题(转)
查看>>
Solaris硬盘分区简介(转)
查看>>
gcc编译器小知识FAQ(转)
查看>>
Linux下多线程编程与信号处理易疏忽的一个例子(转)
查看>>
流氓和木马结合 强行关闭你的防火墙(转)
查看>>
SUSE一纸诉状控告SCO 捍卫知识产权(转)
查看>>
debian下编译2.6.13.2内核的步骤及感受(转)
查看>>
预装正版的市场意义(转)
查看>>
创建小于16M XFree86迷你Linux系统(转)
查看>>
shell中常用的工具(转)
查看>>
使用MySQL内建复制功能来最佳化可用性(转)
查看>>
一个比较vista的vista主题for rf5.0fb(转)
查看>>
推荐一款 Linux 上比较漂亮的字体(转)
查看>>
在Linux中添加新的系统调用(转)
查看>>
Fedora Core 5.0 安装教程{下载}(转)
查看>>
把ACCESS的数据导入到Mysql中(转)
查看>>