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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct Display
{
string words;;
int count;
};
struct Node
{
Display value;
Node* pLeft;
Node* pRight;
};
Node* add(Node* tree, Display value);
Node* addNode(Node* tree, Node* toAdd);
bool treeContains(Node* tree, string word) ;
void displayAlphabetTree(Node* tree);
void main()
{
Display value;
string choiceStr;
char choice;
bool eofReached = false;
value.count = 0;
Node* tree = 0;
string txtFile;
cout << "Enter filename: ";
cin >> txtFile;
cout << endl;
ifstream read(txtFile.c_str());
if(!read)
{
cout << "Error, can't open file" << endl;
exit(1);
}
else
{
while(!read.eof())
{
read >> value.words;
eofReached = read.eof();
if (treeContains(tree, value.words) == false)
{
tree = add(tree, value);
}
}
}
read.close();
do
{
cout << " a - display alphabetic list" << endl;
cout << " f - display frequency ordered list" << endl;
cout << " s - display statistics" << endl;
cout << " x - exit" << endl;
cout << endl;
cout << "Enter choice: ";
cin >> choiceStr;
choice = choiceStr.length() == 0 ? choice = 'e' : choice = choiceStr[0];
switch(choice)
{
case 'a':
case 'A':
displayAlphabetTree(tree);
break;
case 'x':
case 'X':
break;
default:
cout << "Error, invalid choice";
break;
}
}
while(choice != 'x');
}
Node* add(Node* tree, Display value)
{
//tempPtr is a temporary pointer
Node* tempPtr = new Node;
tempPtr->value = value;
tempPtr->pLeft = 0;
tempPtr->pRight = 0;
return addNode(tree, tempPtr);
}
Node* addNode(Node* tree, Node* toAdd)
{
if (tree == 0)
{
return toAdd;
}
else
{
if(toAdd->value.words < tree->value.words)
{
tree->pLeft = addNode(tree->pLeft, toAdd);
return tree;
}
else if(toAdd->value.words == tree->value.words)
{
tree->value.count++;
return tree;
}
else
{
tree->pRight = addNode(tree->pRight, toAdd);
return tree;
}
}
}
bool treeContains(Node* tree, string word)
{
// If a word is in the binary
// tree then true is returned.
// Vice versia, false is returned if not.
if (tree == NULL)
{
// Tree is empty, so it certainly doesn't contain item.
return false;
}
else if (word == tree->value.words)
{
//The word matches to one in the root node.
return true;
}
else if (word < tree->value.words)
{
// The word is less than the one in the root node
// and must be sent to the left subtree.
return treeContains(tree->pLeft, word);
}
else
{
// The word is more than the one in the root node
// and must be sent to the right subtree.
return treeContains(tree->pRight, word);
}
}
void displayAlphabetTree(Node* tree)
{
if(tree != 0)
{
displayAlphabetTree(tree->pLeft);
displayAlphabetTree(tree->pRight);
cout<<tree->value.words<<" "<<"["<<tree->value.count<<"]"<<endl;
}
}
| |