170 likes | 270 Views
Project 2 -- Analysis. void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */. Project 2 -- Analysis. void update(const String &word, Search_tree<Record> &structure, int &num_comps) /*
E N D
Project 2 -- Analysis void write_method() /* Post: A short string identifying the abstract data type used for the structure is written. */
Project 2 -- Analysis void update(const String &word, Search_tree<Record> &structure, int &num_comps) /* Post: If word was not already present in structure, then word has been inserted into structure and its frequency count is 1. If word was already present in structure, then its frequency count has been increased by 1. The variable parameter num_comps is set to the number of comparisons of words done. */
Project 2 -- Analysis void print(Search_tree<Record> &structure) /* Post: All words in structure are printed at the terminal in alphabetical order together with their frequency counts. */
Project 2 – Analysis – Record.h class Record { public: Record(); // default constructor operator Key() const; // cast to Key Record (const Key &a_name); // conversion to Record void increment(); int frequency(); private: Key name; int count; };
Project 2 -- Analysis – Key.h class Key { String key; public: static int comparisons; Key (String x = ""); String the_key() const; }; bool operator ==(const Key &x,const Key &y); bool operator >(const Key &x,const Key &y); bool operator <(const Key &x,const Key &y); bool operator >=(const Key &x,const Key &y); bool operator <=(const Key &x,const Key &y); bool operator !=(const Key &x,const Key &y);
Project 2 – Analysis – Node.h enum Balance_factor { left_higher, equal_height, right_higher }; template <class Entry> struct Binary_node { // data members: Entry data; Binary_node<Entry> *left; Binary_node<Entry> *right; // constructors: Binary_node(); Binary_node(const Entry &x); // virtual methods: virtual void set_balance(Balance_factor b); virtual Balance_factor get_balance() const; };
Project 2 – Analysis – Tree.h template <class Entry> class Binary_tree { public: // Add methods here. Binary_tree(); bool empty() const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); int size() const; void clear(); int height() const; void insert(const Entry &);
Project 2 – Analysis – Tree.h (cont) Binary_tree (const Binary_tree<Entry> &original); Binary_tree & operator =(const Binary_tree<Entry> &original); ~Binary_tree(); // tree testing commands Error_code remove(Entry &); void prenode(void (*f)(Binary_node<Entry> *&)); void swap(); void recursive_inorder(Binary_node<Entry> *, void (*visit)(Entry &)); void recursive_preorder(Binary_node<Entry> *, void (*visit)(Entry &)); void recursive_postorder(Binary_node<Entry> *, void (*visit)(Entry &)); void recursive_insert(Binary_node<Entry> *&sub_root, const Entry &x);
Project 2 – Analysis – Tree.h (cont) int recursive_size(Binary_node<Entry> *sub_root) const; int recursive_height(Binary_node<Entry> *sub_root) const; void recursive_clear(Binary_node<Entry> *&sub_root); Binary_node<Entry> *recursive_copy(Binary_node<Entry> *sub_root); void recursive_swap(Binary_node<Entry> *sub_root); Binary_node<Entry> *&find_node(Binary_node<Entry> *&, const Entry &) const; Error_code remove_root(Binary_node<Entry> *&sub_root); protected: // Add auxiliary function prototypes here. Binary_node<Entry> *root; };
Project 2 – Analysis – STree.h (cont) template <class Record> class Search_tree: public Binary_tree<Record> { public: Error_code insert(const Record &new_data); Error_code remove(const Record &old_data); Error_code tree_search(Record &target) const; Error_code search_and_insert(Binary_node<Record> *&, const Record &); Binary_node<Record> *search_for_node(Binary_node<Record>*, const Record&) const; Error_code search_and_destroy(Binary_node<Record> *&, const Record &); private: // Add auxiliary function prototypes here. };
Project 2 – Analysis – String.h class String { public: // methods of the string ADT String(); ~String(); String (const String ©); // copy constructor String (const char * copy); // conversion from C-string String (List<char> ©); // conversion from List void operator =(const String ©); const char *c_str() const; // conversion to C-style string protected: char *entries; int length; };
Project 2 – Analysis – String.h (cont) bool operator ==(const String &first, const String &second); bool operator >(const String &first, const String &second); bool operator <(const String &first, const String &second); bool operator >=(const String &first, const String &second); bool operator <=(const String &first, const String &second); bool operator !=(const String &first, const String &second);
Project 2 – Analysis – List.h template <class Node_entry> struct Node { // data members Node_entry entry; Node<Node_entry> *next; // constructors Node(); Node(Node_entry, Node<Node_entry> *link = NULL); };
Project 2 – Analysis – List.h (cont) template <class List_entry> class List { public: // Specifications for the methods of the list ADT go here. List(); int size() const; bool full() const; bool empty() const; void clear(); void traverse(void (*visit)(List_entry &)); Error_code retrieve(int position, List_entry &x) const; Error_code replace(int position, const List_entry &x); Error_code remove(int position, List_entry &x); Error_code insert(int position, const List_entry &x);
Project 2 – Analysis – List.h (cont) // The following methods replace compiler-generated defaults. ~List(); List(const List<List_entry> ©); void operator =(const List<List_entry> ©); protected: // Data members for the linked list implementation now follow. int count; Node<List_entry> *head; // The following auxiliary function is used to locate list positions Node<List_entry> *set_position(int position) const; };
Project 2 – Analysis – Timer.h (cont) #include <time.h> class Timer{ public: Timer(); double elapsed_time(); void reset(); private: clock_t start_time; };
Project 2 – Analysis – Timer.cpp Timer::Timer() { start_time = clock(); } double Timer::elapsed_time() { clock_t end_time = clock(); return ((double) (end_time - start_time)) / ((double) CLK_TCK); } void Timer::reset() { start_time = clock(); }