Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 64ac6f3

Browse files
authored
Merge pull request fnplus#571 from hussamEL-Hwary/patch-1
Fixing Trie.py
2 parents bbeb640 + 443dac2 commit 64ac6f3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

‎Data Structures/Trie/Trie.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class TrieNode(object):
32
def __init__(self):
43
self.children = [] #will be of size = 26
@@ -17,6 +16,9 @@ def insert(self, root, key):
1716
pCrawl = root
1817
for i in key:
1918
index = ord(i)-97
19+
if index > 25 or index < 0:
20+
print("Small alphabet charchters only allowed")
21+
return
2022
if(pCrawl.children[index] == None):
2123
# node has to be initialised
2224
pCrawl.children[index] = self.getNode()
@@ -28,6 +30,9 @@ def search(self, root, key):
2830
pCrawl = root
2931
for i in key:
3032
index = ord(i)-97
33+
# handling non alphabet characters
34+
if index > 25 or index < 0:
35+
return False
3136
if(pCrawl.children[index] == None):
3237
return False
3338
pCrawl = pCrawl.children[index]
@@ -45,4 +50,4 @@ def main():
4550
else:
4651
print("The given word doesnt exist")
4752
if __name__ == '__main__':
48-
main()
53+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /