[Tex/LaTex] My version of Red-Black Tree

tikz-pgftikz-trees

Here's the code I have

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\tikzset{
  treenode/.style = {align=center, inner sep=0pt, text centered,
    font=\sffamily},
  arn_n/.style = {treenode, circle, white, font=\sffamily\bfseries, draw=black,
    fill=black, text width=1.5em},% arbre rouge noir, noeud noir
  arn_r/.style = {treenode, circle, red, draw=red, 
    text width=1.5em, very thick},% arbre rouge noir, noeud rouge
  arn_x/.style = {treenode, rectangle, draw=black,
    minimum width=0.5em, minimum height=0.5em}% arbre rouge noir, nil
}

\begin{document}
\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1,
  level distance = 1.5cm}] 
\node [arn_n] {1}
    child{ node [arn_r] {2} 
            child{ node [arn_n] {4} 
                child{ node [arn_r] {6}}
                            child{ node [arn_r] {7}}    
                                              }
    child{ node [arn_r] {3} edge from parent node[above left]
                         {$x$}
            }
; 
\end{tikzpicture}
\end{document}

I want to write something beside each arrow… I tried using edge from parent node[above left]{$x$}

But it messes up the whole thing, probably cause I'm inserting it in the wrong location.

Plus, I wanna write something beside each circle.. Is there a way to do that too?

Help please!

Best Answer

A forest version just because:

forest tree in black and red

\documentclass[tikz]{standalone}

\usetikzlibrary{arrows}
\usepackage{forest}

\begin{document}
  \tikzset{%
    /forest,
    forest node/.style={circle, inner sep=0pt, text centered},
    arn n/.append style={text=white, font=\sffamily\bfseries, draw=black, fill=black, text width=1.5em},
    arn r/.append style={text=red, draw=red, text width=1.5em, very thick},
  }
  \begin{forest}
    for tree={%
      font=\sffamily,
      forest node,
      edge path={
        \noexpand\path[color=black, -stealth', \forestoption{edge}]
          (!u.parent anchor) -- (.child anchor)\forestoption{edge label};
      },
      l sep+=.25cm,
      s sep+=.25cm,
    }
    [1, arn n
      [2, arn r
        [4, arn n
          [6, arn r]
          [7, arn r]
        ]
        [3, arn r, edge label={node[midway, right]{$x$}}, label={right:A}
          [8, arn n]
          [9, arn n]
        ]
      ]
    ]
  \end{forest}

\end{document}