Advertisement
Guest User

xddz

a guest
Mar 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. trie = [['a', ['a', ['*']], ['x', ['e', ['*']]]], ['b', ['i', ['k', ['e']]]], ['c']]
  2.  
  3. def checkchar(tree, word, currindex):
  4. for node in tree:
  5. if currindex == len(word):
  6. if word[currindex -1 ] == '*':
  7. return True
  8. else:
  9. return False
  10. if node[0] == word[currindex]:
  11. currindex +=1
  12. if checkchar(node[1:], word, currindex):
  13. return True
  14. else:
  15. return False
  16.  
  17. def addchar(tree, word, currindex):
  18. # for node in tree:
  19. # if node[0] == word[currindex]:
  20. # currindex +=1
  21. # addchar(node, word, currindex)
  22. # return True
  23. # node.append(list(word[currindex]))
  24. # addchar(node, word, currindex)
  25. for node in tree:
  26. if node[0] == word[currindex]:
  27. currindex +=1
  28. if addchar(node[1:], word, currindex):
  29. return True
  30. else:
  31. node.append(list(word[currindex]))
  32. addchar(node[1:], word, currindex+1)
  33. return True
  34.  
  35.  
  36.  
  37.  
  38.  
  39. def checkcharpre(tree, word, intA):
  40. return checkchar(tree, word + '*', intA)
  41.  
  42. def addword(word):
  43. if checkcharpre(trie, word, 0):
  44. print(trie)
  45. else:
  46. addchar(trie, word, 0)
  47. #Word doesnt exist now we need to add it
  48.  
  49.  
  50. print(checkcharpre(trie, 'axe', 0))
  51. addword("ban")
  52. print(trie)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement