-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_dictionary.cc
More file actions
79 lines (76 loc) · 1.71 KB
/
strings_dictionary.cc
File metadata and controls
79 lines (76 loc) · 1.71 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
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace std;
struct node {
public:
node (string k, int cnt)
: key(k),
count(cnt),
lchild(nullptr),
rchild(nullptr)
{}
void
insert (string k)
{
if (key.compare(k) == 0) {
count++;
} else if (key.compare(k) > 0) {
if (lchild) {
lchild->insert(k);
} else {
lchild = unique_ptr<struct node> (new struct node (k, 1));
}
} else {
if (rchild) {
rchild->insert (k);
} else {
rchild = unique_ptr<struct node> (new struct node (k, 1));
}
}
}
void
inorderTraversal ()
{
if (lchild) {
lchild->inorderTraversal();
}
cout << *this;
if (rchild) {
rchild->inorderTraversal();
}
}
friend std::ostream&
operator << (std::ostream &os, const struct node& n)
{
os << n.key << "::" << n.count << " ";
return os;
}
string key;
int count;
unique_ptr<struct node> lchild;
unique_ptr<struct node> rchild;
};
typedef struct node node;
int
main (int argc, char *argv[])
{
unique_ptr<node> root {nullptr};
string word;
string EOFILE = "EOF";
cout << "Enter the text. End it by writing \"EOF\"" << endl;
while (true) {
cin >> word;
if (word.compare(EOFILE) == 0) {
break;
}
if (root) {
root->insert(word);
} else {
root = unique_ptr<node> (new node (word, 1));
}
}
root->inorderTraversal ();
cout << endl;
}