Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trie = [['a', ['a', ['*']], ['x', ['e', ['*']]]], ['b', ['i', ['k', ['e']]]], ['c']]
- def checkchar(tree, word, currindex):
- for node in tree:
- if currindex == len(word):
- if word[currindex -1 ] == '*':
- return True
- else:
- return False
- if node[0] == word[currindex]:
- currindex +=1
- if checkchar(node[1:], word, currindex):
- return True
- else:
- return False
- def addchar(tree, word, currindex):
- # for node in tree:
- # if node[0] == word[currindex]:
- # currindex +=1
- # addchar(node, word, currindex)
- # return True
- # node.append(list(word[currindex]))
- # addchar(node, word, currindex)
- for node in tree:
- if node[0] == word[currindex]:
- currindex +=1
- if addchar(node[1:], word, currindex):
- return True
- else:
- node.append(list(word[currindex]))
- addchar(node[1:], word, currindex+1)
- return True
- def checkcharpre(tree, word, intA):
- return checkchar(tree, word + '*', intA)
- def addword(word):
- if checkcharpre(trie, word, 0):
- print(trie)
- else:
- addchar(trie, word, 0)
- #Word doesnt exist now we need to add it
- print(checkcharpre(trie, 'axe', 0))
- addword("ban")
- print(trie)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement