처음 두 사례에 대한 코드

처음 두 사례에 대한 코드

나는 오늘날 가장 일반적인 책과 비교하여 내가 배우고 더 직관적으로 표시할 수 있는 오래된 방식으로 AVL을 표현하려고 노력하고 있습니다. 내가 관심 있는 기능이 포함된 원본 이미지는 다음과 같습니다.니클라우스 워스(Niklaus Wirth) 1986년 책그리고 손으로 쓴 메모도 있어요.

시각화는 구체적일 수 있지만 AVL을 표현하는 것은 컴퓨터 과학의 일반적인 데이터 구조 개념이며 실제로 이러한 그림을 강의 노트에 사용하려는 의도입니다. 그래서 저는 그것이 더 많은 청중에게 유용할 수 있다고 믿습니다.

여기에 이미지 설명을 입력하세요

이 책(책)에서 나는 표시된 내용을 정확히 얻으려고 노력하고 있습니다. Some nodes as circles, some as long vertical rectangles, 점선은 트리의 아래쪽 부분을 강조 표시하고 단일 노드에는 제거 노드를 나타내는 X가 있습니다. 일부 직사각형은 그림에 표시된 대로 위쪽, 내부 및 아래쪽에 있습니다. 나는 점선을 일종의 tikz 노드로 표현하고 그에 따라 위치를 지정하려고 노력했지만 운이 없었습니다.


여기에 이미지 설명을 입력하세요

numbers around the nodes (-2, -1, h-1, etc)손으로 쓴 메모에서 직사각형에 문자 'A,B,C,D'를 가져오려고 합니다 circle oriented arrows to indicate rotation direction.


주변의 숫자를 표현하는 방법을 찾을 수 있었습니다다른 질문또한 대표하려고 노력하십시오삼각형 모양의 나무 부분하지만 최소한의 예제를 내가 원하는 대로 수정하는 것은 특히 어려웠습니다.

답변1

TikZ 기반forest패키지비교적 효율적으로 일을 수행하는 것 같습니다. 다음 예제를 통해 시작해보세요.

두 가지 노드 스타일을 정의합니다.

  • circnode노드를 원으로 포맷합니다.
  • rectnode=<num><num>노드를 높이가 x 와 같은 직사각형으로 형식화합니다..5cm

\crossnode또한 교차된 상자를 지정된 위치에 추가하는 매크로를 정의합니다 (귀하의 예에서는 일반적으로 (some rectnode.south)).

마지막으로 두 개의 점선을 수동으로 그려 배경 레이어에 배치합니다.

처음 두 사례에 대한 코드

\documentclass{article}
\usepackage{forest}
\usetikzlibrary{backgrounds}


\forestset{
  circnode/.style={circle,draw,minimum size=.5cm,inner sep=0},
  rectnode/.style={draw,minimum width=.5cm,fill=white,minimum height=#1*.5cm,child anchor=north,anchor=north},
}
\newcommand\crossnode[2][anchor=north]{
  \node(temp)[draw,fill=white,minimum size=.5cm,yshift=\pgflinewidth,#1]at(#2){};
  \draw(temp.north west)--(temp.south east) (temp.north east)--(temp.south west);
}

\begin{document}
\begin{forest}
  for tree={l+=.5cm,s sep=1cm},
  [
    [B,circnode
      [A,circnode
        [,rectnode=3,name=n][,rectnode=3]
      ]
      [,rectnode=3]
    ]
  ]
  \crossnode[anchor=north]{n.south}
  \begin{scope}[on background layer]
    \draw[dashed,thin] let \p1=(temp.north), \p2=(current bounding box.west), \p3=(current bounding box.east) in
    (\x2-.5cm,\y1)--(\x3+.5cm,\y1) (\x2-.5cm,\y1+.5cm)--(\x3+.5cm,\y1+.5cm);
  \end{scope}
\end{forest}

\begin{forest}
  for tree={l+=.5cm,s sep=1cm},
  [
    [C,circnode
      [A,circnode,
        [,rectnode=3]
        [B,circnode
          [,rectnode=2,name=n1]
          [,rectnode=2,name=n2]
        ]
      ]
      [,rectnode=3]
    ]
  ]
  \crossnode{n1.south}
  \crossnode{n2.south}
  \begin{scope}[on background layer]
    \draw[dashed,thin] let \p1=(temp.north), \p2=(current bounding box.west), \p3=(current bounding box.east) in
    (\x2-.5cm,\y1)--(\x3+.5cm,\y1) (\x2-.5cm,\y1+.5cm)--(\x3+.5cm,\y1+.5cm);
  \end{scope}
\end{forest}
\end{document}

산출

여기에 이미지 설명을 입력하세요

관련 정보