FileOps.h
#ifndef INC_10_THE_DISADVANTAGES_OF_BINARY_SEARCH_TREE_AND_MORE_TREES_FILEOPS_H
#define INC_10_THE_DISADVANTAGES_OF_BINARY_SEARCH_TREE_AND_MORE_TREES_FILEOPS_H
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <cassert>
using namespace std;
namespace FileOps{
bool isCharacter( char c ){
return ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' );
}
int firstCharacterIndex(const string& s, int start){
for( int i = start ; i < s.length() ; i ++ )
if( isCharacter(s[i]) )
return i;
return s.length();
}
string lowerS( const string& s){
string ret = "";
for( int i = 0 ; i < s.length() ; i ++ ){
assert( isCharacter(s[i]) );
ret += tolower(s[i]);
}
return ret;
}
bool readFile(const string& filename, vector<string> &words){
string line;
string contents = "";
ifstream file(filename);
if( file.is_open() ){
while( getline(file, line))
contents += ( line + "\n" );
file.close();
}
else{
cout<<"Can not open "<<filename<<" !!!"<<endl;
return false;
}
int start = firstCharacterIndex(contents, 0);
for( int i = start + 1 ; i <= contents.length() ; ){
if( i == contents.length() || !isalpha(contents[i]) ){
words.push_back( lowerS( contents.substr(start,i-start) ) );
start = firstCharacterIndex(contents, i);
i = start + 1;
}
else{
i ++;
}
}
return true;
}
}
#endif
SequenceST.h
#ifndef INC_10_THE_DISADVANTAGES_OF_BINARY_SEARCH_TREE_AND_MORE_TREES_SEQUENCEST_H
#define INC_10_THE_DISADVANTAGES_OF_BINARY_SEARCH_TREE_AND_MORE_TREES_SEQUENCEST_H
#include <iostream>
#include <cassert>
using namespace std;
template<typename Key, typename Value>
class SequenceST{
private:
struct Node{
Key key;
Value value;
Node *next;
Node(Key key, Value value){
this->key = key;
this->value = value;
this->next = NULL;
}
};
Node* head;
int count;
public:
SequenceST(){
head = NULL;
count = 0;
}
~SequenceST(){
while( head != NULL){
Node *node = head;
head = head->next;
delete node;
count --;
}
assert( head == NULL && count == 0 );
}
int size(){
return count;
}
bool isEmpty(){
return count == 0;
};
void insert(Key key, Value value){
Node *node = head;
while( node != NULL ){
if( key == node->key ){
node->value = value;
return;
}
node = node->next;
}
Node *newNode = new Node(key, value);
newNode->next = head;
head = newNode;
count ++;
}
bool contain(Key key){
Node *node = head;
while( node != NULL ){
if( key == node->key ){
return true;
}
node = node->next;
}
return false;
}
Value* search(Key key){
Node *node = head;
while( node != NULL ){
if( key == node->key ){
return &(node->value);
}
node = node->next;
}
return NULL;
}
void remove(Key key){
if( key == head->key ){
Node* delNode = head;
head = head->next;
delete delNode;
count--;
return;
}
Node *node = head;
while( node->next != NULL && node->next->key != key )
node = node->next;
if( node->next != NULL ){
Node* delNode = node->next;
node->next = delNode->next;
delete delNode;
count --;
return;
}
}
};
#endif
main.cpp
#include <iostream>
#include <vector>
#include <string>
#include "BST.h"
#include "SequenceST.h"
#include "FileOps.h"
using namespace std;
int main() {
string filename = "communist.txt";
vector<string> words;
if( FileOps::readFile(filename, words) ) {
cout << "There are totally " << words.size() << " words in " << filename << endl;
cout << endl;
// test BST
time_t startTime = clock();
BST<string, int> *bst = new BST<string, int>();
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*bst).search(*iter);
if (res == NULL)
(*bst).insert(*iter, 1);
else
(*res)++;
}
cout << "'unite' : " << *(*bst).search("unite") << endl;
time_t endTime = clock();
cout << "BST , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete bst;
// test SST
startTime = clock();
SequenceST<string, int> *sst = new SequenceST<string, int>();
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*sst).search(*iter);
if (res == NULL)
(*sst).insert(*iter, 1);
else
(*res)++;
}
cout << "'unite' : " << *(*sst).search("unite") << endl;
endTime = clock();
cout << "SST , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete sst;
// test BST2
startTime = clock();
BST<string, int> *bst2 = new BST<string, int>();
sort( words.begin() , words.end() );
for (vector<string>::iterator iter = words.begin(); iter != words.end(); iter++) {
int *res = (*bst2).search(*iter);
if (res == NULL)
(*bst2).insert(*iter, 1);
else
(*res)++;
}
cout << "'unite' : " << *(*bst2).search("unite") << endl;
endTime = clock();
cout << "BST2 , time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " s." << endl;
cout << endl;
delete bst2;
}
return 0;
}