Example: H-tree and b-tree

Published 2012-11-25 | Author: Andrew Stacey

The H-tree got his name because of its repeating pattern which looks like the letter “H”. It is also called H-fractal, it’s a space-filling curve with a Hausdorff dimension of 2.

A binary tree is a tree where each node has no more than two child nodes.

The example shows how to build up a tree recursively using a foreach loop. It uses the tree stuff already in TikZ. With a slight modification, the routine for drawing the H-tree can be adapted to a full binary tree.

Originally posted to TeX.SE.

Download as: [PDF] [TEX]

H-tree and b-tree

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% H-tree and B-tree
% Author: Andrew Stacey
  htree leaves/.initial=2,
  sibling angle/.initial=20,
  htree level/.initial={}


    (\pgfkeysvalueof{/tikz/sibling angle})*(-.5-.5*\tikznumberofchildren
  \pgfkeysvalueof{/tikz/htree level}%
  growth function=\htree@growth,
  sibling angle=180,
  htree level={

  growth function=\htree@growth,
  sibling angle=60,
  htree level={


  \foreach \l in {0,...,#2} {
    \g@addto@macro\htree@start{child foreach \noexpand\x in {1,2} {\iffalse}\fi}


  level distance=3cm,
  line width=8pt,


  • #1 Danna , January 3, 2013 at 1:58 a.m.

    quesia sabes si me ayudar con un codigo para haces un escudo o si me puedes ayudar a crear fractales gracias


  • #2 Me, April 21, 2013 at 12:20 p.m.

    A B-tree is not the same thing as a binary tree. This is a binary tree, not a B-tree, so the title is wrong.

Adding comments is currently not enabled.