Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trie #39

Merged
merged 2 commits into from
Aug 19, 2017
Merged

Trie #39

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions pygorithm/data_structures/trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
class Node:
def __init__(self, v, p=None, w=False):
self.word = w #If the node represents the end of a word or not
self.parent = p
self.value = v
self.children = {}


class Trie:
def __init__(self):
self.root = Node('') #The root of the trie is always empty

def Insert(self, word):
"""
Insert word in the trie. Starting from the root, move down the trie
following the path of characters in the word. If the nodes for the word
characters end, add them. When the last char is added, mark it as a
word-ending node.
"""
l = len(word)
curr = self.root
for i, c in enumerate(word):
last = False
if(i == l-1):
#The last char of the word
last = True

if(c not in curr.children):
curr.children[c] = Node(c, curr, last)
elif(last):
#c already exists, but as it is the last char of word,
#it should now be flagged as a word in the trie.
curr.children[c].word = True

curr = curr.children[c]

def Search(self, word):
"""
Searches for given word in trie. We want to find the last node for the
word. If we can't, then it means the word is not in the trie.
"""
if self.FindFinalNode(word):
return True
else:
return False

def FindWords(self, prefix):
"""
Find all words with the given prefix
"""
v = self.FindFinalNode(prefix)
wList = self.BuildWordList(v, prefix)
if(v and v.word):
#v exists and the prefix is itself a word; add it to the list.
wList.append(prefix)

return wList

def FindFinalNode(self, word):
"""
Returns the last node in given word. The process goes like this:
Start from the root. For every char in word, go down one level.
If we can't go down a level, then the word doesn't exist.
If we do, and the current char is the last char of the word and
the node we are currently at is a word, then we have found the given
word.
"""
curr = self.root
l = len(word)

for i, c in enumerate(word):
if(c not in curr.children):
#There is no prefix of cWord + c
return None

if(i == l-1):
#Last char of word
return curr.children[c]

curr = curr.children[c]

return None

def BuildWordList(self, v, cWord):
"""
Recursively builds the list of words.
* v: Node to check
* cWord : The word built up to v
"""
if(not v):
return None

wList = []
for i, k in v.children.items():
tempWord = cWord + i

if(k.word):
#If the iterated prefix is a word, add it to the list
wList.append(tempWord)

#The list of words under tWord
wList.extend(self.BuildWordList(k, tempWord))

return wList
20 changes: 19 additions & 1 deletion tests/test_data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
linked_list,
tree,
graph,
heap)
heap,
trie)


class TestStack(unittest.TestCase):
Expand Down Expand Up @@ -237,5 +238,22 @@ def test_heap(self):
self.assertEqual(myHeap.queue, expectedResult)


class TestTrie(unittest.TestCase):
def test_stack(self):
myTrie = trie.Trie()
myTrie.Insert('the')
myTrie.Insert('turtle')
myTrie.Insert('thesaurus')
myTrie.Insert('chocolate')
myTrie.Insert('flying')

self.assertEqual(myTrie.FindWords('th'), ['the', 'thesaurus'])
self.assertEqual(myTrie.FindWords('e'), None)

self.assertEqual(myTrie.Search('chocolate'), True)
self.assertEqual(myTrie.Search('flying'), True)
self.assertEqual(myTrie.Search('walking'), False)


if __name__ == '__main__':
unittest.main()