Please follow instructions and explain how to run makefile. This assignment asks
ID: 3675419 • Letter: P
Question
Please follow instructions and explain how to run makefile.
This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments:
% bstsort [-c] [-o output_file_name] [input_file_name]
If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name
is given with the -o option, the program will output the sorted lines to the given output file; otherwise, the output shall be the standard output. Similarly, if the input_file_name is given, the program will read from the input file; otherwise, the inp
ut will be from the standard input. You must use getopt() to parse the command line arguments to determine the cases.
In addition to parsing and processing the command line arguments, your program needs to do the following:
1. You need to construct a binary search tree as you read from input. A binary search tree is a binary tree. Each node can have at most two child nodes (one on the left and one on the right), both or either one can be empty. If a child node exists, it's the root of a binary search tree (we call subtree). Each node contains a key (in our case, it's a string). If the left subtree of a node exists, it contains only nodes with keys less than the node's key. If the right subtree of a node exists, it contains only nodes with keys greater than the node's key. In case of duplicate keys, you can use a counter scheme (i.e., using an integer to indicate the number of duplicate keys the node is currently representing). You can look up binary search tree on the web or in your Data Structure textbook. Note that you do not need to balance the binary search tree (that is, you can ignore all those rotation operations) in this assignment.
2. Initially the tree is empty (that is, the root is null). The program reads from the input file (or stdin) one line at a time; If the line is not an empty line, it should create a tree node that stores (a copy of) the string (you shall remove the trailing line feed) and then insert the tree node to the binary search tree. Pay attention to situations that a duplicate is found, in which case, you ought to simply increment the count and remove the new node, instead of inserting the duplicate node. An empty line would indicate the end of input for stdin, an empty line or end of file would indicate the end of input for an input file.
3. You must develop two string comparison functions, one for case sensitive and the other for case insensitive. You must not use strcmp() and strcasecmp() functions provided by the C library. You must implement your own version. In the situation where case insensitive is chosen, then use all lower case for the strings.
4. Once the program has read all the input (when EOF is returned), the program then performs an in-order traversal of the binary search tree to print out all the strings one line at a time to the output file or stdout.
5. Before the program ends, it must reclaim the tree! You can do this by performing a post-order traversal, i.e., reclaiming the children nodes before reclaiming the node itself. Make sure you also reclaim the memory occupied by the string as well.
6. It is required that you use getopt for processing command line and use malloc/free functions for dynamically allocating and deallocating nodes and the buffers for the strings. It is required that you implement your own string comparison functions instead of using the corresponding libc functions.
Here's an example:
One can also use './bstsort -o sortedfile < myfile' to take input from stdin (in this case, it's replaced by a file by the shell) and output the sorted lines in the output file (called sortedfile in the example).
Explanation / Answer
I would like to suggest a simple and effient way
Use
your lines of file will be sorted as your need.