孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

转自:http://blog.sina.com.cn/s/blog_537bb50a010000iu.html

续四:
====对1-300000个整数随机放到数组中,为测试做准备====
/*
 * @(#)RandomNum.java
 *
 * Title: This class is to define the RandomNum.
 * Description: This file is the assignment of Algorithms.
 * @copyright (c) OpenSource
 * @author Bobby Yang
 * @tester Bobby Yang
 * @version 1.00
 * @time 2005.11.04
 */

package rbtree;

import java.util.Random;

public class RandomNum {
    int[] A;
   
    //develop a array random number of double type
    void setRandom( ){
        int j = 0;
        double k;
        A = new int[300000];
        Random rand = new Random();
       
        for(int i = 1; i <= 300000; i++){
            if(i == 300000) System.out.println(i);
            do{
                k = rand.nextDouble();
                j = (int)((k * 1000000000) % 300000);
            }
            while((j == 0) || (j == 299999) || ((A[j] != 0) && (A[j - 1] != 0) && (A[j + 1] != 0)));
                if(A[j] == 0) A[j] = i;
                else if(A[j - 1] == 0) A[j - 1] = i;
                else if(A[j + 1] == 0) A[j + 1] = i;
           
        }
    }
}



    ====对红黑树的测试====
/*
 * @(#)Main.java
 *
 * Title: This class is to operate the RBtree.
 * Description: This file is the assignment of Algorithms.
 * @copyright (c) OpenSource
 * @author Bobby Yang
 * @tester Bobby Yang
 * @version 1.00
 * @time 2005.11.02
 */


package rbtree;

public class Main {
    public static void main(String[] args){
        Rbtree rbtree = new Rbtree();
        int[] a = {13, 8, 11, 17, 15, 6, 1, 22, 25, 27};
        for(int i = 0; i < a.length; i++){
            rbtree.rbInsert(a[i]);
        }
        rbtree.preOrder();
       
        rbtree.rbDelete(27);
       
        System.out.println();
   
        rbtree.preOrder();
       
        rbtree.rbDelete(25);
        System.out.println();
   
        rbtree.preOrder();
       
        System.out.println();
       
//        compute the data from 1 to 300000;
        RandomNum rand = new RandomNum();
        System.out.println("Please waiting......., it builting the Array.");
        rand.setRandom();
        System.out.println("Please waiting......., it builting the tree.");
        Rbtree randTree = new Rbtree();
        for(int i = 0; i < 300000; i++){
            randTree.rbInsert(rand.A[i]);
        }
       
        long t1 = System.currentTimeMillis();
        Node node = randTree.search(123456);
        t1 = System.currentTimeMillis() - t1;
        if(node.iData == 123456) System.out.println("You got the node with key = 123456,it cost " + t1 + "ms.");
        else System.out.println("You did not get the node with key = 123456");
   
    }
   
}

posted on 2012-05-12 21:05  孤独的猫  阅读(183)  评论(0编辑  收藏  举报