This assignment is intended to have you start working with binary search trees. There are several parts to the assignment, each described below.
For this assignment, you are provided with some starter code that defines the structures you'll be working with and prototypes the functions youll be writing. Its important that you dont modify the function prototypes.
In this assignment, your work will be limited to the file bst.c, where you will implement the functions described below. In particular, you should write all of your code below the comment in bst.c that says "Below are the functions and structures you'll implement in this assignment." In addition to the descriptions below, there is thorough documentation in bst.c describing how each function youll write should behave.
For the first part of the assignment, you will implement a function called bst_size() that determines the size of a given BST. Specifically, this function should return the number of nodes in the BST.
Hint: it could be easier to think recursively. Feel free to implement any helper functions you need.
For the second part of the assignment, you will implement a function called bst_height() that determines the height of a given BST. Remember, the height of a tree is the maximum depth of any node in the tree (i.e. the number of edges in the path from the root to that node).
Hint: again, it could be easier to think recursively. Feel free to implement any helper functions you need.
For the third part of the assignment, you will implement a function called bst_path sum() that determines whether a BST contains any path from the root to a leaf in which the values of the nodes sum to a specified value.
Hint: again, it could be easier to think recursively. Feel free to implement any helper functions you need.
Finally, you will implement an iterator that returns values from a BST in the same order they would be visited in an in-order traversal of the tree. For a BST, this will be equivalent to ascending sorted order. You will have to define a structure struct bst_iterator to hold the needed data for your iterator, and then you must implement the following functions:
Hint: the key to implementing this iterator is figuring out how to perform an in-order traversal non-recursively, since we can't easily break out of a recursion at a specified step once its started. Note that youre provided with a stack implementation in stack.h. Can you use this to mimic the way a recursive in-order traversal visits nodes in a BST? In particular, can you use a stack to mimic the way function calls are placed on the call stack in a recursive traversal of a BST?
In addition to the starter code provided, you are also provided with some application code in test.c to help verify that your functions are behaving the way you want them to. In particular, the code in test.c calls the functions you'll implement in this assignment, passing them appropriate arguments, and then prints the results. You can use the provided Makefile to compile all of the code in the project together, and then you can run the testing code as follows:
make
./test
In order to verify that your memory freeing functions work correctly, it will be helpful to run the testing application through valgrind.
There is also a more extensive unit test suite for all of the functions you'll write for this assignment included in unittest.c. After running make, you can run these unit tests as follows:
./unittest
There are several unit tests included, and if any unit test failures occur, an error message will be printed out indicating the line number in unittest.c from which the failure originated. There are comments above each test that you can read to more thoroughly understand any given test failure.