Create a simple split tree

I like recursion so I've written a recursive solution.

Helper function for doing the drawing the basic u-shaped element of the tree

draw[x0_, x1_, y0_, y1_] := Line[{{x0, y1}, {x0, y0}, {x1, y0}, {x1, y1}}]

Helper function for doing the recursion

treeF[lvls_, lvl_, xy_, w_, h_] :=
  Module[{x0, x1, y0, y1},
    {x0, x1} = xy[[1]] + w {-1, 1}/2;
    {y0, y1} = xy[[2]] + {0, h};
    tree = {tree, draw[x0, x1, y0, y1]};
    If[lvl < lvls,
      treeF[lvls, lvl + 1, {x0, y1}, w/2, h];
      treeF[lvls, lvl + 1, {x1, y1}, w/2, h]]]

The main function

splitTree[levels_Integer?Positive, minW_: 1, ht_: 1] :=
  Block[{tree = {}},
    treeF[levels, 1, {0, 0}, baseW = 2^(levels - 1) minW, ht];
    Graphics[tree]]

Examples of use

splitTree[5]

tree5

Show[splitTree[7, 1, 5], ImageSize -> 600]

tree7.1.5


One idea is to use Dendrogram on a KaryTree. Here is a function that does this:

splitTree[n_Integer?Positive] := Dendrogram @ KaryTree[
    2^(n+1) - 1,
    VertexWeight -> Floor @ Log2[Range[2^(n+1) - 1]]
]

An example:

splitTree[4]

enter image description here

One can use Graphics options to control the size. For example:

Show[splitTree[6], ImageSize->{300, 30}, AspectRatio->Full]

enter image description here


Repeated scaling + translation is another possibility:

With[{n = 7},
     Graphics[Flatten[NestList[(# /. Line[l_] :> 
                                With[{c = -Mean[l[[{2, -2}]]]}, 
                                     Line /@ Outer[TranslationTransform[#2][#1] &,
                                                   l[[{-1, 1}]], 
                                                   TranslationTransform[c][l].
                                                   DiagonalMatrix[{1/2, 1}], 1]]) &,
                               {Line[{{-16, 1}, {-16, 0}, {16, 0}, {16, 1}}]}, n]]]]

tree