public class Lx {
public static void main(String[] args) {
int[] n ={0,1,2,3,4,5,6,7,8,9};
int [] n2= getSequence(n);
for(int a:n){
System.out.print(a);
}
}
public static int[] getSequence(int[] no) {
int[] sequence = no;
Random random = new Random();
for(int i = 0; i < no.length; i++){
int p = random.nextInt(no.length);
int tmp = sequence[i];
sequence[i] = sequence[p];
sequence[p] = tmp;
}
random = null;
return sequence;
}
}作者: 彭嘉聪 时间: 2012-1-5 16:02
随机抽取0-9任一个元素K,利用a[k]来进行建立树,k-1为k的左子树,k+1为k的右子树。
建好后利用1-3随机数抽取三种遍历的方法,前序中序后序。
然后遍历输出。
用c实现会比较好。作者: 彭嘉聪 时间: 2012-1-5 16:05
k-1不妥,应该用随机数 k-i%M(数组长度),或者k+i%m作者: 胡家福 时间: 2012-1-5 16:26
public class Lx {
public static void main(String[] args) {
int[] n ={0,1,2,3,4,5,6,7,8,9};
//int [] n2= getSequence(n);
getSequence(n);
for(int a:n){
System.out.print(a);
}
}
public static void getSequence(int[] no) {
int[] sequence = no;
Random random = new Random();
for(int i = 0; i < no.length; i++){
int p = random.nextInt(no.length);
int tmp = sequence[i];
sequence[i] = sequence[p];
sequence[p] = tmp;
}
random = null;
//return sequence;
}
}作者: 胡家福 时间: 2012-1-6 10:17