Week 10: Tree Drawing

Chris Tralie

Overview / Logistics

Now that we have the basic tree representation down in python, we will create a tool which will draw them help us to debug and understand algorithms that we will explore on trees next. Click here to download the starter code. Here's an example of how you might end up drawing the default tree that's there:

Here are some coding tips to help you:

  • You should definitely use recursion to help here, and you may want to do more than one recursive traversal through the tree. You can use the inorder traversal of the nodes to choose the x coordinate, and you can obtain the y coordinate by keeping track of the depth of recursion.
  • You can plot a black dot in matplotlib with
  • To help see what's in the tree, you might want to draw the node's value with text slightly to the right of the node's center
  • You should draw a line segment between a node and its children if they exist. To draw a black line segment between two points (x1, y1) and x2, y2 in matplotlib, you can use the code