Question: Complete the implementation of the function add _ all _ suffix _ links that given a suffix trie without suffix links, walks the trie from
Complete the implementation of the function addallsuffixlinks that given a suffix trie without suffix links, walks the trie from root down to leaves and computes suffix nodes for a child node given that of a parent node. In particular, we would like you to complete the getsuffixlinkfordestnode function below as speficied.
def addallsuffixlinksroot debugTrue:
root.addsuffixlinkroot # root must be suffix linked to itself.
worklist # a list of edges wherein parent has a suffix link but child does not
for edge in root.outgoingedges.items:
# iterate through all outgoing edges of the root
if not edge.isleafedge:
worklist.appendedge # add the edge from root to an internal nonleaf node to the worklist.
## now iterate through the worklist
while lenworklist:
edge worklist.pop
assert not edge.isleafedge
p edge.src
n edge.dest
# this is an edge from parent p to node n
sn getsuffixlinkfordestnodeedge # use the edge data to get the suffix node
naddsuffixlinksn
if debug:
printfAdding suffix link nnid nsnid
for nextedge in noutgoingedges.items:
if not nextedge.isleafedge:
worklist.appendnextedge
return
def getsuffixlinkfordestnodeedge:
p edge.src
n edge.dest
assert psuffixlink None, fparent pid has no suffix link but asking for the suffix link of a child"
assert not edge.isleafedge
origstr edge.origstr
lo edge.lo
hi edge.hi
assert lo hi and hi lenorigstr
## TODO: implement the algorithm to find the suffix link of n
## given that of p and the data for the edge from p n
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
