Question: class SuffixTrieNode: def _ _ init _ _ ( self , node _ id , orig _ str ) : self.orig _ str = orig

class SuffixTrieNode:
def __init__(self, node_id, orig_str): self.orig_str = orig_str # a reference to the entire string self.outgoing_edges ={} # dictionary from chars to edges self.suffix_link = None # suffix link : initially set to None self.id = node_id # Note: id ==0 is taken to be root for now. self.depth =0 # automatically set the depth when node's parent is set self.parent = None # parent pointer def is_root(self): return self.id ==0 def get_edge(self, char): if char in self.outgoing_edges: return self.outgoing_edges[char] else: return None def is_leaf(self): return False def add_suffix_link(self, node): self.suffix_link = node def add_outgoing_edge(self, new_edge): edge_init_char = new_edge.get_char_at(0) # ensure that an edge with the initial character does not exist assert edge_init_char not in self.outgoing_edges, f"Char {edge_init_char} already has an outgoing edge for node id:{self.id}" #ensure that the new_edge src matches self assert new_edge.src.id == self.id, f"Src node in outgoing edge id:{new_edge.src.id} does not match node id {self.id}" # add the new edge to the dictionary with the initial char as key self.outgoing_edges[edge_init_char]= new_edge # add a parent pointer from destination to the src of the new edge new_edge.dest.parent = new_edge.src # set the parent pointer of the new edges dest if not new_edge.is_leaf_edge(): # set the depth of the destination node for the edge new_edge.dest.depth = self.depth + new_edge.length() def find_edge_corresponding_to_child(self, child_node): # search among outgoing edges to see if there is one whose destination is the child node for (_, edge) in self.outgoing_edges.items(): if edge.dest.id == child_node.id: return edge return None # no such edge found class SuffixTrieLeaf: def __init__(self, node_id, orig_str, suffix_start_pos): self.orig_str = orig_str # the original string self.id = node_id # the id of this node assert 0<= suffix_start_pos < len(orig_str) self.suffix_start_pos = suffix_start_pos # the starting pos for the suffix self.parent = None # parent pointer initially set to None def is_leaf(self): return True class SuffixTrieEdge: def __init__(self, orig_str, src_node, dest_node, lo, hi): assert 0<= lo < len(orig_str) # lo must be a valid position in the original string # if destination node is a leaf then hi ==-1 else hi !=-1 if dest_node.is_leaf(): assert hi ==-1 else: assert lo <= hi <= len(orig_str) assert not src_node.is_leaf() # src node cannot be a leaf. # edge represents str[lo]...str[hi] inclusive if hi !=-1 # or set[lo]... str[end] self.orig_str = orig_str # set the orig_str field self.lo = lo # set lo/hi self.hi = hi self.src = src_node # set src/dest self.dest = dest_node def is_leaf_edge(self): return self.hi ==-1 def length(self): if self.hi ==-1: return -1 else: return self.hi - self.lo +1 def get_char_at(self, offs): assert self.hi ==-1 or offs + self.lo <= self.hi return self.orig_str[self.lo + offs] def get_sub_str(self, end=-1): if self.hi ==-1: return self.orig_str[self.lo:end] if (end !=-1) else self.orig_str[self.lo:] else: return self.orig_str[self.lo:self.hi+1] def reset_hi_and_dest(self, new_dest, new_hi): assert new_hi >= self.lo, f"Cannot replace hi value by {new_hi}" assert not new_dest.is_leaf(), "Cannot replace destination by a leaf node" self.hi = new_hi new_dest.parent = self.src new_dest.depth = self.src.depth + self.length() self.dest = new_dest class TrieAddress: def __init__(self, node, edge=None, offs=0): assert 0<= 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 >0 else: assert self.offs ==0 def traverse_next(self, c): """Function traverse_next: 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 node/root. # 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!