如何使用 tikz 绘制如下图形

如何使用 tikz 绘制如下图形

我需要生成如下两个图表。我不知道如何使用 graphviz 来完成这项工作。我遇到的一个问题是这些节点的位置;无论我怎么尝试,位置就是跟不上。

请帮忙。谢谢。 在此处输入图片描述 在此处输入图片描述

答案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}

相关内容