11

I'm trying to draw AVL trees with TikZ, what I need is:

Binary tree with simple circle nodes, and adjustable height triangle (or rectangle) nodes for subtrees, both containing only single characters. Here's a picture:

enter image description here

I tried with rectangle and regular polygon, but they seem to be unaffected by aspect ratio... Is there an easy way of drawing something like this in TikZ?

morbusg
  • 25,490
  • 4
  • 81
  • 162
tpv
  • 213

1 Answers1

10

Using the isosceles triangle shape and some manually set heights:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}[
  inner/.style={circle,draw,minimum width=7mm,inner sep=0},
  leaf/.style={isosceles triangle,draw,shape border rotate=90,isosceles triangle stretches=true, minimum height=20mm,minimum width=12mm,inner sep=0,yshift={-20mm},font=\tiny},
  large leaf/.style={leaf,minimum height=35mm,yshift={-14.5mm}},
  level 1/.style={sibling distance=30mm},
  level 2/.style={sibling distance=21mm},
  level 3/.style={sibling distance=14mm},
]
  \node[inner] {T}
    [child anchor=north]
    child {node[inner] {L}
      child {node[large leaf] {}}
      child {node[inner] {LR}
        child{node[leaf] {}}
        child{node[leaf] {}}}}
    child {node[large leaf] {L.Allison}};
\end{tikzpicture}

\end{document}

result

I don't know why the top vertices of the two large triangles are at different heights relative to their sibling nodes.

Caramdir
  • 89,023
  • 26
  • 255
  • 291