Test Writing: BinaryTree Balanced
Created By: Geoffrey Challen
/ Version: 2020.11.0
Let's determine if a binary tree is height balanced.
A tree is height balanced if the height of any node's two subtrees (right and left) never differ by more than 1.
Provide a method named isBalanced
.
isBalanced
accepts a BinaryTree<*>
and returns true
if the tree is balanced, and false
otherwise.
A few hints on this problem:
- Your main entry point method will probably need to start the recursion using a private helper method, because the
main method doesn't accept nullable trees which you want to handle as a valid base case in your recursion
- This helper method will probably have the same arguments as the main method, so you'll need to change something
else to make sure that the method signature is different and the compiler can determine which one you want to use
- You will probably need a second helper method implementing tree height that you call in your primary recursive
method
Test Design Challenge
You're challenge is to write tests for this problem described above.
- Provide a public class named TestBinaryTreeBalanced with a single non-private class method named test that accepts no arguments and does not return a value.
- If the implementation of the class described above is incorrect, your test method should throw an exception.
- If it is correct, do not throw an exception.
- You may want to use Kotlin's assert or check methods