-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearching
executable file
·91 lines (72 loc) · 2.58 KB
/
searching
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
from enum import IntEnum
from typing import List
def LinearCount(wordList: List[str], searchedWord: str) -> int:
word_occurrence = int(0)
for word in wordList:
if word.casefold() == searchedWord.casefold():
word_occurrence += 1
print(f"Occurence of word '{searchedWord}': {word_occurrence}")
return word_occurrence
##############################################################################
class AddOnSide(IntEnum):
UNKNOWN = -1
LEFT = 0
RIGHT = 1
class Node:
def __init__(self, word):
self.occurrence = 1
self.leftTree = None
self.rightTree = None
self.word = word
class BTree:
def __init__(self):
self.root = None
def isEmpty(self) -> bool:
return self.root is None
def getOccurrenceOfWord(self, word) -> int:
if self.isEmpty():
return 0
currentNode = self.root
while currentNode is not None:
if word.casefold() == currentNode.word:
return currentNode.occurrence
elif word.casefold() < currentNode.word:
currentNode = currentNode.leftTree
else:
currentNode = currentNode.rightTree
return 0
def insertInTree(self, word) -> None:
sideToAdd = AddOnSide.UNKNOWN
if self.isEmpty():
self.root = Node(word.casefold())
return
parentNode = None
currentNode = self.root
while currentNode is not None:
if word.casefold() < currentNode.word:
parentNode = currentNode
currentNode = currentNode.leftTree
sideToAdd = AddOnSide.LEFT
elif word.casefold() > currentNode.word:
parentNode = currentNode
currentNode = currentNode.rightTree
sideToAdd = AddOnSide.RIGHT
elif word.casefold() == currentNode.word:
currentNode.occurrence += 1
return
if sideToAdd == AddOnSide.LEFT:
parentNode.leftTree = Node(word.casefold())
elif sideToAdd == AddOnSide.RIGHT:
parentNode.rightTree = Node(word.casefold())
def BinaryCount(wordList: List[str], searchedWord: str) -> int:
bTree = BTree()
for word in wordList:
bTree.insertInTree(word)
occurrenceOfWord = bTree.getOccurrenceOfWord(searchedWord)
return occurrenceOfWord
def main() -> int:
word_list = ['aled', 'aled', 'aled', 'aledd', 'aleddddd']
print(BinaryCount(word_list, 'aled'))
if __name__ == "__main__":
exit(main())