forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_test.cpp
More file actions
84 lines (80 loc) · 3.02 KB
/
Copy pathtree_test.cpp
File metadata and controls
84 lines (80 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>
#include <fstream>
#include <assert.h>
#include <stdlib.h>
#include "binarysearchtree.h"
#include "avltree.h"
using namespace std;
void print_stats(BinarySearchTree * tree, AvlTree * avl_tree) {
cout << "BST: " << endl;
cout << "Left links followed = " << tree->GetLeftLinksFollowed() << endl;
cout << "Right links followed = " << tree->GetRightLinksFollowed() << endl;
cout << "Total number of nodes = " << tree->card_of() << endl;
cout << "Avg. node depth = " << tree->exp_path_length() << endl << endl;
cout << "AVL Tree: " << endl;
cout << "Left links followed = " << avl_tree->GetLeftLinksFollowed() << endl;
cout << "Right links followed = " << avl_tree->GetRightLinksFollowed() << endl;
cout << "Total number of nodes = " << avl_tree->card_of() << endl;
cout << "Single Rotations = " << avl_tree->GetSingleRotations() << endl;
cout << "Double Rotations = " << avl_tree->GetDoubleRotations() << endl;
cout << "Avg. node depth = " << avl_tree->exp_path_length() << endl << endl;
}
int main () {
BinarySearchTree * tree;
AvlTree * avl_tree;
string word;
string inFile;
const string ITEM_NOT_FOUND("");
int i;
int n=0;
int num_words=0;
string word_found; // return value for tree->find()
string avl_word_found; // return value for tree->find()
string response;
tree = new BinarySearchTree(ITEM_NOT_FOUND);
avl_tree = new AvlTree(ITEM_NOT_FOUND);
cout << "Please enter the name of a file of words: ";
cin >> inFile;
ifstream fin(inFile.c_str());
if (!fin) {
cerr <<"Cannot open " << inFile << endl;
exit(1);
}
while (fin >> word) {
n = int (word.size());
assert(n > 0);
for (i=0; i < n; ++i) { // Convert to lowercase.
word[i] = tolower(word[i]);
}
if (!isalpha(word[n - 1])) { // eliminate trailing punctuation
word = word.substr(0, n-1);
}
// Echo the contents the words that have been read to the screen.
cout << " " << word;
num_words++;
tree->insert(word);
avl_tree->insert(word);
}
cout << "\n" << num_words << " words in this text\n";
cout << endl << endl;
// Print out Statistics about Shape of the trees
// and number of links followed.
print_stats(tree, avl_tree);
// Find a word in both trees.
cout << "\tEnter word to lookup > ";
cin >> response;
word_found = tree->find(response);
avl_word_found = avl_tree->find(response);
if (word_found != ITEM_NOT_FOUND && avl_word_found != ITEM_NOT_FOUND) {
cout << "\tWord was found: " << word_found << endl;
} else {
cout << response << " was not found in "
<< inFile.c_str() << endl;
}
// Print out Statistics about Shape of the trees
// and number of links followed.
print_stats(tree, avl_tree);
fin.close();
return 0;
}