I'm trying to draw a binary tree using tikz
package.
If I use only circle
, I get a tree. But I want to represent a subtree
using a triangle
. Now the tree gets uneven. I want to attach the arrows to the top of the triangle
and also want the size of the triangle
comparable to the size of the circle
.
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{shapes.geometric,arrows,fit,matrix,positioning}
\tikzset
{
treenode/.style = {circle, draw=black, align=center, minimum size=1cm},
subtree/.style = {isosceles triangle, draw=black, align=center, minimum height=0.5cm, minimum width=1cm, shape border rotate=90, anchor=north}
}
\begin{document}
\begin{figure}[t]
\centering
\caption{An illustration of a simple delete operation. \label{fig:simple}}
{
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
level distance = 1.5cm},scale=0.6, transform shape]
\node [treenode] {$X$ \\ 100}
child
{
node [treenode] {$Y$ \\ 50}
child
{
node [treenode] {$Z$ \\ 40}
child
{
node [treenode] {$S1$ \\ 30}
}
child
{
node [treenode] {$S2$ \\ 60}
}
}
child
{
node [subtree] {$Z$ \\ 200}
}
}
child
{
node [subtree] {$Z$ \\ 200}
}
;
\end{tikzpicture}
}
\end{figure}
\end{document}
Best Answer
With the OP's current setting the circles and triangles are of comparable size as long as the text is not written into the
subtree
nodes. So a possible solution is to write the text later after the drawing is done. This is shown by the last 2 lines of codes.To have the arrows pointing to the tip of a triangle, the following line is added to the corresponding parent nodes
edge from parent path ={(\tikzparentnode.-50) -- (\tikzchildnode.north)} % -50 is adjustable.
If the triangles are still incomparable, one can write
scale=0.9
into thesubtree
style for shrinking adjustment.Code