求解递归算法的时间复杂度
P99 【算法分析】推导:
T(n)=1 当 n=1 时
T(n)=T(n/2)+1 当 n>1 时
T(n)
=T(n/2)+1
=T(n/22)+1+1
=T(n/23)+1+1+1
=…
=T(n/2k)+k
当n=1时,T(n)=1
等价于 T(1)=1
即:令 n/2k=1
得:T(n)=T(1)
+log2n=1
+log2n
可推出 T(n)=O(log2n)
2
T(n)=O(1) 当 n=1 时
T(n)=2T(n/2)+n 当 n>1 时
T(n)
=2T(n/2)+n
=22T(n/22)+n+n/2
=23T(n/23)+n+n/2+n/22
=…
=2kT(n/2k)+n+n/2+n/22+…+n/2k-1
=2kT(n/2k)+2n
当n=1时,T(n)=O(1)
等价于 T(1)=O(1)
即:令 n/2k=1
得:T(n)=nT(1)
+2n=n
+2n=3n
可推出T(n)=O(n)
扩展:
n+n/2+n/22+…+n/2k-1
=n(1+1/2+1/22+…+1/2k-1)
等比数列求和公式
Sn=a1(1-qn) / 1-q
=n( 1[ 1 - (1/2
)k )]/ 1-1/2
)
=2n[1 - (1/2
)k ]
=> limk->∞2n[1 - (1/2
)k ]=2n(1-0)=2n
3
T(n)=O(1) 当 n=1 时
T(n)=4T(n-1) 当 n>1 时
T(n)
=4T(n-1)
=42T(n-2)
=43T(n-3)
=…
=4kT(n-k)
当n=1时,T(n)=O(1)
等价于 T(1)=O(1)
即:令 n-k=1
得:T(n)=4n-1T(1)=4n-1
可推出T(n)=O(4n-1)
java
二叉树
package imitation;
public class post {
//由后序中序,生成先序
//由后序 post 中序 in 得到二叉树的根节点
public static TreeNode generateBTree(String post, String in) {
TreeNode treeNode = new TreeNode(post.charAt(post.length()-1));
if (post.length() == 1) {
return treeNode;
}
String[] str = disassemble(post, in);//返回后序左子树、后序右子树、中序左子树、中序右子树
//递归左子树
if (str[2] != null && str[2].length() != 0) {
treeNode.lNode = generateBTree(str[0], str[2]);
}
//递归右子树
if (str[3] != null && str[3].length() != 0) {
treeNode.rNode = generateBTree(str[1], str[3]);
}
return treeNode;
}
// 根据先序pre,中序in 得出该节点的
// 先序: 左子树result[0] 右子树result[1]
// 中序: 左子树result[2] 右子树result[3]
public static String[] disassemble(String post, String in) {
String[] result = new String[4];
if (post.length() == 1) {
return result;
}
//1. 由后序确定root结点
String root = String.valueOf(post.charAt(post.length()-1));//拿到post序遍历的最后一个值,将其转换为String类型
//2. 由中序结合root结点,即可以找到左右子树,亦可求出左右子树的长度。
result[2] = in.split(root)[0];
result[3] = in.split(root)[1];//代替下面代码
// if (in.split(root).length == 1) {
// result[3] = "";
// } else {
// result[3] = in.split(root)[1];
// }
//3. 在中序中,求左子树长度
int lLength = result[2].length();
//4. 根据长度 在后序中截取左右子树????
result[0] = post.substring(0, lLength);//从0开始截取,lLength-1结束
result[1] = post.substring(lLength,post.length()-1);
return result;
}
// 传入二叉树的根节点,先序遍历输出节点的值
public static void preTraversal(TreeNode rootNode) {
//根节点root
System.out.print(rootNode.value);
//左子树
if (rootNode.lNode != null) {
preTraversal(rootNode.lNode);
}
//右子树
if (rootNode.rNode != null) {
preTraversal(rootNode.rNode);
}
}
public static void main(String[] args) {
TreeNode rootNode = generateBTree("342651", "324156");
preTraversal(rootNode);
System.out.println();
System.out.println("height:"+height(rootNode));
System.out.println("leafNodes:"+leafNodes(rootNode));
System.out.println("所有元素值的累加和:"+traversalValues(rootNode));
}
//二叉树深度
public static int height(TreeNode t) {
if(t==null) {
return 0;
}else {
int l = height(t.lNode);
int r = height(t.rNode);
return l>r?l+1:r+1;
}
}
//叶子节点数
public static int leafNodes(TreeNode t) {
if(t==null) {
return 0;
}else if(t.lNode==null&&t.rNode==null){
return 1;
}else {
int l=leafNodes(t.lNode);
int r=leafNodes(t.rNode);
return l+r;
}
}
//所有元素值的累加和
public static int traversalValues(TreeNode t) {
int sum = t.value-'0';
int l=0,r=0;
//左子树
if (t.lNode != null) {
l=Integer.valueOf(traversalValues(t.lNode));
}
//右子树
if (t.rNode != null) {
r=Integer.valueOf(traversalValues(t.rNode));
}
return sum+l+r;
}
}
class TreeNode {
public char value;
public TreeNode lNode;
public TreeNode rNode;
public TreeNode(char value) {
this.value = value;
}
}