我的红黑树版本

我的红黑树版本

这是我的代码

\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}

我想在每个箭头旁边写点东西...我尝试使用edge from parent node[above left]{$x$}

但它把整个事情搞乱了,可能是因为我把它插入到了错误的位置。

另外,我想在每个圆圈旁边写些东西。有办法吗?

请帮助!

答案1

版本forest仅仅因为:

黑色和红色的森林树木

\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}

答案2

这是在每个节点旁边写标签的一种可能方法吗?使用(例如,您当前的节点)来演示该技能。此技能也可以在圆圈旁边放置文本。

 node[above right = 0.8em and -0.5em]{$x$} % or simply `above=0.8 em` will do.
 node[right = 1em]{$A$}

再想想,这也许正是你所期望的。

在此处输入图片描述

代码

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,trees,positioning}

\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} node[above right= 2em and -2em]{$x$} node[right = 1em]{$A$}}
}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

代码

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows,trees,positioning}

\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} node[above right = 0.8em and -0.5em]{$x$} node[right = 1em]{$A$}}
}; 
\end{tikzpicture}
\end{document}

答案3

如果您不介意编译,lualatex那么您可以尝试随附的图形绘制功能PGF 3.0(注意:我不认为这是有效的红黑树):

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{graphs,graphdrawing,arrows.meta,quotes}
\usegdlibrary{trees}
\begin{document}
\begin{tikzpicture}[>=Stealth]

\graph [tree layout,
  nodes={shape=circle, ultra thick, font=\sffamily, Sentinel},
  Red/.style={shape=circle, text=red,   fill=white, draw=red},
  Black/.style={shape=circle, text=white, fill=black, draw=black},
  Sentinel/.style={shape=rectangle, fill=black}]{

 1[Black] -> 2[Red] -> { 
   4[Black] -> {
     6[Red, >"$a$", label=180:$b$] -> {/,/}, 
     7[Red] -> {/,/}
   },
   3[Red, >"$x$", label=0:$y$] -> {/,/}
 };

};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容