Search in sources :

Example 1 with RedBlackBST

use of chapter3.section3.RedBlackBST in project algorithms-sedgewick-wayne by reneargento.

the class Exercise35_PrimitiveTypes method doExperiment.

private void doExperiment(int[] numberOfSizesAndQueries, int[] randomIntKeys, double[] randomDoubleKeys, int[] queriesInt, double[] queriesDouble) {
    StdOut.printf("%17s %12s %20s %10s\n", "Symbol table | ", "Key type | ", "Size and queries | ", "Time spent");
    // Linear probing hash table with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Integer, Integer> linearProbingHashTableInteger = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTint<Integer> linearProbingHashTableint = new Exercise4().new HashSTint<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        LinearProbingHashTable<Double, Double> linearProbingHashTableDouble = new LinearProbingHashTable<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTableDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Linear probing hash table with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise4.HashSTdouble<Double> linearProbingHashTabledouble = new Exercise4().new HashSTdouble<>(997);
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            linearProbingHashTabledouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(LINEAR_PROBING_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Integer keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Integer, Integer> redBlackBSTInteger = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTInteger.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, INTEGER_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with int keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STint<Integer> redBlackBSTint = new Exercise5().new STint<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.put(randomIntKeys[j], randomIntKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTint.get(queriesInt[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_INT_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with Double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        RedBlackBST<Double, Double> redBlackBSTDouble = new RedBlackBST<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTDouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
    // Red-black BST with double keys test
    for (int i = 0; i < numberOfSizesAndQueries.length; i++) {
        Exercise5.STdouble<Double> redBlackBSTdouble = new Exercise5().new STdouble<>();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.put(randomDoubleKeys[j], randomDoubleKeys[j]);
        }
        Stopwatch stopwatch = new Stopwatch();
        for (int j = 0; j < numberOfSizesAndQueries[i]; j++) {
            redBlackBSTdouble.get(queriesDouble[j]);
        }
        double timeSpent = stopwatch.elapsedTime();
        printResults(RED_BLACK_BST_SYMBOL_TABLE_TYPE, PRIMITIVE_DOUBLE_KEY_TYPE, numberOfSizesAndQueries[i], timeSpent);
    }
}
Also used : Stopwatch(edu.princeton.cs.algs4.Stopwatch) LinearProbingHashTable(chapter3.section4.LinearProbingHashTable) RedBlackBST(chapter3.section3.RedBlackBST)

Example 2 with RedBlackBST

use of chapter3.section3.RedBlackBST in project algorithms-sedgewick-wayne by reneargento.

the class Exercise13 method rangeLookupCSV.

// Parameters example: 0: csv_file.txt
// 1: 0
// 2: 1
// Queries: arnold fzkey
// rachel wrong
// Output expected:
// arnold 200
// dijkstra 10
// dwayne 201
// fenwick 202
// 
// rene 5
// sedgewick 9
// wayne 10
private void rangeLookupCSV(String[] args) {
    String filePath = Constants.FILES_PATH + args[0];
    In in = new In(filePath);
    int keyField = Integer.parseInt(args[1]);
    int valueField = Integer.parseInt(args[2]);
    RedBlackBST<String, String> symbolTable = new RedBlackBST<>();
    while (in.hasNextLine()) {
        String line = in.readLine();
        String[] tokens = line.split(",");
        String key = tokens[keyField];
        String value = tokens[valueField];
        symbolTable.put(key, value);
    }
    while (!StdIn.isEmpty()) {
        String queryKey1 = StdIn.readString();
        String queryKey2 = StdIn.readString();
        for (String key : symbolTable.keys(queryKey1, queryKey2)) {
            StdOut.println(key + " " + symbolTable.get(key));
        }
        StdOut.println();
    }
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In) RedBlackBST(chapter3.section3.RedBlackBST)

Example 3 with RedBlackBST

use of chapter3.section3.RedBlackBST in project algorithms-sedgewick-wayne by reneargento.

the class Exercise12 method lookupCSV.

// Parameters example: 0: csv_file.txt
// 1: 0
// 2: 1
// Queries: rene
// sedgewick
// wayne
// Output expected:
// rene
// 1 3 5
// sedgewick
// 9
// wayne
// 9 10
private void lookupCSV(String[] args) {
    String filePath = Constants.FILES_PATH + args[0];
    In in = new In(filePath);
    int keyField = Integer.parseInt(args[1]);
    int valueField = Integer.parseInt(args[2]);
    RedBlackBST<String, List<String>> symbolTable = new RedBlackBST<>();
    while (in.hasNextLine()) {
        String line = in.readLine();
        String[] tokens = line.split(",");
        String key = tokens[keyField];
        String value = tokens[valueField];
        if (!symbolTable.contains(key)) {
            symbolTable.put(key, new ArrayList<>());
        }
        symbolTable.get(key).add(value);
    }
    while (!StdIn.isEmpty()) {
        String query = StdIn.readString();
        boolean isFirstValue = true;
        if (symbolTable.contains(query)) {
            for (String value : symbolTable.get(query)) {
                if (isFirstValue) {
                    isFirstValue = false;
                } else {
                    StdOut.print(" ");
                }
                StdOut.print(value);
            }
            StdOut.println();
        }
    }
}
Also used : StdIn(edu.princeton.cs.algs4.StdIn) In(edu.princeton.cs.algs4.In) List(java.util.List) ArrayList(java.util.ArrayList) RedBlackBST(chapter3.section3.RedBlackBST)

Aggregations

RedBlackBST (chapter3.section3.RedBlackBST)3 In (edu.princeton.cs.algs4.In)2 StdIn (edu.princeton.cs.algs4.StdIn)2 LinearProbingHashTable (chapter3.section4.LinearProbingHashTable)1 Stopwatch (edu.princeton.cs.algs4.Stopwatch)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1