Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I just want to know how to write these functions: I got these three functions, {

ID: 3862442 • Letter: I

Question

I just want to know how to write these functions:

I got these three functions,

{

MovieNode *foundMovie = root;

while(founMovie != NULL)

{

if(foundMovie->title > title)

{

   foundMovie = foundMovie->leftChild;

}else if(foundMovie->title < title)

{

   foundMovie = foundMovie->rightChild;

}

return foundMovie;

}

cout<<" Movie not found."<<endl;

return NULL;

}

{

   MovieNode *current = node;

while(current ->lefchild != NULL)

{

current = current->leftChild;

}

return current;

}

But I don't how to use these two to write deleteNode function(ps:if I writed wrong for those two functions,plz help me modify)

one thing I know is that, if i want to use search function;

I can do this:

MovieNode *foundMoive = search(title);

  

Explanation / Answer

Answer:

Hi, I think your search function has some problems, as you can't compare between two strings using the > or < operator, you would need to us the strcmp method. Here is the search function modified for you.

//Search function
MovieNode* search(std::string title){
MovieNode* temp = root;
MovieNode* foundMovie = NULL;
while(temp != NULL){
if(std::strcmp(temp->title, title) == 0){
foundMovie = temp;

break;
}
//Iterating over the entire list until we get the movie node
else{
temp = temp->rightChild;
}
}
if(foundMovie != NULL){
return foundMovie;
}
else{
cout<<"Movie not found.";
return NULL;
}
}

Since, in your code you have used both leftChild and rightChild pointers, I'm assuming you are using a doubly linked list to store your movie data. Here is the deleteNode function :

void deleteMovieNode(std::string title){
MovieNode* temp = root;
MovieNode* left;
MovieNode* right;
while(temp != NULL){
if(strcmp(temp->title, title) == 0){
left = temp->leftChild;
right = temp->rightChild;
if(left != NULL){
left->rightChild = right;
}
if(right != NULL){
right->leftChild = left;
}
temp->leftChild = NULL;
temp->rightChild = NULL;
free(temp);
}
else{
//Iterate over the list, until movie is found
temp = temp->rightChild;
}
}
}

From what I can understand, your treeMinimum function returns the first node in the list, and if this is the desired functionality from treeMinimum, then what you have written for treeMinimum is perfect. No changes needed there.