In the ninth part, you will extend the binary search tree in the eighth part to
ID: 3790775 • Letter: I
Question
In the ninth part, you will extend the binary search tree in the eighth part to support the deletion of a node. The deletion of a node is slightly trickier compared to the search and insert in the eighth part.
The deletion is straightforward if the node to be deleted has only one child. You make the parent of the node to be deleted point to that child. In this scenario, special attention must be paid only when the node to be deleted is the root
Deleting a node with two children requires some more work. In this case, you must find the minimum element in the right subtree of the node to be deleted. Then you insert that node in the place where the node to be deleted was. This process needs to be repeated to delete the minimum node that was just moved.
In either case, if the node to be deleted is the root, you must update the pointer to the root to point to the new root node.
Input format: This program takes a file name as argument from the command line. The file is either blank or contains successive lines of input. Each line contains a character, ’i’, ’s’, or ’d’, followed by a tab and an integer. For each line that starts with ’i’, your program should insert that number in the binary search tree if it is not already there. If the line starts with a ’s’, your program should search for that value. If the line starts with a ’d’, your program should delete that value from the tree.
In C, please. Thanks.
Example Execution: Lets assume we have a file filel.txt with the following contents: i 5 i 3 i 4 i 1 i 6 i 2 s 1 d 3 s 2 Executing the program in the following fashion should produce the output shown below: ninth file1.txt inserted 1 inserted 2 inserted 3 inserted 3 inserted 2 inserted 4 present 3 Success present 4Explanation / Answer
#include int main ( void ) { static const char filename[] = "file.txt"; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 128 ]; /* or other suitable maximum line size */ while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { fputs ( line, stdout ); /* write the line */ } fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } return 0; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.