Binary tree exercise
Click to run World:
Binary tree at
Ancient Brain.
- Clone and Edit your own copy of the binary tree "World".
- This World uses the P5 graphics library.
- If you have not done it, do the P5 Starter Tutorial.
Exercise - Change some parameters
-
Clone the World, and edit your own version.
- Change the range of numbers.
- Change how many nodes there are.
- What if you leave number of nodes the same but give it a large range?
- What if you leave range the same but give it a large number of nodes?
- Give it a massive problem with a huge number of nodes. Don't worry, you cannot damage your browser or the server!
Exercise - Copy the JS libraries
- Save the two JS libraries to your disk:
- Go to menu - User - Uploads
- Upload the two JS libraries.
- From the Uploads page you can Edit them.
- This is a special editor for Uploads. It does not know this is for a P5 World, so "Code help" is more generic.
Also, multiple Worlds may use the JS library, so it cannot give you a "Run" button.
Q. How do you make sure your World uses your JS libraries?
Exercise
Edit the World and the JS libraries to:
- Change background color.
- Change ellipse size.
- Change text color of nodes.
- Change font size of text.
Exercise - Do not draw the graph
With AI tree search,
you do not have to actually draw the graph.
The graph can just be an internal data structure. It does not have to be drawn.
Try the following:
- Run it with MAX = 5000.
- Time how long it takes to finish, using:
var t = Date.now(); // number of milliseconds since 1970
- You can save the time at start, get time at end, subtract them, and console.log the difference.
- Draw tree or not:
- Edit the code to turn drawing of the tree off.
- Comment/uncomment tree.traverse();
- Time how long it takes.
- Console messages or not:
- Comment/uncomment all console.log messages, including in the JS libraries.
- Time how long it takes.
- Console open or closed
- It is much faster if the console is closed when it is writing messages and you just open it at the end.
- Time how long it takes with console open or closed.
I got the following timings for MAX = 5000:
- Draw tree ON. Console log ON. Console OPEN. Time: Around 2.5 seconds.
- Draw tree OFF. Console log OFF. Time: Around 5 milliseconds.
- That is, turn all visuals and debug off and it is 500 times faster.