日期:2014-05-20  浏览次数:20667 次

2分木组合
作业给出代码组合,求100个节点 2分木的高。

请求帮助。

Java code

public class classexample {
    static final int NODE_NUM=10;
    static final int RAND_SEED= 0x1031000;
        /*加入定义和必要的*/
    public static void main(String[] args){
        BST_Node T_root;
        Random rnd=new Random(RAND_SEED);
        T_root=new BST_Node();
        T_root.value=0;    T_root.right=T_root.left=null;
        for(int i=0; i<NODE_NUM;i++){
            insert_v(T_root,rnd.nextInt(NODE_NUM*10));
        }
        /*加入木的高和木的形式表示*/
    }
}


定义 代码

public class BST_Node{
int value;
BST_Node left;
BST_Node right;
}
木高求法

public static int compute_height(BST_Node p){
int lh=0, rh=0;
if(p==null){return 0;}
lh=compute_height(p.left);
rh=compute_height(p.right);
return max(lh,rh)+1;
}
木行的表示
public static void tree_shape(BST_Node p){
if(p==null){
return;
}
System.out.print("(");
if(p.left!=null){ tree_shape(p.left); }
System.out.print(p.value);
if(p.right!=null){ tree_shape(p.right); }
System.out.print(")");
}
节点追加法

public static void insert_n(BST_Node p, BST_Node x){
if(x==null){ return; }
if(x.value < p.value){
if(p.left==null){ p.left=x; }
else { insert_n(p.left, x); }
} else if( p.value < x.value){
if(p.right==null){ p.right=x; } 
else { insert_n(p.right,x); }


}


------解决方案--------------------
给你个简单的
public class Test {
public static void main(String[] args) {
int loop=0;
int total=0;
for(int i=0;i<100;i++){
total += cifang(i);
loop++;
if(total>=100)break;
}
System.out.println(loop);
}
static int cifang(int n){
int sum =1;
if(n==0)sum = 1;
if(n==1)sum = 2;
if(n>=2)
for(int i=0;i<n;i++){
sum = sum*2;
}
return sum;
}
}
------解决方案--------------------
#include<stdio.h>
int main(void) {
double a[7]={1.0,1.5,2.0,2.5,3.0,3.5,4.0};
double *pt=&a[0];
int i;
for(i=0;i<7;i++){
printf("a[%d]=%.2f/n",i,pt[i]);
}
getchar();
return(0);
}
简单的例子理解下,改改。