/
qatree.cpp
executable file
·162 lines (123 loc) · 3.46 KB
/
qatree.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
#include "qatree.h"
bool QATree::IsAnswer(string qaText)
{
int key = 0;
bool isAnswer = false;
if (Search(qaText, key))
{
if (IsLeaf(key))
{
isAnswer = true;
}
}
return isAnswer;
}
bool QATree::CreateQuestionAnswer(string newQuestion, string newAnswer,
string alternativeQA)
{
int key = 0;
bool created = false;
if (root == NULL)
{
Insert(newQuestion, 0);
Insert(newAnswer, 1);
Insert(alternativeQA, -1);
created = true;
}
else
{
if (ScalingRequired(root))
{
nodeScale++;
ScaleNodes(root, nodeScale);
}
/* Search for the alternate answer. Store off the key if it is found */
if (Search(alternativeQA, key))
{
if (IsLeaf(key))
{
/* Replace the existing alternate answer with a new question*/
ReplaceInfo(key, newQuestion);
/* Insert the presumably correct answer to the right of the answer */
Insert(newAnswer, key + 1);
/* Insert the existing, alternative answer to the left of the answer */
Insert(alternativeQA, key - 1);
created = true;
}
else
cout << "Error: \"" << alternativeQA << "\" is a question." << endl;
}
else
cout << "Error: Unable to find answer: \"" << alternativeQA << "\"." << endl;
}
return created;
}
bool QATree::GetNextQA(string question, string &answer, int qaPath){
int parentKey;
int siblingKey;
if (Search(question, parentKey))
{
if (!IsLeaf(parentKey)){
if (qaPath == CORRECT_PATH)
Navigate(parentKey, answer, siblingKey, RIGHT_LINK);
else if (qaPath == INCORRECT_PATH)
Navigate(parentKey, answer, siblingKey, LEFT_LINK);
else
{
cout << "Error: Incorrect question/ answer path defined";
return false;
}
return true;
}
}
return false;
}
bool QATree::GetFirstQA(string &question){
bool found = false;
if (root != NULL)
{
question = root->info;
found = true;
}
return found;
}
QATree::QATree()
{
nodeScale = DEFAULT_SCALE;
}
QATree::~QATree()
{
}
istream & operator >>( istream & input, QATree & QA)
{
int key;
string line;
input >> key;
getline(input, line);
size_t p2 = line.find_last_not_of(' ');
if (p2 != std::string::npos)
{
size_t p1 = line.find_first_not_of(' ');
if (p1 == std::string::npos)
p1 = 0;
line = line.substr(p1, (p2-p1)+1);
}
QA.Insert(line, key);
return input;
}
ostream & operator <<(ostream & output, QATree & QA)
{
StringNode* currentNode = QA.root;
queue< StringNode* > q;
q.push(currentNode);
while (!q.empty()) {
currentNode = q.front();
q.pop();
if (currentNode->lLink != NULL)
q.push(currentNode->lLink);
if (currentNode->rLink != NULL)
q.push(currentNode->rLink);
output << currentNode->key << " " << currentNode->info << endl;
}
return output;
}