Questions tagged [trie]

A tree-like data structure used to hold an associative array, also called a Prefix Tree.

Tries are specialized data structures where a word can be stored as a sequence of characters. Reading the word involves traversing down the branch of the tree. At each node, the possible completions of the partial word can be found by traversing down all possible paths to the leaf level. It is useful for implementing dictionary based functionality such as autocomplete.

1108 questions
8
votes
1 answer

STLish lower_bound function for Radix/Patricia Trie

Lately I've been studying Patricia tries, and working with a really good C++ implementation which can be used as an STL Sorted Associative Container. Patricia tries differ from normal binary trees because leaf nodes have back pointers which point…
Channel72
  • 283
  • 1
  • 2
  • 5
8
votes
4 answers

What would be a sensible way to implement a Trie in .NET?

I get the concept behind a trie. But I get a little confused when it comes to implementation. The most obvious way I could think to structure a Trie type would be to have a Trie maintain an internal Dictionary. I have in fact written one…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
8
votes
1 answer

How to retrieve a random word of a given length from a Trie

I have a simple Trie that I'm using to store about 80k words of length 2 - 15. It works great for checking to see if a string is a word; However, now I need a way of getting a random word of a given length. In other words, I need…
DevOfZot
  • 1,362
  • 1
  • 13
  • 26
8
votes
4 answers

How to print all words in a Trie?

I am trying to create a Trie Implementation in C++. I cannot figure out how to print all words stored in the Trie. This is how I've implemented the TrieNode. struct TrieNode{ bool isWord; int data; //Number of times Word Occured TrieNode…
theUser
  • 1,346
  • 3
  • 21
  • 40
8
votes
9 answers

Efficient String/Pattern Matching in C++ (suffixarray, trie, suffixtree?)

I'm looking for an efficient data structure to do String/Pattern Matching on an really huge set of strings. I've found out about tries, suffix-trees and suffix-arrays. But I couldn't find an ready-to-use implementation in C/C++ so far (and…
Constantin
  • 8,721
  • 13
  • 75
  • 126
7
votes
2 answers

Search for cyclic strings

I am looking for the most efficient way to store binary strings in a data structure (insert function) and then when getting a string I want to check if some cyclic string of the given string is in my structure. I thought about storing the input…
user550413
  • 4,609
  • 4
  • 25
  • 26
7
votes
1 answer

Why aren't C++ maps implemented as tries?

Tries are very fast data structures. Looking up a word takes O(sizeofword) time, while std::maps are self-balacing trees. Why aren't the standard C++ map templates implemented with tries. Is there any specific reason? Are there any tradeoffs of…
Global Warrior
  • 5,050
  • 9
  • 45
  • 75
7
votes
1 answer

Scrabble word finder: building a trie, storing a trie, using a trie?

What I’m trying to do: Build a mobile web application where the user can get help finding words to play when playing scrabble Users get word suggestions by typing in any amount of letters and 0 or more wildcards How I’m trying to do this: Using…
Linus Jäderlund
  • 515
  • 5
  • 13
7
votes
3 answers

Data Structure for representing patterns in strings

I'm looking for a good data structure to represent strings of the form: Domain:Key1=Value1,Key2=Value2... Each "Domain" can contain the following pattern characters - *, ? (* - 0 or more characters, ? - 0 or 1 character) Each "Key" can contain the…
Raj
  • 71
  • 1
7
votes
2 answers

Number of Distinct substring of a string

I was solving DISTINCT SUBSTRING (given a string, we need to find the total number of its distinct substrings). I am using trie of suffixes to solve it. I am passing the test cases, but getting TLE when I submit. Also, the space consumed is very…
aka1234
  • 71
  • 1
7
votes
2 answers

Find the subarray with the max XOR from an array (using a trie)

The problem statement is: Given an array of integers, find the subarray with maximum XOR. Some examples are: Input: arr[] = {1, 2, 3, 4} Output: 7 The subarray {3, 4} has maximum XOR value Input: arr[] = {8, 1, 2, 12, 7, 6} Output: 15 The…
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87
7
votes
1 answer

Trie for Unicode character set

I have to match an input string against a set of prefixes. The match should be the best possible, such that if there are both abcd* and abcde*, then abcdef should match abcde*. I am using Trie for this. The problem is the character in the input and…
ptntialunrlsd
  • 794
  • 8
  • 23
7
votes
4 answers

Optimizing construction of a trie over all substrings

I am solving a trie related problem. There is a set of strings S. I have to create a trie over all substrings for each string in S. I am using the following routine: String strings[] = { ... }; // array containing all strings for(int i = 0; i <…
Bhoot
  • 2,614
  • 1
  • 19
  • 36
7
votes
3 answers

Memory Efficient data structure for Trie Implementation

I am implementing a Trie in python. Till now I have come across two different methods to implement it: 1) use a class Node (similar to struct Node in C++) with data members: char - to store character is_end - to store end of word (true or…
divyum
  • 1,286
  • 13
  • 20
7
votes
2 answers

Unable to port C++ code that inserts into a trie to Rust due to multiple mutable borrows

I have the following C++ code: #include #include using namespace std; struct Trie { bool eow; //end of word char val; vector chd; //children void push_word(const string& word){ Trie& trie = *this; …
András Kovács
  • 29,931
  • 3
  • 53
  • 99