You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Graphviz在单张图中绘制两棵树:一棵自上而下,一棵自下而上

Drawing Two Trees (Top-Down + Bottom-Up) in a Single Graphviz Diagram

Hey there! Let's get your two trees rendered side-by-side in one Graphviz graph—one growing top-down, the other bottom-up. Your existing code has a solid start with subgraphs, but we need to tweak a few things to make the layout behave as expected, plus finish the incomplete second tree.

Key Adjustments to Make:

  • Use cluster subgraphs (prefix with cluster_) to group each tree into a distinct visual block—this helps Graphviz layout them cleanly side-by-side.
  • For the bottom-up tree, set rankdir=BT (bottom-to-top) in its subgraph to reverse the default layout direction.
  • Complete the node connections for the second tree to build a proper hierarchy.

Complete Working Code

digraph TwoTrees {
    // Global styling for better readability
    node [shape=ellipse, style=filled, fillcolor=white];
    edge [arrowhead=vee];

    // Top-Down Tree (default rankdir=TB: top-to-bottom)
    subgraph cluster_top_down {
        label = "Top-Down Tree";
        color = #2ecc71;

        s00 [label="s00", fillcolor=#e8f5e9];
        s00 -> {s10 s11 s12};
        
        s10 [label="s10", fillcolor=#e8f5e9];
        s10 -> {s22 s23};
        
        s11 [label="s11", fillcolor=#e8f5e9];
        s12 [label="s12", fillcolor=#e8f5e9];
        
        s22 [label="s22", fillcolor=#e8f5e9];
        
        s23 [label="s23", fillcolor=#e8f5e9];
        s23 -> {s30 s31};
        
        s30 [label="s30", fillcolor=#e8f5e9];
        s31 [label="s31", fillcolor=#e8f5e9];
    }

    // Bottom-Up Tree (rankdir=BT: bottom-to-top)
    subgraph cluster_bottom_up {
        label = "Bottom-Up Tree";
        rankdir=BT;
        color = #3498db;

        g00 [label="g00", fillcolor=#e3f2fd];
        g10 [label="g10", fillcolor=#e3f2fd];
        g11 [label="g11", fillcolor=#e3f2fd];
        g111 [label="g111", fillcolor=#e3f2fd];
        
        // Child nodes point upward to parent (matches bottom-up flow)
        g10 -> g00;
        g11 -> g00;
        g111 -> g00;
        
        // Add deeper nodes to complete the tree structure
        g20 [label="g20", fillcolor=#e3f2fd];
        g21 [label="g21", fillcolor=#e3f2fd];
        g20 -> g11;
        g21 -> g11;
        
        g22 [label="g22", fillcolor=#e3f2fd];
        g22 -> g111;
    }
}

Breakdown of the Changes:

  • The cluster_top_down subgraph uses Graphviz's default top-to-bottom layout, with parent nodes pointing down to their children as you originally wrote.
  • The cluster_bottom_up subgraph uses rankdir=BT to flip the layout—children sit below their parent, and arrows point upward to the root. (If you prefer keeping parent-to-child arrows, you can do that too; the rankdir will still reverse the visual flow.)
  • Added subtle color coding and node styling to make each tree visually distinct at a glance.
  • Filled in the missing connections for the second tree to create a full, hierarchical structure.

You can render this code with any Graphviz tool (like running dot -Tpng two_trees.dot -o two_trees.png in your terminal) to see the final output.

内容的提问来源于stack exchange,提问作者tongbaiming

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:41:13