我目前正在使用二叉搜索树处理电影数据库项目。树中的每个节点都包含电影的标题,电影制作的年份以及包含所有演员姓名的链接列表。这些信息都是从.txt文件中读取的。
我已经创建了一些功能,但是我遇到了麻烦。我正在尝试创建一个函数,使用户能够输入一个字符串,该字符串将是actor的名字和姓氏,然后遍历树节点。如果在节点内的链接列表中找到了actor名称,则该函数将打印出该影片的标题。
我以前从未使用过STL,所以我不知道访问元素是否与手动创建列表相同,这就是我遇到问题的原因。
任何帮助将不胜感激。到目前为止,这是我的代码:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <fstream>
#include <list>
using namespace std;
typedef struct treeNode{
int year;
string title;
list<string> actors;
treeNode* left;
treeNode* right;
}* treePtr;
treePtr root = NULL;
treePtr fillTree(treePtr p, int y, string t, list<string> a);
void print_titles(treePtr root);
void print_before_year(treePtr root, int key);
void print_actors_movies(???);
int main(){
ifstream inFile ("movies.txt");
string x;
treePtr root = NULL;
int count = 0;
if(inFile.is_open()){
while(getline (inFile, x)){
if(x == ""){
continue;
}
count++;
int index = x.find_last_of(" ");
string title = x.substr(0, index);
string year = x.substr(index + 2, x.length() - index - 3);
list<string> actor;
int counter = 0;
while(getline(inFile, x)){
if(x == ""){
break;
}
else{
actor.push_back(x);
}
}
root = fillTree(root, atoi(year.c_str()), title, actor);
}
}
int choice;
do{
cout <<"\nWelcome to the Movie Store. Would you like to: \n(1)
See what movies are available? \n(2) Search for an actor? \n(3)
Search for a year? \n(4) Search for a movie? \n(0) Exit the Store" << endl;
cin >> choice;
switch(choice){
case 0:
cout << "Thank you come again." << endl;
break;
case 1:
print_titles(root);
break;
case 2:
case 3:
int year;
cout << "Please enter the year you wish to search for: " << endl;
cin >> year;
cout << endl;
cout << "Films made before " << year << ":" << endl;
print_before_year(root, year);
break;
case 4:
default:
cout << "Try again." << endl;
}
} while(choice != 0);
return 0;
}
treePtr fillTree(treePtr p, int y, string t, list<string> a){
treePtr n = new treeNode;
n->year = y;
n->title = t;
n->actors = a;
n->left = NULL;
n->right = NULL;
if(p == NULL){
p = n;
}
else{
treePtr prev = p;
treePtr curr = p;
while(curr != NULL){
if(curr->year > y){
prev = curr;
curr = curr->left;
}
else{
prev = curr;
curr = curr->right;
}
}
if(prev->year > y){
prev->left = n;
}
else{
prev->right = n;
}
}
return p;
}
void print_titles(treePtr root){
if(root == NULL) return;
if(root->left) print_titles(root->left);
cout<<root->title<<endl;
if(root->right) print_titles(root->right);
}
void print_before_year(treePtr root, int key){
if(root == NULL) return;
if(root->left) print_before_year(root->left, key);
if(root->year < key){
cout << root->title << endl;
}
else return;
if(root->right) print_before_year(root->right, key);
}
void print_actors_movies(???){
}
以下是.txt文件以防万一:http://www2.cs.uidaho.edu/~bruceb/cs121/Assignments/movies.txt
答案 0 :(得分:1)
如果您有treeNode
,并且想要查找名称actor
,则可以使用
bool hasActor(treeNode const* node, std::string const& name) {
return node->actors.end()
!= std::find(node->actors.begin(), node->actors.end(), name);
}
函数std::find()
使用{{1}将begin
和end
范围内每个位置标识的值与作为最后一个参数传递的value
进行比较}。如果此表达式返回*it == value
,则返回true
。如果此表达式没有返回it
的位置,则返回true
。
答案 1 :(得分:1)
简单的广度优先搜索和std::find()
怎么样?
bool printTitleForActor(Node *root, const std::string &actor)
{
if (!root)
return false;
if (std::find(root->actors.begin(), root->actors.end(), actor) != root->actors.end()) {
std::cout << root->title << std::endl;
return true; // only if you want to terminate upon first find
}
return printTitleForActor(root->left) || printTitleForActor(root->right);
}