kopia lustrzana https://github.com/animator/learn-python
Update trie.md
rodzic
1b0bd26cbe
commit
04bc0893a2
|
@ -27,7 +27,7 @@ Node Class have mainly two components
|
||||||
|
|
||||||
Code Block of Node Class :
|
Code Block of Node Class :
|
||||||
|
|
||||||
```
|
```python
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.alphabets = [None] * 26
|
self.alphabets = [None] * 26
|
||||||
|
@ -40,7 +40,7 @@ Now we need to implement Trie. We create another class named Trie with some meth
|
||||||
|
|
||||||
Code Implementation of Initialization:
|
Code Implementation of Initialization:
|
||||||
|
|
||||||
```
|
```python
|
||||||
class Trie:
|
class Trie:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.root = Node()
|
self.root = Node()
|
||||||
|
@ -51,7 +51,7 @@ class Trie:
|
||||||
1. **Insertion**: Inserts the word into the Trie. This method takes `word` as parameter. For each character in the word, it checks if there is a corresponding child node. If not, it creates a new `Node`. After processing all the characters in word, it increments the `end_of_word` value of the last node.
|
1. **Insertion**: Inserts the word into the Trie. This method takes `word` as parameter. For each character in the word, it checks if there is a corresponding child node. If not, it creates a new `Node`. After processing all the characters in word, it increments the `end_of_word` value of the last node.
|
||||||
|
|
||||||
Code Implementation of Insertion:
|
Code Implementation of Insertion:
|
||||||
```
|
```python
|
||||||
def insert(self, word):
|
def insert(self, word):
|
||||||
node = self.root
|
node = self.root
|
||||||
for char in word:
|
for char in word:
|
||||||
|
@ -69,7 +69,7 @@ There are two cases in Searching:
|
||||||
- *Word found*: It happens when the search word is present in the Trie. This case will occur, when the `end_of_word` value is greater than `0` of the node after traversing the whole word.
|
- *Word found*: It happens when the search word is present in the Trie. This case will occur, when the `end_of_word` value is greater than `0` of the node after traversing the whole word.
|
||||||
|
|
||||||
Code Implementation of Searching:
|
Code Implementation of Searching:
|
||||||
```
|
```python
|
||||||
def Search(self, word):
|
def Search(self, word):
|
||||||
node = self.root
|
node = self.root
|
||||||
for char in word:
|
for char in word:
|
||||||
|
@ -84,7 +84,7 @@ Code Implementation of Searching:
|
||||||
|
|
||||||
Code Implementation of Deletion:
|
Code Implementation of Deletion:
|
||||||
|
|
||||||
```
|
```python
|
||||||
def delete(self, word):
|
def delete(self, word):
|
||||||
node = self.root
|
node = self.root
|
||||||
for char in word:
|
for char in word:
|
||||||
|
@ -96,7 +96,7 @@ def delete(self, word):
|
||||||
|
|
||||||
Python Code to implement Trie:
|
Python Code to implement Trie:
|
||||||
|
|
||||||
```
|
```python
|
||||||
class Node:
|
class Node:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.alphabets = [None] * 26
|
self.alphabets = [None] * 26
|
||||||
|
|
Ładowanie…
Reference in New Issue