I was tring to build a trie tree base on a word list, below is the node class defintion, self.next_trees for children of the node.
class Node(object):
def __init__(self, charactor, tree_dict={}):
self.charactor = charactor
self.next_trees = tree_dict
self.is_end = False
and build the tree use this function
def build_tree(words):
root = Node(None)
for word in words:
trace = root.next_trees
for i,c in enumerate(word):
if c not in trace:
trace[c] = Node(charactor=c)
pdb.set_trace()
if i == len(word)-1:
trace[c].is_end = True
else:
trace = trace[c].next_trees
else:
trace = trace[c].next_trees
return root
each time when the codes run to the break point, the object "trace" refers to is exactly the same object "trace[c].next_trees" refer to,rather than a empty dict"{}" enter image description here
and I copy similiar codes to a new .py file and run, it won't happen again.why it happens here?(python version 2.7.12)