Tikz:树和反向树结合在一起

Tikz:树和反向树结合在一起

我需要在 tikz 中重现以下图像:

在此处输入图片描述

我先画上部:“正常”的树。我用森林来做这件事。

但是,我不知道如何做下部......“反转树”(和“合并线”)。

下面是我写的代码:

\documentclass[tikz,border=10pt]{standalone}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}
  [
    {$\left[3,2,1,3,5\right]$}
    [{$\left[3,2,1\right]$}
        [{$\left[3,2\right]$}
            [{$\left[3\right]$}]
            [{$\left[2\right]$}]
        ]
        [{$\left[1\right]$}]
    ]
    [{$\left[3,5\right]$}
        [{$\left[3\right]$}]
        [{$\left[5\right]$}]

    ]
  ]
\end{forest}

\end{document}

谢谢你的帮助。

答案1

这就是你想要的吗?(我不知道该怎么办forest)。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[yscale=1.5,
n/.style={circle,draw,minimum size=9mm}]
\path
(0,0)     node    (1a) {[3 2 1 3 5]}
(-2,-1)   node    (2a) {[3 2 1]}
+(-1,-1)  node    (3a) {[3 2]}
+(1,-1)   node[n] (3b) {[1]}
(2,-1)    node    (2b) {[3 5]}
+(-1,-1)  node[n] (3c) {[3]}
+(1,-1)   node[n] (3d) {[5]}
(3a.center)
+(-.8,-1) node[n] (4a) {[3]}
+(.8,-1)  node[n] (4b) {[2]}
(4a-|1a)  coordinate (M)
(2a|-M)   coordinate (N);

\path
($(4a)+(4b)-(3a)$) node (5a) {[2 3]}
($(2a)!2!(N)$)     node (6a) {[1 2 3]}
(6a-|2b)           node (6b) {[3 5]}
($(1a)!2!(M)$)     node (7a) {[1 2 3 3 5]};

\draw[blue] 
([shift={(-90:.5)}]M)--+(0:4)--+(180:6) 
node[right,shift={(-90:.3)}]{Merging};
\draw 
(1a)--(2a) (1a)--(2b)
(2a)--(3a) (2a)--(3b) 
(2b)--(3c) (2b)--(3d)
(3a)--(4a) (3a)--(4b);
\draw[dashed] 
(5a)--(4a) (5a)--(4b)
(6a)--(3b) (6a)--(5a)
(6b)--(3c) (6b)--(3d)
(7a)--(6a) (7a)--(6b);
\end{tikzpicture}
\end{document}

答案2

人们总是可以向森林添加一些东西,但在这个例子中,树的下部远不如上部那么自动化/优雅。

\documentclass[tikz,border=10pt]{standalone}
\usepackage[linguistics]{forest}
\begin{document}
\begin{forest}
for tree={if n children=0{circle,draw,inner sep=1pt}{}}
  [{$\left[3,2,1,3,5\right]$}
    [{$\left[3,2,1\right]$}
        [{$\left[3,2\right]$}
            [{$\left[3\right]$},alias=a1]
            [{$\left[2\right]$},alias=a2]
        ]
        [{$\left[1\right]$},alias=a3]
    ]
    [{$\left[3,5\right]$}
        [{$\left[3\right]$},alias=a4]
        [{$\left[5\right]$},alias=a5]
    ]
  ]
\path ($(a1)!0.5!(a2)$) coordinate(aux1) 
($(a2)!0.5!(a3)$) coordinate(aux2) 
($(a4)!0.5!(a5)$) coordinate(aux3) foreach \X in {1,2,3} {($(a1)+(0,-\X)$) coordinate
(l\X)} 
 (aux1|-l1)  node(b1) {$[2,3]$}  
 (aux2|-l2)  node(b2) {$[1,2,3]$}
 (aux3|-l2)  node(b3) {$[3,5]$}
  (aux3|-l3)  node(b4) {$[1,2,3,3,5]$} ;
\draw[dashed] (a1) -- (b1) -- (b2) (a2)-- (b1) (a3) -- (b2) (a4) -- (b3) (a5) -- (b3)
 (b2) -- (b4) -- (b3) ;  
\end{forest}
\end{document}

在此处输入图片描述

答案3

使用游戏包裹:

树结构:

在此处输入图片描述

\documentclass[tikz]{standalone}
    
\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
%%% tree structure:
\tikzset{oval node/.style={ellipse node,draw=none}}
\tikzset{circle node/.style={ellipse node,minimum size=1.6em}}
%%% upper part
\xtdistance{15mm}{30mm}
\istrooto(0){[3 2 1 3 5]}   \istb  \istb  \endist
% upper left
\xtdistance{15mm}{15mm}
\istrooto(L1)(0-1){[3 2 1]} \istb  \istb  \endist
\istrooto(L2)(L1-1){[3 2]}  \istb  \istb  \endist
\istrooto(L3)(L1-2)[circle node]{[1]}     \endist
\istrooto(L4)(L2-1)[circle node]{[3]}     \endist
\istrooto(L5)(L2-2)[circle node]{[2]}     \endist
% upper right
\xtdistance{15mm}{15mm}
\istrooto(R1)(0-2){[3 5]}   \istb  \istb  \endist
\istrooto(R2)(R1-1)[circle node]{[3]}     \endist
\istrooto(R3)(R1-2)[circle node]{[5]}     \endist
%%% lower part (copy from upper part)
\setistgrowdirection'{north}
\xtdistance{15mm}{30mm}
\istrooto(0)(0,-90mm){[1 2 3 4 5]}  % root position
                            \istb[dashed] \istb[dashed] \endist
% lower left
\xtdistance{15mm}{15mm}
\istrooto(L1)(0-1){[3 2 1]} \istb[dashed] \istb[dashed] \endist
\istrooto(L2)(L1-1){[3 2]}  \istb[dashed] \istb[dashed] \endist
\istrooto(L3)(L1-2)[circle node]{[1]}     \endist
\istrooto(L4)(L2-1)[circle node]{[3]}     \endist
\istrooto(L5)(L2-2)[circle node]{[2]}     \endist
% lower right
\xtdistance{15mm}{15mm}
\istrooto(R1)(0-2){[3 5]}   \istb[dashed] \istb[dashed] \endist
\istrooto(R2)(R1-1)[circle node]{[3]}     \endist
\istrooto(R3)(R1-2)[circle node]{[5]}     \endist

\end{istgame}

\end{document}

微调:

在此处输入图片描述

\documentclass[tikz]{standalone}
    
\usepackage{istgame}

\begin{document}

\begin{istgame}[font=\scriptsize]
%%% fine tuning:
\tikzset{oval node/.style={ellipse node,draw=none}}
\tikzset{circle node/.style={ellipse node,minimum size=1.6em}}
%%% upper part
\xtdistance{15mm}{30mm}
\istrooto(0){[3 2 1 3 5]}   \istb  \istb  \endist
% upper left
\xtdistance{15mm}{15mm}
\istrooto(L1)(0-1){[3 2 1]} \istb  \istb  \endist
\istrooto(L2)(L1-1){[3 2]}  \istb  \istb  \endist
\istrooto(L3)(L1-2)[circle node]{[1]}     \endist
%\istrooto(L4)(L2-1)[circle node]{[3]}     \endist
%\istrooto(L5)(L2-2)[circle node]{[2]}     \endist
% upper right
\xtdistance{15mm}{15mm}
\istrooto(R1)(0-2){[3 5]}   \istb  \istb  \endist
%\istrooto(R2)(R1-1)[circle node]{[3]}     \endist
%\istrooto(R3)(R1-2)[circle node]{[5]}     \endist
%%% lower part (copy from upper part)
\setistgrowdirection'{north}
\xtdistance{15mm}{30mm}
\istrooto(0)(0,-90mm){[1 2 3 3 5]}  % root position
                            \istb[dashed] \istb[dashed] \endist
% lower left
\xtdistance{15mm}{15mm}
\istrooto(L1)(0-1){[1 2 3]} 
  \istb[dashed] 
  \istb<level distance=45mm>[dashed]      %% 45mm
  \endist
\istrooto(L2)(L1-1){[2 3]}  \istb[dashed] \istb[dashed] \endist
\istrooto(L3)(L1-2)[circle node]{[1]}     \endist
\istrooto(L4)(L2-1)[circle node]{[3]}     \endist
\istrooto(L5)(L2-2)[circle node]{[2]}     \endist
% lower right
\xtdistance{45mm}{15mm}                   %% 45mm
\istrooto(R1)(0-2){[3 5]}   \istb[dashed] \istb[dashed] \endist
\istrooto(R2)(R1-1)[circle node]{[3]}     \endist
\istrooto(R3)(R1-2)[circle node]{[5]}     \endist

\end{istgame}

\end{document}

相关内容