[Python Beginner][DIY] Make Fractal Trees

Image for post
Image for post
Fig. Fractal Tree

Before we get into creating a fractal tree, let us understand, how it works.

So a fractal tree works on the principle of Recursion. Basically, recursion is a programming technique that revolves around a function calling itself in iterative steps.

Image for post
Image for post

So, how do we do recursion exactly? and why is it being used here?

To do recursion in a function, we must follow 2 simple rules:
1. Make it as logical as possible since debugging a recursion can be a pain
2. Make sure you have a break statement with an if statement inside the recursive function else the function will run till infinity

def factorial(n):
if n == 1:
return 1
return n * factorial(n-1) # calling the function again

print(factorial(3)) #Output: 6

In the above example, you can see how recursion is done in python. It is quite straightforward i.e. you call the function again and again till the number n gets reduced to 1 and then it exits the recursion loop.

*Here having the if n==1 statement is extremely important else, recursion will go till negative infinity.

Now, what are the advantages and disadvantages of recursion?

i. The main benefit of a recursive approach to algorithm design is that it allows programmers to take advantage of the repetitive structure present in many problems.
ii. Complex case analysis and nested loops can be avoided.
iii. Recursion can lead to more readable and efficient algorithm descriptions.

i. Slowing down execution time and storing on the run-time stack more things than required in a non-recursive approach are major limitations of recursion.
ii. If recursion is too deep, then there is a danger of running out of space on the stack and ultimately program crashes.
iii. Even if some recursive function repeats the computations for some parameters, the run time can be prohibitively long even for very simple cases.

Image for post
Image for post
Okay….how is all that technical mumbo jumbo related to me making trees?

Well now comes the fun part.

A fractal tree is a tree made up by creating recursive branching in a graphics module in python. We can use turtle module or pygame module.

Since turtle will be easier, we will go with it.

First things first, install turtle module in your python environment. Doing a simple pip install turtle in command line should do the trick.

*For more info on how to do pip install in python, refer here.

Let us do a simple code first before jumping head-first into the fractal tree.

import turtle

myTurtle = turtle.Turtle() # initialization of the module
myWin = turtle.Screen() # initialization of the screen

def drawSpiral(myTurtle, lineLen):
if lineLen > 0:
myTurtle.forward(lineLen) #move forward
myTurtle.right(90) # move right with a 90 degree angle
drawSpiral(myTurtle,lineLen-5) #draw the above movements

Image for post
Image for post
Output to the above code

The above code is pretty straightforward, go forward and then take a right and do it in a loop with reducing distance and voila! spiral.

Now to make a tree.

First, we need to make the branches, the basic idea is to go forward, move right then back to the origin and move left to make a ‘Y’ shape. Repeat it on a loop and pretty much done. Adjust the branch length for making a more complex tree.

import turtle

def tree(branchLen,t):
if branchLen > 5:

def main():
t = turtle.Turtle()
myWin = turtle.Screen()

Image for post
Image for post
The output of the above code
Image for post
Image for post
Changed the branchLen = 100 and branchLen > 2

Play around the code a bit and try if you can make an even more complex tree.

Image for post
Image for post
Task: Try making this by modifying the values around.

If you wish to contact me, the LinkedIn account and Facebook group links are here.

Sources used: http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsintro-VisualizingRecursion.html, https://en.wikipedia.org/wiki/Fractal_tree_index, https://www.sparknotes.com/cs/recursion/whatisrecursion/section1/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store