Skip to content

Instantly share code, notes, and snippets.

@vineetrok
Created November 24, 2011 18:06
Show Gist options
  • Select an option

  • Save vineetrok/1391946 to your computer and use it in GitHub Desktop.

Select an option

Save vineetrok/1391946 to your computer and use it in GitHub Desktop.
Illustration of Trie Data Structure in Python
class Node:
def __init__(self):
self.next = {}
self.word_marker = False
def add(self, string):
if len(string) == 0:
self.word_marker = True
return
key = string[0]
string = string[1:]
if self.next.has_key(key):
self.next[key].add(string)
else:
node = Node()
self.next[key] = node
node.add(string)
def partial(self, sf=None):
if self.next.keys() == []:
print "Match:",sf
return
if self.word_marker == True:
print "Match:",sf
for key in self.next.keys():
self.next[key].partial(sf+key)
def search(self, string, sf=""):
if len(string) > 0:
key = string[0]
string = string[1:]
if self.next.has_key(key):
sf = sf + key
self.next[key].search(string,sf)
else:
print "No match"
else:
if self.word_marker == True:
print "Match:",sf
for key in self.next.keys():
self.next[key].partial(sf+key)
def exact_match(self, string, sf=''):
if len(string) > 0:
key = string[0]
string = string[1:]
if self.next.has_key(key):
sf = sf + key
self.next[key].exact_match(string,sf)
else:
print "No match"
else:
if self.word_marker == True:
print "Match:",sf
elif self.word_marker==False:
print 'No Match.'
def all_trie(self):
a=sorted(self.next.keys())
if(len(a)==0):
print 'Empty Trie.'
for b in a:
self.next[b].partial(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment