I'm trying to draw a binary tree using tikz. I use shift
option to define the relative position of child nodes. But as the tree grows, the nodes start to overlap if I use a fixed shift. I'm able to get around this by manually specifying the location of children nodes. But is there a better way to do it?
\documentclass{article}
\usepackage{tikz}
\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=0.5cm, shape border rotate=90, anchor=north},
process/.style={rectangle, minimum width=2cm, minimum height=1cm, align=center, text width=2cm, draw},
connector/.style={circle, minimum size=1cm, align=center, text width=0.5cm, draw},
arrow/.style={thick, ->, >=stealth}
}
\usepackage{circuitikz}
\begin{document}
\begin{tikzpicture}
\newcommand\xShift{1.5}
\newcommand\yShift{1}
\node(x) [treenode] at (0, 0) {100};
\node(xl) [treenode] at ([shift=({-\xShift,-\yShift})]x) {50};
\node(xr) [treenode] at ([shift=({\xShift,-\yShift})]x) {200};
\node(xll) [treenode] at ([shift=({-\xShift,-\yShift})]xl) {25};
\node(xlr) [treenode] at ([shift=({\xShift,-\yShift})]xl) {70};
\node(xlll) [ground] at ([shift=({-\xShift,-\yShift})]xll) {};
\node(xllr) [subtree] at ([shift=({\xShift,-\yShift})]xll) {};
\node(xlrl) [treenode] at ([shift=({-\xShift,-\yShift})]xlr) {60};
\node(xlrr) [subtree] at ([shift=({\xShift,-\yShift})]xlr) {};
\draw[->] (x) -- (xl);
\draw[->] (x) -- (xr);
\draw[->] (xl) -- (xll);
\draw[->] (xl) -- (xlr);
\draw[->] (xll) -- (xlll);
\draw[->] (xll) -- (xllr.north);
\draw[->] (xlr) -- (xlrl);
\draw[->] (xlr) -- (xlrr.north);
\end{tikzpicture}
\end{document}
Related question Draw a binary tree using tikz
graphdrawing
library and run withlualatex
25
and its right subtree which is a triangle, I'm not able to specify that the arrow should be on the north of the triangle.