Question: class SuffixTrieNode: def _ _ init _ _ ( self , node _ id , orig _ str ) : self.orig _ str = orig
class SuffixTrieNode:
def initself nodeid origstr: self.origstr origstr # a reference to the entire string self.outgoingedges # dictionary from chars to edges self.suffixlink None # suffix link : initially set to None self.id nodeid # Note: id is taken to be root for now. self.depth # automatically set the depth when node's parent is set self.parent None # parent pointer def isrootself: return self.id def getedgeself char: if char in self.outgoingedges: return self.outgoingedgeschar else: return None def isleafself: return False def addsuffixlinkself node: self.suffixlink node def addoutgoingedgeself newedge: edgeinitchar newedge.getcharat # ensure that an edge with the initial character does not exist assert edgeinitchar not in self.outgoingedges, fChar edgeinitchar already has an outgoing edge for node id:selfid #ensure that the newedge src matches self assert newedge.srcid self.id fSrc node in outgoing edge id:newedge.srcid does not match node id selfid # add the new edge to the dictionary with the initial char as key self.outgoingedgesedgeinitchar newedge # add a parent pointer from destination to the src of the new edge newedge.dest.parent newedge.src # set the parent pointer of the new edges dest if not newedge.isleafedge: # set the depth of the destination node for the edge newedge.dest.depth self.depth newedge.length def findedgecorrespondingtochildself childnode: # search among outgoing edges to see if there is one whose destination is the child node for edge in self.outgoingedges.items: if edge.dest.id childnode.id: return edge return None # no such edge found class SuffixTrieLeaf: def initself nodeid origstr suffixstartpos: self.origstr origstr # the original string self.id nodeid # the id of this node assert suffixstartpos lenorigstr self.suffixstartpos suffixstartpos # the starting pos for the suffix self.parent None # parent pointer initially set to None def isleafself: return True class SuffixTrieEdge: def initself origstr srcnode, destnode, lo hi: assert lo lenorigstr # lo must be a valid position in the original string # if destination node is a leaf then hi else hi if destnode.isleaf: assert hi else: assert lo hi lenorigstr assert not srcnode.isleaf # src node cannot be a leaf. # edge represents strlostrhi inclusive if hi # or setlo strend self.origstr origstr # set the origstr field self.lo lo # set lohi self.hi hi self.src srcnode # set srcdest self.dest destnode def isleafedgeself: return self.hi def lengthself: if self.hi : return else: return self.hi self.lo def getcharatself offs: assert self.hi or offs self.lo self.hi return self.origstrselflo offs def getsubstrself end: if self.hi : return self.origstrselflo:end if end else self.origstrselflo: else: return self.origstrselflo:self.hi def resethianddestself newdest, newhi: assert newhi self.lo fCannot replace hi value by newhi assert not newdest.isleaf "Cannot replace destination by a leaf node" self.hi newhi newdest.parent self.src newdest.depth self.srcdepth self.length self.dest newdest class TrieAddress: def initself node, edgeNone, offs: assert offs self.node node # set the node self.edge edge # set the edge self.offs offs # set the offset if self.edge None: assert self.offs else: assert self.offs def traversenextself c: Function traversenext: find the next address if one exists that is obtained when we encounter character c Return the new address if one exists or else return None.""" if self.edge None: # Is the address pointing to an internal node? # Yes: address is just a pointer to an internal noderoot # check if the node has an outgo
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
