Programmers are often obsessed with drawing fractals. I think we need a new computer based medium.

The H tree is a fairly simple type of fractal made of horizontal and vertical lines. Here it is at it's tenth iteration (courtesy Wikipedia):

H tree

Now, imagine each of the lines in the image is a directory (folder) in a standard computer file system. All but the smallest lines intersect two lines smaller than themselves; these two smaller lines are subdirectories of the larger line. Thus the large horizontal line in the middle is the parent directory of the two largest vertical lines, which are in turn parents, grandparents, etc. of rest of the lines in the image.


Write a program that takes in a positive integer N via stdin or the command line (or closest alternative) and creates a directory tree that mirrors the Nth iteration of the H tree fractal.

The first iteration (N = 1) is a single horizontal line. In each subsequent iteration a new set of vertical or horizontal lines is added to the ends of the current lines. So for N = 2 two vertical lines are added (making an H shape), for N = 3 four horizontal lines are added to that, etc.

The name of the root directory must always be tree. The names of the subdirectories must match the direction they are in with respect to their parent line, using right, left, up and down.

Since the root directory is always a horizontal line it will always have right and left subdirectories. But those two will have up and down subdirectories, and those will have right and left again, and so on.

The directories at the end of the limit of iterations should be empty.


For N = 3 the directory tree should look like this:


Additional Info

  • Ideally the tree directory will pop up in the same folder the source code is in, but it's alright if it goes to some working directory.
  • You may assume that there is no pre-existing tree directory in the place yours will be created.
  • You may assume that your program will always be run on the same common modern operating system (Mac/Windows/Linux). Basically don't worry about using a file separator that doesn't work in all OS's.


This is code-golf, so the shortest code in bytes wins.

  • \$\begingroup\$ Do the subdirectories of tree always have to be left and right instead of up and down? \$\endgroup\$
    – KSFT
    Jan 17, 2015 at 18:54
  • 1
    \$\begingroup\$ @KSFT Yes: "Since the root directory is always a horizontal line it will always have right and left subdirectories." \$\endgroup\$ Jan 17, 2015 at 19:20

4 Answers 4


Ruby, 127 bytes

f=->n{n<2?['tree']:f[n-1].map{|p|%w{left right up down}[n%2*2,2].map{|d|p+?/+d}}.flatten}
system'mkdir "'+f[gets.to_i]*'" "'+?"

Tested on Windows. Takes input via STDIN.

f recursively generates a list of the required leaves of the tree, and then I just feed them to a single system call of mkdir.


Lua, 179

t=0+io.read()a={"left","right","up","down"}function h(d,v)if v~=t then os.execute("mkdir "..d)x=1 if v%2==1 then x=3 end for g=x,x+1 do h(d.."\\"..a[g],v+1)end end end h("tree",0)

Python - 194

from os import system as s
a="mkdir "
def f(n):
 if n<j%2:return
 for i in(0,1):m=["up"if n%2 else"left","rdiogwhnt"[n%2::2]][i];s(a+m);s("cd "+m);f(n-1);s("cd..")
  • \$\begingroup\$ its slightly shorter to do from os import*;s=system \$\endgroup\$
    – DenDenDo
    Jan 17, 2015 at 21:39
  • \$\begingroup\$ Two more things: you can also do ["left","up"][n%2] and you can drop the brackets around (0,1) to give for i in 0,1: \$\endgroup\$
    – Sp3000
    Jan 17, 2015 at 23:18

Python 2 + *nix coreutils, 212 189

Generates all the innermost paths and calls

mkdir -p

import os
for i in range(2**n):os.system("mkdir -p "+os.path.join('tree',*([['right','left'],['up','down']][b%2][int(j)]for b,j in enumerate('{:0{}b}'.format(i,n)))))

Crashes if input < 1

  • \$\begingroup\$ you can combine the first two lines: import os,itertools as t \$\endgroup\$
    – DenDenDo
    Jan 17, 2015 at 21:36
  • \$\begingroup\$ @DenDenDo Removed itertools completely \$\endgroup\$
    – user80551
    Jan 18, 2015 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.