5

I'm trying to draw a k-ary tree using tikz.

Nice K² Tree

Unfortunately I couldn't manage to avoid the nodes from overlapping. I also tried setting level/.style={sibling distance=30mm/#1}, growth parent anchor=south and experimenting with the settings, but so far to no avail. It would be nice if there would be no space at the top node position (like the original tree).

Overlapping Tree

This is the code I used:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}[]
\node{}
  child {node {1}
    child {node {1}
      child {node {1}
        child {node {0}}
        child {node {1}}
        child {node {0}}
        child {node {0}}
      } 
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {1}}
      }
      child {node {0}}
      child {node {0}}
    }
    child {node {1}
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {0}}
      }
      child {node {0}}
      child {node {0}}
      child {node {0}}
    }
    child {node {0}}
    child {node {1}
      child {node {0}}
      child {node {0}}
      child {node {0}}
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {0}}
      }
    }
  }
  child {node {0}}
  child {node {1}
    child {node {0}}
    child {node {1}
        child {node {0}}
        child {node {1}
          child {node {1}}
          child {node {0}}
          child {node {1}}
          child {node {0}}
        }
        child {node {0}}
        child {node {1}
          child {node {1}}
          child {node {0}}
          child {node {0}}
          child {node {0}}
        }
    }
    child {node {0}}
    child {node {0}}
  }
  child {node {1}
    child {node {1}
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {1}}
          child {node {0}}
        }
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {0}}
          child {node {0}}
        }
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {0}}
          child {node {0}}
        }
        child {node {0}}
    }
    child {node {0}}
    child {node {0}}
    child {node {0}} 
  };
\end{tikzpicture}
\end{figure}
\end{document}
  • If there's a pattern, you can draw it programmatically, if you wish. Is there? – cfr Oct 19 '16 at 22:14
  • There is: If a parent node has value 1, it has k children, otherwise it has 0. – Jan B Oct 20 '16 at 8:58
  • @janB That's not sufficient a pattern to determine the values of the k children though. – Alan Munn Oct 20 '16 at 11:27
  • You're right I thought about writing some code, in order to just supply a bitvector level-wise e.g. l1=1011 l2=110101001000 that would be useful in this case, but I don't think the time used for developing this will be spent wisely. I might write a small script though, which generates the code below, if I need it often. – Jan B Oct 20 '16 at 11:33
11

Use forest. It automatically spaces nodes to make as compact a tree as possible, and has a much easier syntax. Depending on your needs, you could also generate the tree programmatically.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}for tree={inner sep=0pt,outer sep=0pt}
  [1
    [1
      [1
        [0]
        [1]
        [0]
        [0]
      ] 
      [1
        [0]
        [0]
        [1]
        [1]
      ]
      [0]
      [0]
    ]
    [1
      [1
        [0]
        [0]
        [1]
        [0]
      ]
      [0]
      [0]
      [0]
    ]
    [0]
    [1
      [0]
      [0]
      [0]
      [1
        [0]
        [0]
        [1]
        [0]
      ]
    ]
  [0]
  [1
    [0]
    [1
        [0]
        [1
          [1]
          [0]
          [1]
          [0]
        ]
        [0]
        [1
          [1]
          [0]
          [0]
          [0]
        ]
    ]
    [0]
    [0]
  ]
  [1
    [1
        [1
          [0]
          [1]
          [1]
          [0]
        ]
        [1
          [0]
          [1]
          [0]
          [0]
        ]
        [1
          [0]
          [1]
          [0]
          [0]
        ]
        [0]
    ]
    [0]
    [0]
    [0]
 ]
]
\end{forest}

\end{document}

output of code

2

Seems that forest is really the right tool for the job. Appearently, to do this as you wanty with TikZ you need to use the algorithms of graphdrawing library, which require LuaLaTeX (See section 26 for the GraphDrawing engine and 21 for the customizable tree keys of the TikZ Manual):

\documentclass[border=5mm,tikz]{standalone}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[tree layout, level 4/.style={sibling distance=0.01em}]
\node{}
  child {node {1}
    child {node {1}
      child {node {1}
        child {node {0}}
        child {node {1}}
        child {node {0}}
        child {node {0}}
      } 
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {1}}
      }
      child {node {0}}
      child {node {0}}
    }
    child {node {1}
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {0}}
      }
      child {node {0}}
      child {node {0}}
      child {node {0}}
    }
    child {node {0}}
    child {node {1}
      child {node {0}}
      child {node {0}}
      child {node {0}}
      child {node {1}
        child {node {0}}
        child {node {0}}
        child {node {1}}
        child {node {0}}
      }
    }
  }
  child {node {0}}
  child {node {1}
    child {node {0}}
    child {node {1}
        child {node {0}}
        child {node {1}
          child {node {1}}
          child {node {0}}
          child {node {1}}
          child {node {0}}
        }
        child {node {0}}
        child {node {1}
          child {node {1}}
          child {node {0}}
          child {node {0}}
          child {node {0}}
        }
    }
    child {node {0}}
    child {node {0}}
  }
  child {node {1}
    child {node {1}
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {1}}
          child {node {0}}
        }
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {0}}
          child {node {0}}
        }
        child {node {1}
          child {node {0}}
          child {node {1}}
          child {node {0}}
          child {node {0}}
        }
        child {node {0}}
    }
    child {node {0}}
    child {node {0}}
    child {node {0}} 
  };
\end{tikzpicture}
\end{document}

enter image description here

Note the second number of the second level (the 0), it's not where it's supposed to be, I don't know why and tried a lot of keys to position it better but all unsuccessfully, since I don't know what happens behind the scenes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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