Reputation: 47
I'm currently working on a movie database project using binary search trees. Each node in the tree contains the title of the film, the year the film was made, and a linked list containing the names of all the actors. This information is all read in from a .txt file.
I've created a few functions already, but I've hit a snag. I'm trying to make a function that enables the user to enter a string, which would be the actors first and last name, and then traverse the tree nodes. If the actors name is found in the linked list within a node, the function would print out the title of that film.
I've never used the STL before, so I don't know if accessing an element is that same as if you were to create the list manually, which is why I'm having issues.
Any help would be appreciated. Here is my code so far:
#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(???){
}
And here is the .txt file just in case you need it: http://www2.cs.uidaho.edu/~bruceb/cs121/Assignments/movies.txt
Upvotes: 0
Views: 124
Reputation:
How about a simple breadth-first search and 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);
}
Upvotes: 1
Reputation: 154035
If you have a treeNode
and you want to look for the name actor
, you can simple use
bool hasActor(treeNode const* node, std::string const& name) {
return node->actors.end()
!= std::find(node->actors.begin(), node->actors.end(), name);
}
The function std::find()
compares the value identified by each position in the range between the begin
and the end
to the value
passed as last argument using *it == value
. If this expression returns true
it returns it
. If there is no position for which this expression return true
, it returns end
.
Upvotes: 1