def insert(self,word): currNode=self.startNode self.startNode.incTCounter() for char in word: prevNode=currNode if(not prevNode.getNext(char)): currNode=Node(prevNode.getString()+char) currNode.setTCounter(1) prevNode.setNext(char,currNode) else: currNode=prevNode.getNext(char) currNode.incTCounter() currNode.setIsWord(True) currNode.incWCounter()
def _insert(self, node, word, index): """ Internal method to insert a word in the tree. We use the same criteria as used in the _search method. """ if word is None or len(word) < 1: raise ValueError("invalid word") c = word[index] if node is None: node = Node(c) # se non esiste ancora la radice # restituiamo immediatamente il nuovo nodo # print word if c == node.getChar(): if (index +1) < len(word): #print "CHILD", node.getChild() node.setChild(self._insert(node.getChild(), word, index +1)) else: node.setIsWord(True) elif c < node.getChar(): node.setSmaller(self._insert(node.getSmaller(), word, index)) else: node.setLarger(self._insert(node.getLarger(), word, index)) return node