9.3 Recursive Programs

Before we look at the program used to draw the tree, we must first learn a little more about recursion. In computer science and mathematics, a recursive function is a function that calls itself. A very simple, but erroneous, illustration of a recursive function is shown in LISTING 9.1.

Image

LISTING 9.1 An erroneous recursive function

The hello function prints the message "Hello World" and then calls itself again. If you ran this function in Python, the program would continue to call itself and print "Hello World" until Python simply crashed. Clearly, there’s more to a successful recursive function than simply calling ...

Get Python Programming in Context, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.