/* main.cpp Program to read in strings from a file and insert them into a dictionary. Date: October 29, 2001 Flags: -b binary search tree -r AVL tree (recursive) -i AVL tree (iterative) (for BONUS problem) -s Splay tree -h hash function (quadratic probing) */ #include #include #include "mystring.h" #include "AvlTree.h" #include "BinarySearchTree.h" #include "SplayTree.h" #include "QuadraticProbing.h" void main(int argc, char * argv[]) { bool error = false; // timer variables int total_time = 0; int start_time = 0; int finish_time = 0; int num_insertions = 0; // An enum type to identify the // data structure/algorithm to use // (based on the command line argument) enum algorithm { NOALG = 0, useBST = 1, useAVL_recursive = 2, useAVL_iterative = 3, useSplay = 4, useHashTable = 5 } ; algorithm which_algorithm = NOALG; ifstream infile_stream; const string ITEM_NOT_FOUND (""); AvlTree avltree(ITEM_NOT_FOUND); BinarySearchTree bst(ITEM_NOT_FOUND); SplayTree splaytree(ITEM_NOT_FOUND); HashTable hashtable(ITEM_NOT_FOUND); string str; // Handle command line arguments. // Usage: argv[0] -[brish] input_filename output_filename // Options: // -b use a standard binary search tree // -r use the recursive implementation of an AVL tree // -i use the iterative implementation of an AVL tree // -s splay tree // -h hash function (quadratic probing) if ((argc < 3) || (argc > 3)) { cout << "Usage: " << argv[0] << " -[brish] infile" << endl; error = true; } else { // figure out which option was chosen if (argv[1][0] == '-') { switch (argv[1][1]) { case 'b': which_algorithm = useBST; break; case 'r': which_algorithm = useAVL_recursive; break; case 'i': which_algorithm = useAVL_iterative; break; case 's': which_algorithm = useSplay; break; case 'h': which_algorithm = useHashTable; break; default: cout << "Usage: "; cout << "-" << argv[1][1] << " is not a valid option." << endl; error = true; break; } // Get the input filename infile_stream.open(argv[2]); if (!infile_stream) { cout << "Error: Could not open " << argv[2] << "." << endl; error = true; } } else { cout << "Usage: " << argv[0] << " -[brish] filename" << endl; error = true; } } if (!error) { // start the timer start_time = clock(); while (getword(infile_stream, str)) { switch (which_algorithm) { case useBST: bst.insert(str); num_insertions++; break; case useAVL_recursive: avltree.insert(str); num_insertions++; break; case useAVL_iterative: avltree.insert_iter(str); num_insertions++; break; case useSplay: splaytree.insert(str); num_insertions++; break; case useHashTable: hashtable.insert(str); num_insertions++; break; } } // stop the timer finish_time = clock(); total_time += (finish_time - start_time); cout << "Processor time for insertions: "; cout << total_time * 1000 / CLOCKS_PER_SEC << " ms." << endl; cout << "Number of insertions: " << num_insertions << endl; } // Close the file stream. if (infile_stream) { infile_stream.close(); } cout << argv[0] << " is done." << endl; }