|
| 1 | +# TODO: how do you set the BACKGROUND COLOR of a GraphViz node ... fill=? |
| 2 | + |
| 3 | +# TODO: make this work in both Python 2 and 3 |
| 4 | + |
| 5 | +from collections import defaultdict |
| 6 | + |
| 7 | +import sys |
| 8 | +is_python3 = (sys.version_info[0] == 3) |
| 9 | + |
| 10 | +if is_python3: |
| 11 | + import io as cStringIO |
| 12 | +else: |
| 13 | + import cStringIO |
| 14 | + |
| 15 | +ID = 0 |
| 16 | + |
| 17 | +class TreeNode: |
| 18 | + def __init__(self, dat): |
| 19 | + self.data = str(dat) # convert to string for easy display in GraphViz |
| 20 | + self.parent = None |
| 21 | + self.left = None |
| 22 | + self.right = None |
| 23 | + |
| 24 | + self.__penwidth = 1 # thickness of node border |
| 25 | + |
| 26 | + # HTML-like RGB hex values - e.g., "#bb0000" |
| 27 | + self.__color = None # border color |
| 28 | + self.__fill = None # internal node color |
| 29 | + |
| 30 | + # assign unique IDs in node creation order |
| 31 | + global ID |
| 32 | + self.id = 'n' + str(ID) |
| 33 | + ID += 1 |
| 34 | + |
| 35 | + def set_border_color(self, col): |
| 36 | + self.__color = col |
| 37 | + |
| 38 | + def set_fill(self, col): |
| 39 | + self.__fill = col |
| 40 | + |
| 41 | + def set_width(self, w): |
| 42 | + assert w > 0 |
| 43 | + self.__penwidth = w |
| 44 | + |
| 45 | + def highlight(self): |
| 46 | + self.__color = 'red' |
| 47 | + self.__penwidth = 2 |
| 48 | + |
| 49 | + def reset_style(self): |
| 50 | + self.__color = None |
| 51 | + self.__fill = None |
| 52 | + self.__penwidth = 1 |
| 53 | + |
| 54 | + def __str__(self): |
| 55 | + ret = '%s[label="%s"' % (self.id, self.data) |
| 56 | + if self.__penwidth > 1: |
| 57 | + ret += ',penwidth=%d' % self.__penwidth |
| 58 | + if self.__color: |
| 59 | + ret += ',color="%s"' % self.__color |
| 60 | + if self.__fill: |
| 61 | + ret += ',fill="%s"' % self.__fill |
| 62 | + ret += ']' |
| 63 | + return ret |
| 64 | + |
| 65 | + |
| 66 | +# render a binary tree of TreeNode objects starting at root in a pretty |
| 67 | +# GraphViz format using the balanced tree hack from |
| 68 | +# http://www.graphviz.org/content/FaqBalanceTree |
| 69 | +def graphviz_render(root, ios, compress=False): |
| 70 | + separator = '\n' |
| 71 | + if compress: |
| 72 | + separator=',' |
| 73 | + ios.write('digraph G{') |
| 74 | + |
| 75 | + queue = [] # each element is (node, level #) |
| 76 | + |
| 77 | + # Key: level number |
| 78 | + # Value: sorted list of node IDs at that level (including phantom nodes) |
| 79 | + nodes_by_level = defaultdict(list) |
| 80 | + |
| 81 | + |
| 82 | + def render_phantom(parent_id, suffix): |
| 83 | + phantom_id = parent_id + '_phantom_' + suffix |
| 84 | + ios.write('%s [label="",width=.1,style=invis]%s' % (phantom_id, separator)) |
| 85 | + ios.write('%s->%s [style=invis]%s' % (parent_id, phantom_id, separator)) |
| 86 | + return phantom_id |
| 87 | + |
| 88 | + def bfs_visit(): |
| 89 | + # base case |
| 90 | + if not queue: |
| 91 | + return |
| 92 | + |
| 93 | + n, level = queue.pop(0) |
| 94 | + |
| 95 | + ios.write(str(n) + separator) # current node |
| 96 | + if n.left or n.right: |
| 97 | + if n.left: |
| 98 | + ios.write('%s->%s%s' % (n.id, n.left.id, separator)) |
| 99 | + queue.append((n.left, level+1)) |
| 100 | + nodes_by_level[level+1].append(n.left.id) |
| 101 | + else: |
| 102 | + # insert phantom to make tree look good |
| 103 | + ph_id = render_phantom(n.id, 'L') |
| 104 | + nodes_by_level[level+1].append(ph_id) |
| 105 | + |
| 106 | + # always insert invisible middle phantom |
| 107 | + ph_id = render_phantom(n.id, 'M') |
| 108 | + nodes_by_level[level+1].append(ph_id) |
| 109 | + |
| 110 | + if n.right: |
| 111 | + ios.write('%s->%s%s' % (n.id, n.right.id, separator)) |
| 112 | + queue.append((n.right, level+1)) |
| 113 | + nodes_by_level[level+1].append(n.right.id) |
| 114 | + else: |
| 115 | + # insert phantom to make tree look good |
| 116 | + ph_id = render_phantom(n.id, 'R') |
| 117 | + nodes_by_level[level+1].append(ph_id) |
| 118 | + |
| 119 | + bfs_visit() # recurse! |
| 120 | + |
| 121 | + queue.append((root, 1)) |
| 122 | + bfs_visit() |
| 123 | + |
| 124 | + if not compress: |
| 125 | + ios.write('\n') |
| 126 | + |
| 127 | + # make sure all nodes at the same level are vertically aligned |
| 128 | + for level in nodes_by_level: |
| 129 | + node_ids = nodes_by_level[level] |
| 130 | + if len(node_ids) > 1: |
| 131 | + ios.write(('{rank=same %s [style=invis]}' % '->'.join(node_ids)) + separator) |
| 132 | + |
| 133 | + ios.write('}') # cap it off |
| 134 | + |
| 135 | + |
| 136 | +def to_graphviz_string(root): |
| 137 | + s = cStringIO.StringIO() |
| 138 | + graphviz_render(root, s, True) |
| 139 | + return s.getvalue() |
| 140 | + |
| 141 | +COUNT = 0 |
| 142 | +def preorder_traversal(root, n): |
| 143 | + if not n: return |
| 144 | + |
| 145 | + global COUNT |
| 146 | + f = open('preorder_' + str(COUNT) + '.dot', 'w') |
| 147 | + n.highlight() |
| 148 | + graphviz_render(root, f) |
| 149 | + n.reset_style() |
| 150 | + f.close() |
| 151 | + COUNT += 1 |
| 152 | + |
| 153 | + preorder_traversal(root, n.left) |
| 154 | + preorder_traversal(root, n.right) |
| 155 | + |
| 156 | + |
| 157 | +# TODO: add a function to create a tree from a nested list/tuple |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + # simple test tree |
| 161 | + a = TreeNode('a') |
| 162 | + |
| 163 | + b0 = TreeNode('b0') |
| 164 | + a.left = b0 |
| 165 | + |
| 166 | + b1 = TreeNode('b1') |
| 167 | + a.right = b1 |
| 168 | + |
| 169 | + c0 = TreeNode('c0') |
| 170 | + b0.left = c0 |
| 171 | + |
| 172 | + c1 = TreeNode('c1') |
| 173 | + b0.right = c1 |
| 174 | + |
| 175 | + c2 = TreeNode('c2') |
| 176 | + b1.left = c2 |
| 177 | + |
| 178 | + d1 = TreeNode('d1') |
| 179 | + c0.right = d1 |
| 180 | + |
| 181 | + d2 = TreeNode('d2') |
| 182 | + c2.left = d2 |
| 183 | + |
| 184 | + d3 = TreeNode('d3') |
| 185 | + c1.left = d3 |
| 186 | + |
| 187 | + d4 = TreeNode('d4') |
| 188 | + c1.right = d4 |
| 189 | + |
| 190 | + #f = open('test.dot', 'w') |
| 191 | + #graphviz_render(a, f, True) |
| 192 | + #f.close() |
| 193 | + print to_graphviz_string(a) |
| 194 | + |
| 195 | + #preorder_traversal(a, a) |
| 196 | + |
| 197 | + |
| 198 | +''' |
| 199 | +/* balanced tree hack from http://www.graphviz.org/content/FaqBalanceTree */ |
| 200 | +
|
| 201 | +/* |
| 202 | +digraph G { |
| 203 | + a -> b0 |
| 204 | + xb [label="",width=.1,style=invis] |
| 205 | + a -> xb [style=invis] |
| 206 | + a -> b1 |
| 207 | +
|
| 208 | + {rank=same b0 -> xb -> b1 [style=invis]} |
| 209 | +
|
| 210 | + b0 -> c0 |
| 211 | + xc [label="",width=.1,style=invis] |
| 212 | + b0 -> xc [style=invis] |
| 213 | + b0 -> c1 |
| 214 | +
|
| 215 | + {rank=same c0 -> xc -> c1 [style=invis]} |
| 216 | +} |
| 217 | +*/ |
| 218 | +''' |
| 219 | + |
| 220 | +''' |
| 221 | +from bintree_module import TreeNode, to_graphviz_string |
| 222 | +import GChartWrapper |
| 223 | +import html_module |
| 224 | +
|
| 225 | +a = TreeNode('a') |
| 226 | +b0 = TreeNode('b0') |
| 227 | +a.left = b0 |
| 228 | +b1 = TreeNode('b1') |
| 229 | +a.right = b1 |
| 230 | +c0 = TreeNode('c0') |
| 231 | +b0.left = c0 |
| 232 | +c1 = TreeNode('c1') |
| 233 | +b0.right = c1 |
| 234 | +c2 = TreeNode('c2') |
| 235 | +b1.left = c2 |
| 236 | +d1 = TreeNode('d1') |
| 237 | +c0.right = d1 |
| 238 | +
|
| 239 | +html_module.display_img(GChartWrapper.GraphViz(to_graphviz_string(a))) |
| 240 | +''' |
0 commit comments