het_fadia

Untitled

Jul 22nd, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. from collections import deque
  2. class Trie:
  3. def __init__(self, *words):
  4. self.root = dict()
  5. for word in words:
  6. self.add(word)
  7. self.searching_method=[self.dfs,self.dfs_iterative,self.bfs]
  8. def add(self, word):
  9. current_dict = self.root
  10. for letter in word:
  11. current_dict = current_dict.setdefault(letter, dict())
  12. current_dict["_end_"] = True
  13.  
  14. def __contains__(self, word):
  15. current_dict = self.root
  16. for letter in word:
  17. if letter not in current_dict:
  18. return False
  19. current_dict = current_dict[letter]
  20. return "_end_" in current_dict
  21.  
  22. def __delitem__(self, word):
  23. current_dict = self.root
  24. nodes = [current_dict]
  25. for letter in word:
  26. current_dict = current_dict[letter]
  27. nodes.append(current_dict)
  28. del current_dict["_end_"]
  29. def prefix(self,word,search_index=2):
  30. """print all the possible words starting with preifx word(parameter of prefix)"""
  31. current_dict = self.root
  32. for letter in word:
  33. if letter not in current_dict:
  34. return ""
  35. current_dict = current_dict[letter]
  36. self.searching_method[search_index](current_dict,word)
  37. def dfs(self,current_dict,prefix_word):
  38. """dfs to print words through prefix_word"""
  39. for j in current_dict:
  40. if j=='_end_':
  41. print(prefix_word)
  42. else:
  43. self.dfs(current_dict[j],prefix_word+j)
  44. def dfs_iterative(self,current_dict,prefix_word):
  45. """dfs to print words through prefix_word"""
  46. stack=[]
  47. words_queue = [prefix_word]
  48. stack.append(current_dict)
  49. while stack:
  50. current_dict=stack.pop()
  51. prefix_word=words_queue.pop()
  52. for j in current_dict:
  53. if j=='_end_':
  54. print(prefix_word)
  55. else:
  56. stack.append(current_dict[j])
  57. words_queue.append(prefix_word+j)
  58.  
  59.  
  60. def bfs(self,current_dict,prefix_word):
  61. """bfs to print words through prefix_word"""
  62. q=deque()
  63. words_queue = deque()
  64. q.append(current_dict)
  65. words_queue.append(prefix_word)
  66. while len(q):
  67. current_dict=q.popleft()
  68. prefix_word=words_queue.popleft()
  69. for j in current_dict:
  70. if j=='_end_':
  71. print(prefix_word)
  72. else:
  73. q.append(current_dict[j])
  74. words_queue.append(prefix_word+j)
  75. def count_prefix(self,word):
  76. current_dict = self.root
  77. for letter in word:
  78. if letter not in current_dict:
  79. return 0
  80. current_dict = current_dict[letter]
  81. return self.bfs_count(current_dict, word)
  82. def bfs_count(self,current_dict,prefix_word):
  83. """bfs to count all words starting through prefix_word"""
  84. q=deque()
  85. q.append(current_dict)
  86. counter=0
  87. while len(q):
  88. current_dict=q.popleft()
  89. for j in current_dict:
  90. if j=='_end_':
  91. counter+=1
  92. else:
  93. q.append(current_dict[j])
  94. return counter
  95. a=Trie()
  96. a.add('1')
  97. a.add('12')
  98. a.add('13')
  99. a.add('113')
  100. a.add('1143')
  101. a.add('1243')
  102. a.add('1245')
  103. print("prefix searching through dfs")
  104. a.prefix('1',0)
  105. print("prefix searching through dfs_iterative")
  106. a.prefix('1',1)
  107. print("prefix searching through bfs")
  108. a.prefix('1',2)
  109. print('count words starting through 12')
  110. print(a.count_prefix('12'))
Add Comment
Please, Sign In to add comment