algorithm - Storing a Binary Tree in computer memory -
the table below represents tree stored in computer's memory. each node of tree contains 3 cells. first cell contains data stored; second cell contains pointer first cell's left child, , third cell contains pointer first cell's right child. value of 00 pointer represents nil pointer. separate variable points root of tree. root of tree cell starts @ 4f; is, variable holds root pointer has value 4f. fill in resulting tree. draw picture of resulting tree. (one way draw picture of binary tree using typewriter graphics using / indicate left pointer , \ indicate right pointer. can see example of in next problem.)
address contents 40 a1 41 00 42 00 43 b2 44 00 45 00 46 d4 47 49 48 00 49 c3 4a 00 4b 00 4c e5 4d 43 4e 40 4f f6 50 46 51 4c i have idea how solve problem. have idea solve problem
here's solution:

it generated python code, using python tree.py | dot -t png a.png
data = """ 40 a1 41 00 42 00 43 b2 44 00 45 00 46 d4 47 49 48 00 49 c3 4a 00 4b 00 4c e5 4d 43 4e 40 4f f6 50 46 51 4c""".split("\n") data = [line.split() line in filter(none, data)] data = dict(map((lambda x: int(x, 16)), line) line in data) def print_tree(data, p): print ' n%x [label="%d"];' % (p, data[p]) c in data[p+1], data[p+2]: if not c: continue print_tree(data, c) print ' n%x -> n%x;' % (p, c) print 'digraph tree {' print_tree(data, 0x4f) print '}'
Comments
Post a Comment