Here are some tools I've found helpful.
Binary search trees pop up everywhere in computer science. A binary search tree is made up of nodes. Each node has some data and up to two child nodes, named "left" and "right". Here's a graphical illustration of a happy little binary search tree holding a few letters of data:
An important property of a binary search tree is that for every node, everything in the left subtree is less than the data at the node and everything in the right subtree is greater than the data at the node.
create objects that have
null if there is no
subtree on that side.
Here's a little tree with two nodes.
Now we can build up functions that work on binary search trees.
Here's the function
contains that goes down the tree
to see if a given value exists in the tree.
Of course there's more than one way to skin a cat. Here's the same function implemented in a recursive way.
Now you try.
Write a function
count that counts the number of elements in a
binary search tree. If you're ambitious, solve it once with recursion
and once without recursion.