Search code examples
graphviz

How to display identical captions of nodes


I wanted to draw RB-Tree as in Wiki 1, 1

but I can't find a possibility of display of the identical caption for different nodes(in this case nil - nodes). It is possible?

  digraph "rb-tree"{
  bgcolor = whitesmoke;
  forcelabels = true;
  margin = 0;
  node [shape = circle,
        style = filled,
        fontsize = 14,
        margin = 0,
        fillcolor = black,
        fontcolor = white];
  edge [fontsize = 10,
        arrowhead = vee];
8 [fillcolor = red];
17 [fillcolor = red];
nil_8l [shape = box];
nil_8r [shape = box];
nil_17l [shape = box];
nil_17r [shape = box];
13->8;
13->17;
8->nil_8l;
8->nil_8r;
17->nil_17l;
17->nil_17r;
}

Solution

  • Graphiz has the possibility to set labels. So use:

     digraph "rb-tree"{
      bgcolor = whitesmoke;
      forcelabels = true;
      margin = 0;
      node [shape = circle,
            style = filled,
            fontsize = 14,
            margin = 0,
            fillcolor = black,
            fontcolor = white];
      edge [fontsize = 10,
            arrowhead = vee];
    8 [fillcolor = red];
    17 [fillcolor = red];
    nil_8l [shape = box label="nil"];
    nil_8r [shape = box label="nil"];
    nil_17l [shape = box label="nil"];
    nil_17r [shape = box label="nil"];
    13->8;
    13->17;
    8->nil_8l;
    8->nil_8r;
    17->nil_17l;
    17->nil_17r;
    }