Skip to content

Commit 25df94e

Browse files
committed
Added Tries Content
1 parent 0497738 commit 25df94e

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

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+
has_children: false
7+
---
8+
9+
{{ page.title }}
10+
======================
11+
12+
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.
13+
14+
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.
15+
16+
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.
17+
18+
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.
19+
20+
-----------------------------------------------------------------------
21+
22+
## Implementation Considerations
23+
24+
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.
25+
26+
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.
27+
28+
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.
29+
30+
{: .example }
31+
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)