在 Graphviz 的嵌套子图中放置节点

Placement of nodes in a nested subgraph in Graphviz

我正在尝试创建一个二元图,用不同的颜色着色和标记不同的节点。

为了获得子树周围的边界,我使用了几乎完美无缺的子图:

graph G
{
graph [ranksep="0.25", nodesep="0.25"]
rankdir = TB;
node [shape=ellipse, style=filled, fillcolor="#0068B4", color=white, fontcolor=white, penwidth=10]
edge [arrowtail="none"]

subgraph cluster_0 {
  node [fillcolor="#99CC00"]
  color="#99CC00"
  style=filled
  fillcolor=white
  fontcolor="#99CC00"
  label="12 is the root of\n26's left child\r"
  18;
    subgraph cluster_01 {
      node [fillcolor="#00B0F0"]
      color="#00B0F0"
      fontcolor="#00B0F0"
      style=filled
      fillcolor=white
      label="4 is the root of                         \n12's left child                         \r"
      7   [fillcolor=white]
      4 -- 13
      4 --  7 [style=invisible]
      7 -- 13 [style=invisible] 
      { rank=same; 7, 13 }
      { rank=same; 4, 18 }
      
    }


  12 -- {4,18}
  
  { rank=same; 12 }
}

subgraph cluster_1 {
  node [fillcolor="#C00000"]
  color="#C00000"
  style=filled
  fillcolor=white
  fontcolor="#C00000"
  label="32 is the root of\n26's right child\r"
  
  35   [fillcolor=white]

  32 -- 38
  32 -- 35 [style=invisible]
  35 -- 38 [style=invisible] 
  
  { rank=same; 32 }
  { rank=same; 35, 38 }
}


26 -- {12, 32}


{ rank=same; 26 }

}

导致此输出:

如您所见,节点 18 位于蓝色子集群内。除了在 4 和 18 之间放置一个空白虚拟节点(如不可见节点 7 和 35)之外,是否有一种简单的方法可以将该节点“开箱即用”?

但是,添加 graph [newrank=true] 并重新排列几行会使事情变得更好:

graph G
{
graph [ranksep="0.25", nodesep="0.25" newrank=true]
rankdir = TB;
node [shape=ellipse, style=filled, fillcolor="#0068B4", color=white, fontcolor=white, penwidth=10]
edge [arrowtail="none"]

subgraph cluster_0 {
  node [fillcolor="#99CC00"]
  color="#99CC00"
  style=filled
  fillcolor=white
  fontcolor="#99CC00"
  label="12 is the root of\n26's left child\r"
    subgraph cluster_01 {
      node [fillcolor="#00B0F0"]
      color="#00B0F0"
      fontcolor="#00B0F0"
      style=filled
      fillcolor=white
      label="4 is the root of                         \n12's left child                         \r"
      7   [fillcolor=white]
      4 -- 13
      4 --  7 [style=invisible]
      7 -- 13 [style=invisible] 
      { rank=same; 7, 13 }
    }
      { rank=same; 4, 18 }
  12 -- {4,18}
  { rank=same; 12 }
}

subgraph cluster_1 {
  node [fillcolor="#C00000"]
  color="#C00000"
  style=filled
  fillcolor=white
  fontcolor="#C00000"
  label="32 is the root of\n26's right child\r"
  35   [fillcolor=white]
  32 -- 38
  32 -- 35 [style=invisible]
  35 -- 38 [style=invisible] 
  { rank=same; 32 }
  { rank=same; 35, 38 }
}
26 -- {12, 32}
{ rank=same; 26 }
}

给出: