Skip to content

Added Trie Logic #86

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

Merged
merged 1 commit into from
Oct 3, 2020
Merged
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
40 changes: 40 additions & 0 deletions data-structures/trie/trie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class trienode:
def __init__(self,character):
self.character = character
self.nextnodes = {}
self.isEnd = False
self.dataNode = None

class trie: # use case: username/password
def __init__(self):
self.start = trienode('')
def insert(self,id,value): # returns true on successful insertion (value == password)
temp = self.start
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an appropriate variable name for temp
Refer to - https://www.quora.com/Why-is-using-a-variable-name-temp-considered-bad-practice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the variable name.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable name is still temp. Please change it to something meaningful @GajeshS

for nextChar in id:
temp.nextnodes[nextChar] = temp.nextnodes.get(nextChar,trienode(nextChar))
temp = temp.nextnodes[nextChar]
if temp.isEnd == True:
print("ID already Exists!!")
return False
temp.isEnd =True
temp.dataNode = value
return True
def findNode(self,id): # returns false if node doesn't exist and true, node if it does
temp = self.start
for nextChar in id:
next = temp.nextnodes.get(nextChar,None)
if next is None:
return False,None
temp = next
if temp.isEnd == True:
return True,temp
else:
return False,None

if __name__ == '__main__':
t = trie()
t.insert('test1',"dummy1")
t.insert('test2',"dummy2")
print(t.findNode('test')) # (false,None)
a,b = t.findNode('test1')
print(a,b.dataNode) # true,dummy1