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}

관련 정보