How do the NUnit tests verify that your BST is working?

advertisement

Note: Binary Search Tree Verification

The goal for this note is to clarify how the NUnit tests will verify that your BST methods are working correctly.

You may be wondering how the NUnit tests verify that your BST’s methods have been correctly implemented. This note will (hopefully) clarify how this is done, by clarifying two things: what’s being verified, how the tests gain access to your tree of nodes, and how a correct tree is described.

Firstly, the only thing that the test verifies is the overall structure of the nodes within the BST

– the test doesn’t actually verify that you’ve followed any particular approach for doing the insertion. As long as the nodes end up in the correct place, the test can’t determine if you did this iteratively (using a loop), recursively, or via some other mechanism.

Secondly, the test gains access to your tree of nodes by declaring a subclass of your BinarySearchTree class, and accessing the protected field top. Because of this, you’re not allowed to change the name of the BinarySearchTree class, nor the top field. This subclass inherits your implementation of the Add/Find/etc methods, and adds it’s own “ValidateTree” method. The test’s basic approach is that it will create an object of this subclass, call your Add method (which the object inherited) to load the tree, then call ValidateTree in order to check that your Add method put everything in the right spot(s).

Thirdly, the test describes the correct structure of the tree using an array. This is actually a really common trick – heapsort uses this trick, for example. The array describes the tree in the following manner: Whatever value is at index zero ( 0 ) is what should be at the root of the tree. Whatever value is at slot 1 is the value in the node to the LEFT of the root. The value in slot 2 is the value to the RIGHT of the root.

The following picture shows an array that contains the values {10, 5, 20, 1, 7}, and the

BST that the array represents. If you look at the green arrows on the array, you can clearly see the connections between 10 (the root value, in the array at index 0), 5 (the the value to the LEFT of the root), and 20 (the value to the RIGHT of the root).

So what if we want more than three values in the tree/array? Well, clearly we can just extend the pattern we’ve already used. If the root’s index was 1 (instead of

0), then the left child of a given node will be at that array index that is twice the root, and the right child is at the root’s index multiplied by 2, plus 1. The only tricky part is that since our array is zeroindexed, we’ll need to use a slightly more complicated formula – we’ll convert the root’s index to being “1-indexed”, figure out where twice (or twice plus one) is, then convert back to being zero indexed, using the following pseudocode: int LeftOf(int index)

{

return ( (index + 1) * 2 ) – 1;

} int RightOf(int index)

{

return ( (index + 1) * 2 ) ; // this is the same as adding one to twice the index

}

If these formulas are confusing, don’t worry – your instructor feels oddly compelled to explain them in the name of offering a complete education, but doesn’t expect you to use them 

The main thing is that if you return to the picture above, you’ll see that the left child of the value 5 is in array slot 3, and it’s right child is in array slot 4.

The only thing left to describe is the ‘BLANK’ constant, which the test interprets to mean that there is no node in a given spot. So for example, the test array {100,

BLANK, 200} can be pictured like so:

One last, quick point about this way of representing a BST in an array: it’s a great way to go, as long as you have a completely full tree. If the tree skews to the right ( like the one pictured above), you’ll see that the array still needs to have that

B LANK slot 1. A real binary search tree doesn’t need to waste that space, which is much, much better. Especially when we want to represent the tree that { 100, 50,

200, BLANK, BLANK, BLANK, 250 } represents. That’s why this is a nice trick to know, but isn’t used as much as BSTs are.

So at this point, you should be able to examine the test code that checks your

BST.Add method for correctness, and be able to see what the expected tree is from the source code.

What you need to do for this note:

1. Nothing

– this is a note that’s intended to clarify something, not a required exercise.

Download