背景:我正在编写一个程序,该程序接收数据列表并将其解析为二进制搜索树。该数据的密钥为“字符串”类型。我正在编写一个布尔函数,如果要添加的数据的键在引用节点中的数据的键之前,则返回“ true”,如果新的键取代旧的,则返回“ false”,然后递归地运行如果所讨论的字符相等,则该函数递增一个位置为ASCII值。
目前看来,我的比较是正确的,并且似乎可以正确递归,但是在所有情况下都返回false。
mBST.cpp
class mBST {
private:
struct mNode {
movie M;
string k;
mNode* left;
mNode* right;
};
mNode *root;
mNode *insert(movie m, mNode *t) {
bool rec;
if (t == NULL) {
t = new mNode();
t->M = m;
t->k = m.getTitle();
t->left = NULL;
t->right = NULL;
} else {
if(abcOrder(m, t, 0)){
t->left = insert(m, t->left);
}
else {
t->right = insert(m, t->right);
}
}
return t;
};
mNode *remove(movie m, mNode *t);
mNode *search(string k, mNode *t);
mNode *min(mNode *t);
mNode *max(mNode *t);
void inorder(mNode *t);
public:
mBST() {
root = NULL;
}
void insert(movie m) {
root = insert(m, root);
};
void remove(movie m);
void search(string k);
void inorder();
//Initial call passes i = 0
bool abcOrder(movie m, mNode *ref, int i) {
int data = NULL;
//Gets key data and converts to lowercase as needed
int newNode = m.getTitle().at(i);
if (newNode < 97)
newNode = newNode + 32;
int refNode = ref->M.getTitle().at(i);
if (refNode < 97)
refNode = refNode + 32;
//Compares key data at position 'i'
if (newNode < refNode) {
return true;
} else if (newNode > refNode) {
return false;
} else {
//If newNode.key == refNode.key, increment position and run again
i++;
abcOrder(m, ref, i);
}
}
};
MoviePeopleBST.cpp
int main()
{
ifstream inFile;
ofstream outFile;
mBST movieBST;
pBST peopleBST;
inFile.open("movies.txt");
//Reads in movies text file and sends to BST
while (!inFile.eof()) {
string junk;
string sTemp;
movie temp;
int iTemp;
getline(inFile, sTemp);
if (sTemp != "") {
temp.setTitle(sTemp);
inFile >> iTemp;
temp.setYear(iTemp);
getline(inFile, junk);
inFile >> iTemp;
temp.setRunTm(iTemp);
getline(inFile, junk);
getline(inFile, sTemp);
temp.setRating(sTemp);
getline(inFile, sTemp);
temp.setAspect(sTemp);
getline(inFile, sTemp);
temp.setColor(sTemp);
movieBST.insert(temp);
//movieVector.push_back(temp);
}
}
inFile.close();
/*inFile.open("people.txt");
//Reads in people text file and sends to BST
while (!inFile.eof()) {
string junk;
string sTemp;
people temp;
int iTemp;
getline(inFile, sTemp);
if (sTemp != "") {
temp.setFirst(sTemp);
getline(inFile, sTemp);
temp.setLast(sTemp);
inFile >> iTemp;
temp.setbYear(iTemp);
getline(inFile, junk);
inFile >> iTemp;
temp.setdYear(iTemp);
getline(inFile, junk);
getline(inFile, sTemp);
temp.setGender(sTemp);
peopleBST.insert(temp);
//peopleVector.push_back(temp);
}
}
inFile.close();*/
return 0;
}
文本文件作为要插入的数据:
laak
4
5
guide
payment
exclusive
laab
5
4
impartial
sigh
stage
laaj
9
8
change
cannon
wound
laal
8
7
acid
help
tickle
给出以下键: 1. ak 2. laab 3.拉杰 4. laal
我们应该看到以下输出:
true(1剩下2)
true,然后为false(1的左侧为3,2的右侧)
false(k在1的右边)
实际上返回的都是假,其中4是3的右子,3是2的右,而2是1的右。
注意:尽管我的插入字符方法具有唯一的第一个字符,但在实现此功能之前,该方法正常工作。
答案 0 :(得分:1)
production.rb
...