二叉红黑树,添加一个指针?

二叉红黑树,添加一个指针?

我想说明红黑树的旋转算法。如何在叶子“5”上绘制外部(和临时)指针。箭头必须来自左上方并带有标签(例如“y”)。

我的(不完整的)例子

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetikzlibrary{positioning}

\tikzstyle{arn_n} = [circle, white, font=\bfseries, draw=black, fill=black, align=center, inner sep=0pt,text width=1.5em, text centered]% black node
\tikzstyle{arn_r} = [circle, red, draw=red, align=center, inner sep=0pt,text width=1.5em, text centered, very thick]% red node
\tikzstyle{arn_x} = [rectangle, draw=black, align=center, inner sep=0pt,minimum width=0.5em, minimum height=0.5em]% NIL 'node'

\begin{document}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}] 
\node [arn_n] {33}
    child{ node [arn_r] {15} 
        child{ node [arn_n] {10} 
            child{ node [arn_r] {5} 
                   edge from parent node[above left] {$x$}} % name of pointer
            child{ node [arn_x] {}}
        }
        child{ node [arn_n] {20}
            child{ node [arn_r] {18}}
            child{ node [arn_x] {}}
        }                            
    }
    child{ node [arn_r] {47}
        child{ node [arn_n] {38} 
            child{ node [arn_r] {36}}
            child{ node [arn_r] {39}}
        }
        child{ node [arn_n] {51}
            child{ node [arn_r] {49}}
            child{ node [arn_x] {}}
        }
    }
; 
\end{tikzpicture}
\end{document}

答案1

您可以使用name所需的节点使其在\draw命令中可用,然后使用类似

\draw [<-, ultra thick] (leaf 5) -- node [auto] {$y$} ++(135:1cm) ;

添加带标签的箭头:

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usetikzlibrary{positioning}

\tikzstyle{arn_n} = [circle, white, font=\bfseries, draw=black, fill=black, align=center, inner sep=0pt,text width=1.5em, text centered]% black node
\tikzstyle{arn_r} = [circle, red, draw=red, align=center, inner sep=0pt,text width=1.5em, text centered, very thick]% red node
\tikzstyle{arn_x} = [rectangle, draw=black, align=center, inner sep=0pt,minimum width=0.5em, minimum height=0.5em]% NIL 'node'

\begin{document}

\begin{tikzpicture}[->,>=stealth',level/.style={sibling distance = 5cm/#1, level distance = 1.5cm}] 
\node [arn_n] {33}
    child{ node [arn_r] {15} 
        child{ node [arn_n] {10} 
            child{ node [arn_r, name=leaf 5] {5} 
                   edge from parent node[above left] {$x$}} % name of pointer
            child{ node [arn_x] {}}
        }
        child{ node [arn_n] {20}
            child{ node [arn_r] {18}}
            child{ node [arn_x] {}}
        }                            
    }
    child{ node [arn_r] {47}
        child{ node [arn_n] {38} 
            child{ node [arn_r] {36}}
            child{ node [arn_r] {39}}
        }
        child{ node [arn_n] {51}
            child{ node [arn_r] {49}}
            child{ node [arn_x] {}}
        }
    }
; 

\draw [<-, ultra thick] (leaf 5) -- node [auto] {$y$} ++(135:1cm) ;
\end{tikzpicture}
\end{document}

相关内容