You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/Data Structures/Tries/trie.md
+14-17Lines changed: 14 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ for word in data_store:
37
37
```
38
38
39
39
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
41
41
42
42
```
43
43
e
@@ -48,7 +48,7 @@ However, if we represent the storage of numbers in a tree such that each letter
48
48
49
49
```
50
50
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...
52
52
The above representation is called a trie.
53
53
54
54
# Standard Trie Operations
@@ -58,22 +58,20 @@ The above representation is called a trie.
58
58
59
59
# Building a Trie
60
60
61
-
## Building a node for the elements of the trie
61
+
## Defining a node class for the elements of the trie
62
62
63
63
To start building a trie, you first need to define a node with the revelant attributes needed for any trie.
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
77
75
78
76
Then the trie gets built by creating a node for each letter and adding it as a child to the node before it
79
77
@@ -84,7 +82,7 @@ Start by initializing an empty node
84
82
```
85
83
class Trie:
86
84
def __init__(self):
87
-
self.node = Node(None)
85
+
self.node = Node()
88
86
```
89
87
90
88
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
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
105
103
106
-
First take a look at the code (ignore the return value)
0 commit comments