tikz를 사용하여 다음과 같은 그래프를 그리는 방법

tikz를 사용하여 다음과 같은 그래프를 그리는 방법

다음 두 가지와 같은 그래프를 생성해야 합니다. graphviz를 사용하여 작업을 완료하는 방법을 알 수 없습니다. 내가 직면한 한 가지 문제는 이러한 노드의 위치입니다. 아무리 노력해도 pos가 따라오지 않습니다.

도와주세요. 감사합니다. 여기에 이미지 설명을 입력하세요 여기에 이미지 설명을 입력하세요

답변1

또 다른 출발점으로 ...

편집하다:
이제 완료되었습니다 :-)

\documentclass[margin=3mmf]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc,
                fit,
                positioning,
                shapes.arrows}

\begin{document}
    \begin{tikzpicture}[ 
node distance = 4mm and 4mm,
A/.style = {single arrow, draw, minimum height=22mm},
 every edge/.append style = {draw, -{Stealth[scale=0.8]}},
every label/.append style = {inner ysep=2ex, font=\small, text=red, align=center},
F/.style = {draw, densely dashed, very thin, rounded corners, fit=#1,
            node contents={}},
V/.style = {%Vertex
            circle, draw=#1, fill=#1!30, minimum size=1em, inner sep=2pt},
V/.default = gray
                        ]
\node (v1) [V=olive] {1};
\node (v2) [V, below right=of v1] {5};
\node (v3) [V, below right=of v2] {4};
%
\node (v4) [V, below=of v3] {1};
\node (v5) [V=olive, below=of v4] {1};
%
\node (v6) [V, above right=of v3] {3};
\node (v7) [V=olive, above=of v6] {2};
\node (v8) [V=olive, right=of v6] {1};
%%
\node[F=(v1) (v2)];
\node[F=(v4) (v5)];
\node[F=(v7) (v8)];
%%%
\draw   (v2) -- (v3) -- (v4)
                (v3) -- (v6);
\draw   (v1) edge (v2) 
        (v7) edge (v6) 
        (v8) edge (v6)
        (v5) edge (v4); 
%%%%
\node (A) [A, right=of v3 -| v8,
           label=below:{push leaf values\\ to parents}] {};      
%%%%%
\node (v11) [V, right=of v8 -| A.east] {6};
\node (v12) [V, below right=of v11] {4};
\node (v13) [V, below=of v12] {2};
%
\node (v14) [V,above right=of v12] {6};
\draw (v11) -- (v12) -- (v13)
               (v12) -- (v14);
    \end{tikzpicture}
\end{document}

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

답변2

이에 대한 빠른 시작:

부모 흡수

\documentclass[tikz,border=3.14mm]{standalone}

\usetikzlibrary{positioning,fit,arrows.meta}

\tikzset{
    every node/.style={circle,draw,fill=gray!20,inner sep=0pt,minimum size=5mm},
    childnode/.style={fill=olive!30},
    fitnode/.style={fill=none, rectangle, inner sep= 2pt,densely dashed,rounded corners},
    arr/.style={-Stealth}
    }

\begin{document}
    \begin{tikzpicture}[node distance=6mm]
        \node (root) {4};
        
        \node[above left= of root] (A1) {5};
        \node[above left= of A1,childnode] (A2) {1};
        
        \node[above right= of root] (B1) {3};
        \node[above = of B1,childnode] (B2) {2};
        \node[right = of B1,childnode] (B3) {1};
        
        \node[below= of root] (C1) {4};
        \node[below= of C1] (C2) {1};
        \node[below= of C2,childnode] (C3) {1};
        
        \path   (A2) edge[arr] (A1)
                (A1) edge (root)
                (B2) edge[arr] (B1)
                (B3) edge[arr] (B1)
                (B1) edge (root)
                (C3) edge[arr] (C2)
                (C2) edge (C1)
                (C1) edge (root)
                ;
        
        \node[fitnode,fit=(A1) (A2)] {};
        \node[fitnode,fit=(B2) (B3)] {};
        \node[fitnode,fit=(C1) (C3)] {};
    \end{tikzpicture}
\end{document}

관련 정보