-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrefixTree.py
184 lines (136 loc) · 5.12 KB
/
PrefixTree.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
'''
Copyright Reese Wells, 2017
'''
import BinarySearchTree
class PrefixTree:
'''
A prefix tree implementation
'''
def __init__(self):
'''
creates an empty prefix tree
'''
self._children = None
self._data = False
self._END = 256
def _initChildren(self):
'''
initiates 256 children to false prefix trees, with one extra
boolean to signify the end of a word
rtype : boolean
returns true if successful in initializing all the children
'''
t1 = []
for i in range(0, 256):
t1.append(PrefixTree())
t1.append(False)
self._children = t1
def add(self, letter):
'''
Adds a new letter to the tree
rtype : boolean
returns True if data was added
returns False if data was already there
'''
if self._children is None:
self._initChildren()
if not self._children[ord(letter)]._data:
self._children[ord(letter)]._data = True
def contains(self, letter):
'''
Checks if the given node has a child that matches data
rtype : boolean
returns True if one of the children's data matches
returns False if none of the children's data matches
'''
if self._children[ord(letter)]._data:
return True
return False
def addWord(self, word):
'''
adds a word to the tree by recursively calling add()
rtype : boolean
returns True if the word was added
returns False if the tree already contained the word
NOTE: returns true if given an empty string
'''
if len(word) == 0:
if self._children is None:
self._initChildren()
self._children[self._END] = True
else:
self.add(word[:1])
self._children[ord(word[:1])].addWord(word[1:])
def containsWord(self, word):
'''
Checks if the tree contains the given word by calling contains()
recursively and checking each letter
rtype : boolean
returns True if the tree contains the word
returns False if the tree does not contain the word
'''
if len(word) == 0:
return self._children[self._END]
elif self._children is not None and self._children[ord(word[:1])]._data:
return self._children[ord(word[:1])].containsWord(word[1:])
else:
return False
def test():
# test making the tree
p = PrefixTree()
print ("adding 'a': " + str(p.add('a')))
print ("adding 'b': " + str(p.add('b')))
print ("adding 'a': " + str(p.add('a')))
print ("contains 'a': " + str(p.contains('a')))
# test containsWord
print ("p.containsWord('aa'): " + str(p.containsWord('aa')))
print ("p.containsWord('ab'): " + str(p.containsWord('ab')))
print ("p.containsWord('abcd'): " + str(p.containsWord('abcd')))
print ("p.containsWord('d'): " + str(p.containsWord('d')))
# test addWord
print ("p = PrefixTree(): ")
p = PrefixTree()
print ("adding 'hello': " + str(p.addWord('hello')))
print ("adding 'hello': " + str(p.addWord('hello')))
print ("adding 'world': " + str(p.addWord('world')))
print ("adding 'python': " + str(p.addWord('python')))
print ("adding 'java': " + str(p.addWord('java')))
print ("adding 'word': " + str(p.addWord('word')))
# test containsWord
print ("p.containsWord('hello'): " + str(p.containsWord('hello')))
print ("p.containsWord('hel'): " + str(p.containsWord('hel')))
print ("p.containsWord('world'): " + str(p.containsWord('world')))
print ("p.containsWord('w'): " + str(p.containsWord('w')))
print ("p.containsWord('python'): " + str(p.containsWord('python')))
print ("p.containsWord('pythone'): " + str(p.containsWord('pythone')))
print ("p.containsWord('java'): " + str(p.containsWord('java')))
print ("p.containsWord(''): " + str(p.containsWord('')))
print ("p.containsWord('word'): " + str(p.containsWord('word')))
return p
def testDictionary():
'''
Adds all the words in the dictionary from the file "words.txt"
rtype : PrefixTree()
'''
dictionary = open("words.txt", 'r')
dictionaryTree = PrefixTree()
# first lets get a line count to know how many words we've added at any give time
lineCount = 0
for line in dictionary:
lineCount = lineCount + 1
# now let's add the words, spitting out percentages on occasion
currentLine = 0
currentProgress = 0
dictionary = open("words.txt", 'r')
for line in dictionary:
# get our current progress through the words
percentDone = int((currentLine / lineCount) * 100)
if percentDone > currentProgress:
currentProgress = percentDone
print("finished adding " + str(currentProgress) + "% of words in the dictionary")
# add the next word
dictionaryTree.addWord(line.strip())
#increment currentLine
currentLine = currentLine + 1
return dictionaryTree
# dictionary = testDictionary()