-
Notifications
You must be signed in to change notification settings - Fork 27
/
bintree_module.py
442 lines (361 loc) · 11.3 KB
/
bintree_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# TODO: how do you set the BACKGROUND COLOR of a GraphViz node ... fill=?
# example code snippet to visualize, which uses '#break' as a breakpoint
# feel free to clean up however you like
'''
from bintree_module import TNode
import html_module
r = TNode('a',
left=TNode('b0',
left=TNode('c0',
right=TNode('d1')),
right=TNode('c1',
left=TNode('d3'),
right=TNode('d4'))),
right=TNode('b1',
left=TNode('c2')))
def highlight_and_display(root):
def f(node):
node.highlight()
html_module.display_img(root.to_graphviz_img()) #break
node.reset_style()
return f
def preorder(t, visitfn):
if not t:
return
visitfn(t)
preorder(t.left, visitfn)
preorder(t.right, visitfn)
preorder(r, highlight_and_display(r))
'''
from collections import defaultdict
import GChartWrapper
import html_module
import sys
is_python3 = (sys.version_info[0] == 3)
if is_python3:
import io as cStringIO
else:
import cStringIO
ID = 0
# somewhat inspired by bst.py from MIT 6.006 OCW
# http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/search.htm
class TNode:
def __init__(self, dat, left=None, right=None):
self.data = dat
self.parent = None
self.left = left
self.right = right
if self.left:
self.left.parent = self
if self.right:
self.right.parent = self
self.__penwidth = 1 # thickness of node border
# HTML-like RGB hex values - e.g., "#bb0000"
self.__color = None # border color
self.__fill = None # internal node color
# assign unique IDs in node creation order
global ID
self.id = 'n' + str(ID)
ID += 1
def disconnect(self):
self.left = None
self.right = None
self.parent = None
def set_border_color(self, col):
self.__color = col
def set_fill(self, col):
self.__fill = col
def set_width(self, w):
assert w > 0
self.__penwidth = w
def highlight(self):
self.__color = 'red'
self.__penwidth = 2
def reset_style(self):
self.__color = None
self.__fill = None
self.__penwidth = 1
def is_leaf(self):
return not (self.left or self.right)
def graphviz_str(self):
ret = '%s[label="%s"' % (self.id, str(self.data)) # convert to str() for display
if self.__penwidth > 1:
ret += ',penwidth=%d' % self.__penwidth
if self.__color:
ret += ',color="%s"' % self.__color
if self.__fill:
ret += ',fill="%s"' % self.__fill
ret += ']'
return ret
def __str__(self):
return 'TNode(%s)' % repr(self.data)
# render a binary tree of TNode objects starting at self in a pretty
# GraphViz format using the balanced tree hack from
# http://www.graphviz.org/content/FaqBalanceTree
def graphviz_render(self, ios, compress=False):
separator = '\n'
if compress:
separator=','
ios.write('digraph G{')
if not compress:
ios.write('\n')
queue = [] # each element is (node, level #)
# Key: level number
# Value: sorted list of node IDs at that level (including phantom nodes)
nodes_by_level = defaultdict(list)
def render_phantom(parent_id, suffix):
phantom_id = parent_id + '_phantom_' + suffix
ios.write('%s [label="",width=.1,style=invis]%s' % (phantom_id, separator))
ios.write('%s->%s [style=invis]%s' % (parent_id, phantom_id, separator))
return phantom_id
def bfs_visit():
# base case
if not queue:
return
n, level = queue.pop(0)
ios.write(n.graphviz_str() + separator) # current node
if n.left or n.right:
if n.left:
ios.write('%s->%s%s' % (n.id, n.left.id, separator))
queue.append((n.left, level+1))
nodes_by_level[level+1].append(n.left.id)
else:
# insert phantom to make tree look good
ph_id = render_phantom(n.id, 'L')
nodes_by_level[level+1].append(ph_id)
# always insert invisible middle phantom
ph_id = render_phantom(n.id, 'M')
nodes_by_level[level+1].append(ph_id)
if n.right:
ios.write('%s->%s%s' % (n.id, n.right.id, separator))
queue.append((n.right, level+1))
nodes_by_level[level+1].append(n.right.id)
else:
# insert phantom to make tree look good
ph_id = render_phantom(n.id, 'R')
nodes_by_level[level+1].append(ph_id)
bfs_visit() # recurse!
queue.append((self, 1))
bfs_visit()
if not compress:
ios.write('\n')
# make sure all nodes at the same level are vertically aligned
for level in nodes_by_level:
node_ids = nodes_by_level[level]
if len(node_ids) > 1:
ios.write(('{rank=same %s [style=invis]}' % '->'.join(node_ids)) + separator)
ios.write('}') # cap it off
def to_graphviz_string(self):
s = cStringIO.StringIO()
self.graphviz_render(s, True)
return s.getvalue()
def to_graphviz_img(self):
return GChartWrapper.GraphViz(self.to_graphviz_string())
# from MIT 6.006 OCW
# http://ocw.mit.edu/ans7870/6/6.006/s08/lecturenotes/search.htm
class BST(object):
"""
Simple binary search tree implementation.
This BST supports insert, find, and delete-min operations.
Each tree contains some (possibly 0) BSTnode objects, representing nodes,
and a pointer to the root.
"""
def __init__(self):
self.root = None
def to_graphviz_img(self):
if self.root:
return GChartWrapper.GraphViz(self.root.to_graphviz_string())
else:
return ''
def insert(self, t):
"""Insert data t into this BST, modifying it in-place."""
new = TNode(t)
if self.root is None:
self.root = new
else:
node = self.root
while True:
if t < node.data:
# Go left
if node.left is None:
node.left = new
new.parent = node
break
node = node.left
else:
# Go right
if node.right is None:
node.right = new
new.parent = node
break
node = node.right
return new
def find(self, t):
"""Return the node for data t if is in the tree, or None otherwise."""
node = self.root
while node is not None:
if t == node.data:
return node
elif t < node.data:
node = node.left
else:
node = node.right
return None
def delete_min(self):
"""Delete the minimum data (and return the old node containing it)."""
if self.root is None:
return None, None
else:
# Walk to leftmost node.
node = self.root
while node.left is not None:
node = node.left
# Remove that node and promote its right subtree.
if node.parent is not None:
node.parent.left = node.right
else: # The root was smallest.
self.root = node.right
if node.right is not None:
node.right.parent = node.parent
parent = node.parent
node.disconnect()
return node, parent
def __str__(self):
if self.root is None:
return 'empty tree'
else:
return 'tree with root: %s' % str(self.root)
if __name__ == "__main__":
# simple test tree
r = TNode('a',
left=TNode('b0',
left=TNode('c0',
right=TNode('d1')),
right=TNode('c1',
left=TNode('d3'),
right=TNode('d4'))),
right=TNode('b1',
left=TNode('c2',
left=TNode('d2'))))
f = open('test.dot', 'w')
r.graphviz_render(f)
f.close()
'''
t = BST()
import random
nums = range(10)
random.shuffle(nums)
for i in nums:
t.insert(i)
f = open('test.dot', 'w')
t.root.graphviz_render(f)
f.close()
'''
'''
/* balanced tree hack from http://www.graphviz.org/content/FaqBalanceTree */
/*
digraph G {
a -> b0
xb [label="",width=.1,style=invis]
a -> xb [style=invis]
a -> b1
{rank=same b0 -> xb -> b1 [style=invis]}
b0 -> c0
xc [label="",width=.1,style=invis]
b0 -> xc [style=invis]
b0 -> c1
{rank=same c0 -> xc -> c1 [style=invis]}
}
*/
'''
'''
from bintree_module import TNode
import html_module
r = TNode('a',
left=TNode('b0',
left=TNode('c0',
right=TNode('d1')),
right=TNode('c1',
left=TNode('d3'),
right=TNode('d4'))),
right=TNode('b1',
left=TNode('c2')))
def highlight_and_display(root):
def f(node):
node.highlight()
html_module.display_img(root.to_graphviz_img()) #break
node.reset_style()
return f
def preorder(t, visitfn):
if not t:
return
visitfn(t)
preorder(t.left, visitfn)
preorder(t.right, visitfn)
preorder(r, highlight_and_display(r))
'''
'''
from bintree_module import BST
import html_module
import random
t = BST()
html_module.display_img(t.to_graphviz_img())
nums = range(10)
random.shuffle(nums)
for i in nums:
t.insert(i)
html_module.display_img(t.to_graphviz_img())
'''
# insertion into a BST with each step animated
#
# TODO: think of a more elegant way to separate out algorithm from HTML
# rendering code
'''
import html_module, GChartWrapper, random
from bintree_module import TNode
class BST:
def __init__(self):
self.root = None
def to_graphviz_img(self):
if self.root:
return GChartWrapper.GraphViz(self.root.to_graphviz_string())
else:
return ''
def insert(self, t):
"""Insert data t into this BST, modifying it in-place."""
new = TNode(t)
if self.root is None:
self.root = new
else:
node = self.root
while True:
node.highlight()
html_module.display_img(self.to_graphviz_img()) #break
node.reset_style()
if t < node.data:
# Go left
if node.left is None:
node.left = new
new.parent = node
new.highlight()
html_module.display_img(self.to_graphviz_img()) #break
new.reset_style()
break
node = node.left
else:
# Go right
if node.right is None:
node.right = new
new.parent = node
new.highlight()
html_module.display_img(self.to_graphviz_img()) #break
new.reset_style()
break
node = node.right
return new
t = BST()
nums = range(10)
random.shuffle(nums)
for i in nums:
t.insert(i)
'''