Skip to content

Commit ce9497f

Browse files
authored
Added Tries Content (#67)
As described. Make sure UKL reviews this as well.
2 parents 69f9561 + fe75945 commit ce9497f

2 files changed

Lines changed: 86 additions & 1 deletion

File tree

docs/Language/Python/Python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: default
33
title: Python
44
nav_order: 1
55
parent: Language
6-
has_children: false
6+
has_children: true
77
---
88

99
{{ page.title }}

docs/Language/Python/Tries.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
layout: default
3+
title: Tries
4+
nav_order: 1
5+
parent: Python
6+
grand_parent: Language
7+
has_children: false
8+
---
9+
10+
{{ page.title }}
11+
======================
12+
13+
In computer science, a trie (pronounced "try") is a tree-like data structure used to store a dynamic set or associative array where the keys are usually strings. The term "trie" comes from the word "retrieval," and the structure is sometimes called a "prefix tree" because it can efficiently store and retrieve keys based on their prefixes. Each node represents a single character or a part of a key.
14+
15+
The root node represents the empty string, and each level of the tree corresponds to a character in the keys. The nodes in a trie have links (edges) to other nodes, typically representing characters. Traversing the edges from the root to a particular node spells out a key.
16+
17+
Tries excel at handling keys with common prefixes. This makes them particularly efficient for tasks like string matching or autocomplete, where a common prefix needs to be shared among multiple keys. Retrieving a key from a trie is also highly efficient, typically taking time proportional to the length of the key. This is because each level of the trie corresponds to a character in the key.
18+
19+
Tries tend to have a high space complexity, especially for large datasets with many keys sharing common prefixes. However, various optimizations, such as compressing branches with a single child, can reduce space requirements.
20+
21+
-----------------------------------------------------------------------
22+
23+
## Implementation Considerations
24+
25+
1. Memory Usage: Tries can be memory-intensive, especially when dealing with a large number of keys. Various techniques, such as compression and optimization, can be applied to reduce memory usage.
26+
27+
2. Alphabet Size: The size of the alphabet (number of possible characters) can affect the performance of a trie. Larger alphabets may result in larger and more complex tries.
28+
29+
3. Node Representation: Nodes in a trie can be implemented using arrays, linked lists, or other data structures. The choice of representation impacts both time and space complexity.
30+
31+
{: .example }
32+
```python
33+
# Implementation Example of a Depth First Search within the Trie.
34+
35+
def PrintTree(rootNode):
36+
print("---- Root Children ----")
37+
#root nodes text
38+
for key in rootNode.children:
39+
print(key)
40+
print("-----------------------")
41+
42+
#children of children.
43+
stack = []
44+
45+
stack.append(rootnode)
46+
47+
while(len(stack) > 0):
48+
currentNode = stack.pop()
49+
50+
print(currentNode.value)
51+
52+
for key in currentNode.children:
53+
stack.append(currentNode.children[key])
54+
```
55+
56+
Credit: UKL
57+
58+
-----------------------------------------------------------------------
59+
60+
## Use Cases
61+
62+
1. Autocomplete and Predictive Text: Tries are often used in autocomplete systems where users are presented with suggested words or phrases based on partial inputs.
63+
64+
2. Spell Checking: Tries can be used in spell-checking algorithms to efficiently identify misspelled words by traversing the trie and checking for valid words.
65+
66+
3. IP Routing Tables: In networking, tries are used in IP routers to efficiently search for the longest prefix match in IP routing tables.
67+
68+
4. Symbol Tables: Tries are used in symbol tables for compilers and interpreters to efficiently look up variable and function names.
69+
70+
5. Distributed Databases: Tries can be employed in distributed databases for efficiently storing and querying key-value pairs.
71+
72+
-----------------------------------------------------------------------
73+
74+
## Sources
75+
76+
https://www.geeksforgeeks.org/pattern-searching-using-trie-suffixes/
77+
78+
https://www.geeksforgeeks.org/count-distinct-substrings-string-using-suffix-trie/
79+
80+
https://www.hackerearth.com/practice/notes/trie-suffix-tree-suffix-array/
81+
82+
---
83+
84+
#### Author: JDSherbert
85+
#### Published: 29/12/2023

0 commit comments

Comments
 (0)