Skip to content

Commit 197d0a1

Browse files
committed
corrected the name tries.md to trie.md to match convention
1 parent ff6ddc4 commit 197d0a1

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

en/Data Structures/Tries/trie.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for word in data_store:
3737
```
3838

3939
Without a doubt, this strategy will work, but the time complexity of doing this is *O(num of words x len of longest word)* which is quite expensive.
40-
However, if we represent the storage of numbers in a tree such that each letter appears only once in a particular level in the tree, we can achieve a much better search time
40+
However, if we represent the storage of numbers in a tree such that each letter appears only once in a particular level in the tree, we can achieve a much better search time. Take, for example, the tree below
4141

4242
```
4343
e
@@ -48,7 +48,7 @@ However, if we represent the storage of numbers in a tree such that each letter
4848
4949
```
5050

51-
You can see from the above tree representation, that all the words are in the tree, starting from the letter e, which starts all the words, then a, n, and g coming next and so on...
51+
You can see from the above representation, that all the words are in the tree, starting from the letter e, which is found at the beginning of all the words, then a, n, and g coming in the next level and so on...
5252
The above representation is called a trie.
5353

5454
# Standard Trie Operations
@@ -58,22 +58,20 @@ The above representation is called a trie.
5858

5959
# Building a Trie
6060

61-
## Building a node for the elements of the trie
61+
## Defining a node class for the elements of the trie
6262

6363
To start building a trie, you first need to define a node with the revelant attributes needed for any trie.
6464

6565
```
6666
class Node:
67-
def __init__(self, val: string=None, is_word: bool=False):
68-
self.val = val
67+
def __init__(self, is_word: bool=False):
6968
self.is_word = is_word
7069
self.children = {}
7170
```
7271

7372
Here, you can see that the class `Node` has three instance attributes:
74-
1. val: *string* = to hold the value / text of the node
75-
2. isword: *bool* = to mark whether that node in the trie marks the completion of a word
76-
3. children: *Dict* = to hold pointers to other children nodes
73+
1. is_word: *bool* = to mark whether that node in the trie marks the completion of a word
74+
2. children: *Dict* = to hold pointers to other children nodes
7775

7876
Then the trie gets built by creating a node for each letter and adding it as a child to the node before it
7977

@@ -84,7 +82,7 @@ Start by initializing an empty node
8482
```
8583
class Trie:
8684
def __init__(self):
87-
self.node = Node(None)
85+
self.node = Node()
8886
```
8987

9088
For the insert operation, fetch the starting node, then for every letter in the word, add it to the children of the letter before it. The final node has its `is_word` attribute marked as **True** because we want to be aware of where the word ends
@@ -94,7 +92,7 @@ def insert(self, word: str) -> None:
9492
node = self.node
9593
for ltr in word:
9694
if ltr not in node.children:
97-
node.children[ltr] = Node(ltr)
95+
node.children[ltr] = Node()
9896
node = node.children[ltr]
9997
node.is_word=True
10098
```
@@ -103,7 +101,7 @@ def insert(self, word: str) -> None:
103101

104102
For the search operation, fetch the starting node, then for every letter in the word, check if it is present in the `children` attribute of the current node. As long as it is present, repeat for the next letter and next node. If during the search process, we find a letter that is not present, then the word does not exist in the trie. If we successfully get to the end of the iteration, then we have found what we are looking for. It is time to return a value
105103

106-
First take a look at the code (ignore the return value)
104+
Take a look at the code
107105

108106
```
109107
def search(self, word: str) -> bool:
@@ -112,7 +110,7 @@ def search(self, word: str) -> bool:
112110
if ltr not in node.children:
113111
return False
114112
node = node.children[ltr]
115-
return node.isword
113+
return node.is_word
116114
```
117115

118116
For the return value, there are two cases:
@@ -123,24 +121,23 @@ Now here is the full code
123121

124122
```
125123
class Node:
126-
def __init__(self, val: string=None, is_word: bool=False):
127-
self.val = val
124+
def __init__(self, is_word: bool=False):
128125
self.is_word = is_word
129126
self.children = {}
130127
131128
class Trie:
132129
133130
def __init__(self):
134-
self.node = Node(None)
131+
self.node = Node()
135132
136133
137134
def insert(self, word: str) -> None:
138135
node = self.node
139136
for ltr in word:
140137
if ltr not in node.children:
141-
node.children[ltr] = Node(ltr)
138+
node.children[ltr] = Node()
142139
node = node.children[ltr]
143-
node.isword=True
140+
node.is_word=True
144141
145142
146143
def search(self, word: str) -> bool:

0 commit comments

Comments
 (0)