Step 1. Binary tree

This program (or "World") implements and searches a binary search tree. As we noted in the introduction, we are not explaining AI theory here. You will have to find that elsewhere. This is a series of AI exercises.
How to use these pages

  • Click video to play. Click World image to run.
  • Maximise video to see text and code clearly.
  • Click "Clone and Edit" to make your own copy of the World and edit it.
  • Everything works in the browser. No install needed. You need to Register and log in to edit.
  • Use buttons (or back and forward arrow keys) to move through the course.

  • Warning: If you click "Clone and Edit" again, you make a new World. You do not return to the first World. To return to that, just keep its tab open. If you lose its tab, you can find the World again through your home page.

Video

World

Binary tree
Binary tree search demo.

Notes

  • Click on the World image to run it. (Opens in new window/tab.)
  • It creates a random binary tree and searches it.
  • Reload for a new search problem.
  • This World writes lots of information to the browser console.
  • You must view console to see what is going on during (a) build tree and (b) search tree.


Credits

  • This is a modified port of "01_binary_tree_viz" from AI course by Daniel Shiffman.
  • See his binary tree videos parts one and two.
  • In the videos he does not use Ancient Brain but codes on localhost.


View the JavaScript

  • You can see the JavaScript source code.
  • Supporting files:

  • Just type tree into the JavaScript console to see the data structure. Click to expand.
  • The World picks a random item to search for.
  • To do a manual search: Type something like this in the console:
     tree.search(10); 

Exercise

  1. Clone and Edit your own copy of the binary tree World.
  2. Copy the two supporting JS files to your own files. Edit your World to point to these two new files.
  3. Change the range of numbers.
  4. Change how many nodes there are.
  5. Give it a massive problem with a huge number of nodes. Don't worry, you cannot damage your browser or the server!

Tweet this step: