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

Используяistgameупаковка:

Древовидная структура:

введите описание изображения здесь

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

Связанный контент