-
Notifications
You must be signed in to change notification settings - Fork 368
/
trie.py
132 lines (90 loc) · 3.2 KB
/
trie.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
# Path: C/Trees/trie.c
# C program to implement trie.
# A prefix trie is an ordered tree data structure used in the representation of a set of strings over a finite alphabet set,
# which allows efficient storage of words with common prefixes.
class TrieNode:
# Trie node
def __init__(self):
# 26 lowercase alphabets
self.children = [None] * 26
self.isEndOfWord = False
# Creates a new Trie node
def createNode():
return TrieNode()
# Inserts a word into the Trie
def insert(root, word):
currentNode = root
for char in word:
ind = ord(char) - ord('a')
if currentNode.children[ind] is None:
currentNode.children[ind] = createNode()
currentNode = currentNode.children[ind]
currentNode.isEndOfWord = True
# Searches for a word in the Trie
def search(root, word):
currentNode = root
for char in word:
ind = ord(char) - ord('a')
if currentNode.children[ind] is None:
return False
currentNode = currentNode.children[ind]
return currentNode is not None and currentNode.isEndOfWord
# Checks if a Trie node has any children
def hasChildren(node):
for child in node.children:
if child is not None:
return True
return False
# Deletes a word from the Trie
def deleteWord(root, word, level):
if root is None:
return False
if level == len(word):
if root.isEndOfWord:
root.isEndOfWord = False
if not hasChildren(root):
root = None
return True
return False
ind = ord(word[level]) - ord('a')
deleted = deleteWord(root.children[ind], word, level + 1)
if deleted and not hasChildren(root.children[ind]):
root.children[ind] = None
return deleted
# Deletes the entire Trie
def deleteTrie(root):
if root is None:
return
for child in root.children:
if child is not None:
deleteTrie(child)
del root
# Program starts here
if __name__ == "__main__":
root = createNode()
# Inserting words into the Trie
insert(root, "apple")
insert(root, "banana")
insert(root, "car")
insert(root, "cat")
insert(root, "cart")
insert(root, "candid")
print("Words: apple, banana, car, cat, cart, candid are Inserted.\n")
print("Search results:")
# Searching for words in the Trie
print("apple:", "Found" if search(root, "apple") else "Not found")
print("banana:", "Found" if search(root, "banana") else "Not found")
print("care:", "Found" if search(root, "care") else "Not found")
print("cat:", "Found" if search(root, "cat") else "Not found")
print("cart:", "Found" if search(root, "cart") else "Not found")
print("dog:", "Found" if search(root, "dog") else "Not found")
# Deleting words from the Trie
deleteWord(root, "banana", 0)
print("\nAfter deleting 'banana':")
print("banana:", "Found" if search(root, "banana") else "Not found")
deleteWord(root, "car", 0)
print("\nAfter deleting 'car':")
print("car:", "Found" if search(root, "car") else "Not found")
print("cart:", "Found" if search(root, "cart") else "Not found")
# Deleting the entire Trie
deleteTrie(root)