# Why is this tree overlapping and missing edges?

\node [circle, draw] at (6, 0)  {63}
child{node [circle, draw] (left node) {26}
child{node [rectangle,draw] (left node) {A:12}}
child{node [circle, draw] (right node) {14}
child{node[rectangle,draw] (left node) {B:7}}
child{node [circle, draw] (right node) {7}
child{node[rectangle,draw] (left node) {Z:2}}
child{node[rectangle,draw] (right node) {X:5}}
}
}
}
child{node [circle, draw] (right node) {37}
child{node [rectangle,draw]  (left node) {I:18}}
child{node [circle, draw] (right node) {19}
child{node[rectangle,draw] (left node) {S:9}}
child{node[rectangle,draw] (right node) {M:10}}
}}
;


I'm new to tree building with LaTeX and I just can't seem to troubleshoot my own problems. Can anyone see the problem with this code that is causing the tree to come out messed up?

-

One problem is all the (left node) and (right node) notations. That syntax gives a name to the nodes. Usually node names are unique, but you've named half of them one thing and half of them another thing! So just take those out.

The sibling distance is by default 15mm at each level of the tree. There's no computation about the number of descendants to make the tree space out automatically. So you just have to make sure that the first level stretches out a bit.

Here is a complete example document:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[level 1/.style={sibling distance=30mm},level 2/.style={sibling distance=15mm}]
\node [circle, draw] at (6, 0)  {63}
child{node [circle, draw]  {26}
child{node [rectangle,draw]  {A:12}}
child{node [circle, draw]  {14}
child{node[rectangle,draw]  {B:7}}
child{node [circle, draw]  {7}
child{node[rectangle,draw]  {Z:2}}
child{node[rectangle,draw]  {X:5}}
}
}
}
child{node [circle, draw]  {37}
child{node [rectangle,draw]  {I:18}}
child{node [circle, draw]  {19}
child{node[rectangle,draw] {S:9}}
child{node[rectangle,draw]  {M:10}}
}}
;
\end{tikzpicture}

\end{document}


-
dude! took me like 2hrs to figure this out! thanks for the response though, I appreciate it! – Trevor Arjeski Apr 7 '11 at 1:58
I think you don't have to do it for every level by hand. Since I'm a newbie too, I don't want to edit your answere, but this from this example from here seems to work for me: [level/.style={sibling distance=45mm/#1}] – lumbric Dec 20 '11 at 18:32

Depending on how many of these you need to draw, and how exactly they need to look like the example in Matthew's answer, the tikz-qtree package provides a very quick way to input trees of this sort. It uses a simple labelled bracketing input method to describe the tree. Here's your example formatted using it.

\documentclass{article}
\usepackage{tikz-qtree}
\begin{document}
\tikzset{edge from parent/.style=
{draw, edge from parent path={(\tikzparentnode) -- (\tikzchildnode)}}}
\begin{tikzpicture}[every leaf node/.style={draw,rectangle,minimum width={3em}},
every internal node/.style={draw,circle}]
\Tree
[.63
[.26 A:12
[.14 B:7
[.7 Z:2 X:5 ]]]
[.37 I:18
[.19 S:9 M:10  ]]]

\end{tikzpicture}

\end{document}


The syntax is simple: [.X Y Z ] creates a node X with daughters Y and Z (which can themselves be nodes.) The space between the closing bracket and the Z is important for the tree parser ([.X Y Z] will give an error).

Thanks to kgr for figuring out how to make the branches extend from the centre of the circled nodes rather than the bottom (which is the default for tikz-qtree, and more appropriate for linguistic trees.)

-
I like the way tikz-qtree makes it easy to specify the tree—much simpler than tikz alone. Is there a way to make the connections emanate from the center of each node? – Matthew Leingang Apr 7 '11 at 2:14
@Matthew Not that I could figure out on a quick inspection of the code. It's mainly designed for drawing linguistic trees, (for example see this tree here but ignoring the circled and boxed nodes) and the branch placement is designed for that. (That's why I mentioned it depended on how exactly the tree needed to match yours.) – Alan Munn Apr 7 '11 at 2:44
This is great! Thanks for sharing – Trevor Arjeski Apr 8 '11 at 14:49
@Matthew kgr figured out a way to make the branches extend from the logical centre of each node. (In an answer to my comment here: How should I draw a tree with tikz. I've updated my answer to reflect that. – Alan Munn May 30 '11 at 22:36

Building on @Alan Munn's answer, you may prefer this style (cribbed from the tikz-qtree manual):

\begin{tikzpicture}[every leaf node/.style={draw,rectangle,minimum width={3em}},
every internal node/.style={draw,circle,child anchor=center}]

\tikzset{edge from parent/.style={draw,
edge from parent path={(\tikzparentnode.south)
--  +(0,-4pt)
-|  (\tikzchildnode)}}}

\Tree
[.63
[.26 A:12
[.14 B:7
[.7 Z:2 X:5 ]]]
[.37 I:18
[.19 S:9 M:10  ]]]

\end{tikzpicture}


If you vote this answer up, then please consider, in fairness, also voting up @Alan Munn's

-
A beautiful tree indeed – Trevor Arjeski Apr 8 '11 at 14:52