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.

1. Implement a function to determine the size of a BST

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.

2. Implement a function to determine the height of a BST

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.

3. Implement a function to check path sums in a BST

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.

4. Implement an in-order iterator for a BST

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:

  • bst_iterator_create() - allocates and initializes an iterator for a given BST
  • bst_iterator_free() - frees all memory allocated to a BST iterator
  • bst_iterator_has_next() - should tell the user whether there are more values in the BST to which to iterate
  • bst_iterator_next() - should return the next value in the in-order iteration of the BST

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?

Testing your work

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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.