Tikz를 사용하여 트리에 점/선 괄호 만들기

Tikz를 사용하여 트리에 점/선 괄호 만들기

나는 tikz를 사용하여 그림과 같은 점을 만들 수 있는지 스스로에게 묻고 있습니다.

나무의 예

암호

\begin{tikzpicture}

\tikzstyle{level 1}=[sibling distance=60mm]
\tikzstyle{level 2}=[sibling distance=33mm]
\tikzstyle{level 3}=[sibling distance=10mm]
\tikzstyle{level 4}=[sibling distance=5mm]
\node {n} 
    child {node {$\frac{n}{3}$}
        child {node {$\frac{n}{9}$}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}
        child {node {$\frac{n}{9}$}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}}
        child {node {$\frac{n}{3}$}
        child {node {$\frac{n}{9}$}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}
        child {node {$\frac{n}{9}$}
                        child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}}
        child {node {$\frac{n}{3}$}
            child{node{$\frac{n}{9}$}
                            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}
            child{node{$\frac{n}{9}$}
                            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}
            child{node {$\frac{n}{27}$}}}
    };

그리고 마지막 자식 노드와 자식 노드 사이에 점선을 시작하고 싶습니다.

답변1

다음은 Tikz를 사용한 솔루션입니다. 코드는 다음을 기반으로 했습니다.재귀 트리의 예이지만 예시 이미지에 맞게 크게 수정되었습니다. 실제 분수가 되기를 원하는지 확실하지 않지만 cn/n아래 코드부터 쉽게 수정할 수 있습니다.

산출

구성표

암호

\documentclass[margin=10pt]{standalone} 
\usepackage{tikz}
\usepackage{tikz-qtree}
\usetikzlibrary{trees,calc,arrows.meta,positioning,decorations.pathreplacing,bending}

\tikzset{
    edge from parent/.style={draw, thick, blue!70!black},
    no edge from this parent/.style={
        every child/.append style={
        edge from parent/.style={draw=none}}},
    level 3/.style={yshift=5cm},
    level 4/.style={level distance=5mm} 
         }

\begin{document}

\begin{tikzpicture}[
    level/.style={sibling distance=40mm/#1},
    text=blue!70!black,
    >=latex,
    font=\sffamily
    ]

\node (z){cn} 
  child {node (a) {cn/2}
    child {node  (b) {cn/4}
      child {node (b1) {$\vdots$}[no edge from this parent]
       child {node (b11) {c}}
      }
      child {node (b2) {$\vdots$}[no edge from this parent]
       child {node (b12) {c}}
      }
    }
    child {node (g) {cn/4}
      child {node (g1) {$\vdots$}[no edge from this parent]
       child {node (g11) {c}}
      }
      child {node (g2) {$\vdots$}[no edge from this parent]
       child {node (g12) {c}}
      }
    }
  }
    child {node (d) {cn/2}
      child {node  (e) {cn/4}
        child {node (e1) {$\vdots$}[no edge from this parent]
         child {node (e11) {c}}
        }
        child {node (e2) {$\vdots$}[no edge from this parent]
         child {node (e12) {c}}
        }
      }
      child {node (f) {cn/4}
        child {node (f1) {$\vdots$}[no edge from this parent]
         child {node (f11) {c}}
        }
        child {node (f2) {$\vdots$}[no edge from this parent]
         child {node (f12) {c}
         }
         }
  }
};

\node[left=5 of z]  (ln1) {cn}[no edge from this parent]
    child {node (ln2) {cn}[no edge from this parent]
        child {node (ln3) {cn}[no edge from this parent]
            child {node (ln4) {}[no edge from this parent]
                child {node (ln5) {cn}}}}};

\path (b12.north east) -- (g11.north west) node [midway] {$\cdots$};
\path (e12.north east) -- (f11.north west) node [midway] {$\cdots$};

\coordinate (cd1) at ($(f12)+(1,0)$);
\coordinate (nb1) at ($(g12)!.5!(e11)$);

\draw[blue!70!black,thick,<->,] 
    (cd1) -- (cd1|-z.east) node [near start, fill=white] {log(n)};

\draw[blue!70!black,dashed,thick,->]    
    ($(z.west)+(-1em,0)$) -- (ln1);
\draw[blue!70!black,dashed,thick,->]    
    ($(a.west)+(-1em,0)$) -- (ln2.east);
\draw[blue!70!black,dashed,thick,->]    
    ($(b.west)+(-1em,0)$) -- (ln3);
\draw[blue!70!black,dashed,thick,->]    
    ($(b11.west)+(-1em,0)$) -- (ln5);

\draw[blue!70!black,thick,decorate,decoration={brace,amplitude=10pt,mirror},->,-{latex[flex=1pt]}] (b11.south west) -- (f12.south east);
\end{tikzpicture}
\end{document}

답변2

를 사용한 솔루션은 다음과 같습니다 forest. 이는 대상 이미지의 '이렇게'를 취한다는 점에 유의하십시오.극도로진지하게. 이는 메인 트리의 실제 사양이 매우 간결하다는 것을 의미합니다.

   [c, name=node 1
     [, name=node 2 [, name=node 3 [][]][[][]]][[[][]][[][]]]
   ]

분명히 덜 일반적인 것을 원한다면 노드의 내용을 직접 지정할 수 있습니다.

또한 내가 하는 방식으로 노드 4-13의 레이블을 지정할 이유가 실제로 없다는 점에 유의하십시오. 특히 10~13번 노드의 사양은 사실 단지 노드 워크를 조금 실험해보고 싶었기 때문입니다.

\documentclass[tikz,border=5pt]{standalone}
\usepackage{forest,textcomp}
\usetikzlibrary{positioning,arrows.meta,decorations.pathreplacing}
\begin{document}
 \begin{forest}
   for tree={
     font=\sffamily,
     before typesetting nodes={
       if={level>0}{
         if n children=0{
           append/.wrap pgfmath arg={
             [#1, font=\sffamily, edge=dotted]
           }{content("!r")},
           content={},
         }{
           content/.wrap 2 pgfmath args={#2n\textfractionsolidus#1}{int(level()*2)}{content("!r")}
         }
       }{
         delay n=1{
           content/.wrap value={##1n},
           node walk={
             every step/.style={alias=node 4}, F,
             every step/.style={alias=node 10}, N,
             every step/.style={alias=node 11}, N,
             every step/.style={alias=node 9},
             node walk={
               rL,
               node walk={
                 every step/.style={alias=node 12}, P,
                 every step/.style={alias=node 13}, P}
             }
           }
         }
       }
     },
   }
   [c, name=node 1
     [, name=node 2 [, name=node 3 [][]][[][]]][[[][]][[][]]]
   ]
   \begin{scope}[font=\sffamily, >=Triangle]
     \node (node 5) [left=25pt of node 4] {cn};
     \foreach \i/\j in {6/3,7/2,8/1} \node (node \i) at (node 5 |- node \j) {cn};
     \foreach \i/\j in {1/8,2/7,3/6,4/5} \draw [densely dashed, ->] (node \i.west) -- (node \j.east);
     \path [draw, decorate, decoration={brace, amplitude=10pt}] (node 9.south east) -- (node 4.south west);
     \path [draw, <->] (node 9.south east) +(15pt,0) coordinate (c) -- (c |- node 1.north) node [pos=.4, fill=white] {log(n)};
   \end{scope}
   \draw [dotted] (node 10.north east) -- (node 11.north west) (node 13.north east) -- (node 12.north west);
  \end{forest}
\end{document}

노드 워크 트리

관련 정보