-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexicon.cpp
164 lines (150 loc) · 3.77 KB
/
lexicon.cpp
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
#include <iostream>
#include <string>
using namespace std;
class lexicon {
public:
lexicon() {
root = NULL;
}
~lexicon() {};
void insert(const string &s) {
root = InsertNode(root, s);
}
int lookup(const string &s) const {
node *p = SearchNode(root, s);
if (p != NULL) {
return p->count;
}
else {
return 0;
}
}
int depth(const string &s) {
int num = lookup(s);
if (num > 0) {
int d = NodeDepth(root, s);
return d;
}
else {
return -1;
}
}
void replace(const string &s1, const string &s2) {
if (lookup(s1) > 0) {
int count1 = 0;
node *r1 = SearchNode(root, s1);
count1 = r1->count;
r1->count = 0;
root = DeleteNode(root, s1);
if (lookup(s2) == 0) {
insert(s2);
node *r2 = SearchNode(root, s2);
r2->count = count1;
}
else {
node *r2 = SearchNode(root, s2);
r2->count += count1;
}
}
}
friend ostream & operator << (ostream &out, const lexicon &l) {
l.inOrder(l.root);
return out;
}
private:
struct node {
string data;
node *left, *right;
int count;
node (const string &s): data(s), left(NULL), right(NULL), count(1){}
};
node *root;
static node* InsertNode (node* r, const string &s) {
if (r == NULL) {
return new node(s);
}
if (s == r->data) {
++(r->count);
r->data = s;
}
else if (s < r->data) {
r->left = InsertNode(r->left, s);
}
else {
r->right = InsertNode(r->right, s);
}
return r;
}
static node* SearchNode (node *r, const string &s) {
if (r == NULL) {
return NULL;
}
if (r->data == s) {
return r;
}
if (s < r->data) {
return SearchNode(r->left, s);
}
else {
return SearchNode(r->right, s);
}
}
static int NodeDepth (node *r, const string &s) {
if (r == NULL) {
return 0;
}
if (r->data > s) {
return 1 + NodeDepth(r->left, s);
}
else if (r->data < s) {
return 1 + NodeDepth(r->right, s);
}
else {
return 0;
}
}
static node* minValueNode(node *r) {
node *current = r;
while (current && current->left != NULL) {
current = current->left;
}
return current;
}
static node* DeleteNode (node *r, const string &s) {
if (r == NULL) {
return r;
}
if (s < r->data) {
r->left = DeleteNode(r->left, s);
}
else if (s > r->data) {
r->right = DeleteNode(r->right, s);
}
else {
if ((r->left == NULL) && (r->right == NULL)) {
return NULL;
}
else if (r->left == NULL) {
node *temp = r->right;
delete r;
return temp;
}
else if (r->right == NULL) {
node *temp = r->left;
delete r;
return temp;
}
node *temp = minValueNode(r->left);
r->data = temp->data;
r->count = temp->count;
r->left = DeleteNode(r->left, r->data);
}
return r;
}
static void inOrder(const node *r) {
if (r == NULL) return;
inOrder(r->left);
cout << r->data << " " << r->count << endl;
inOrder(r->right);
}
};