import re
class Node:
def __init__(self, word):
self.word = word
self.count = 1
self.left = None
self.right = None
class WordBST:
def __init__(self):
self.root = None
def insert(self, word):
if self.root is None:
self.root = Node(word)
return
current = self.root
while True:
if word == current.word:
current.count += 1
return
elif word < current.word:
if current.left is None:
current.left = Node(word)
return
current = current.left
else:
if current.right is None:
current.right = Node(word)
return
current = current.right
def search(self, word):
current = self.root
while current:
if word == current.word:
return current.count
elif word < current.word:
current = current.left
else:
current = current.right
return 0
def _inorder(self, node, result):
if node:
self._inorder(node.left, result)
result.append((node.word, node.count))
self._inorder(node.right, result)
def get_sorted_words(self):
result = []
self._inorder(self.root, result)
return result
text = "это пример текста это просто пример текста"
words = re.findall(r'\w+', text.lower())
tree = WordBST()
for w in words:
tree.insert(w)
print(tree.search("пример"))
print(tree.get_sorted_words())