draw h tree

🏠

pythonic tricks

 1from math import sqrt
 2
 3def drawHTree(x, y, length, depth, current_depth=0):
 4  if current_depth == depth:
 5    # recursion base case
 6    return
 7  
 8  # this is the horizontal line of the H 
 9  drawLine(-length/2, 0, length/2, 0)
10  # this is the left vertical line
11  drawLine(-length/2, length/2, -length/2, -length)
12  # this is the right vertical line
13  drawLine(length/2, length/2, length/2, -length)
14  
15  # H at the top left
16  drawHTree(-length/2, length/2, length/sqrt(2), depth, current_depth+1)
17  # H at the bottom left
18  drawHTree(-length/2, -length/2, length/sqrt(2), depth, current_depth+1)
19  
20  # h at the top right
21  drawHTree(length/2, length/2, length/sqrt(2), depth, current_depth+1)
22  # h at the bottom right
23  drawHTree(length/2, -length/2, length/sqrt(2), depth, current_depth+1)
24
25  
26drawHTree(0, 0, 5, 5)

Pasted image 30.png