Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Trie a K-ary tree?

If you look at the node definitions for a simple Trie and a simple K-ary tree, they look the same.

(using C++ notation)

template <size_t K>
trieNode
{
    trieNode *[K]
};

template <size_t K>
KaryNode
{
    KaryNode *[K]
};

At its simplest a K-ary tree has multiple children per node (2 for a binary tree)

And a Trie has "multiple children per node"

It seems that a K-ary tree makes it's choice of child based on comparison( < or > ) of Keys

While a Trie makes it's choice of child based on (unary) equality of sub-spans of the Key

Since neither data structure has made it into any standards, what would be best definition of each, and how would they be differentiated?

like image 323
Glenn Teitelbaum Avatar asked Jan 10 '14 04:01

Glenn Teitelbaum


People also ask

Is trie and tree same?

In computer science, a trie, also called digital tree or prefix tree, is a type of k-ary search tree, a tree data structure used for locating specific keys from within a set. These keys are most often strings, with links between nodes defined not by the entire key, but by individual characters.

What is AK ary tree?

The K-ary tree is a rooted tree, where each node can hold at most k number of children. If the value of k is 2, then this is known as binary tree. The binary tree, or ternary trees are some specialized k-ary trees. So k-ary trees re generalized. Example of K-ary Tree −

What is N Arr tree?

N-ary trees are tree data structures that allow us to have up to n children nodes for each of the nodes, differing from the standard binary trees which allow only up to 2 children nodes for each node.

What is full N-ary tree?

A complete n-ary tree is an n-ary tree in which the nodes at each tree level should have exactly n children(they are complete), except for the nodes at the last level. If the nodes at the last level are not complete, the nodes must be as left as possible.


1 Answers

From the point of view of the shape of the data structure, a trie is clearly an N-ary tree, in the same way that a balanced binary search tree is a binary tree, the difference being in how the data structure manages the data.

A binary search tree is a binary tree with additional constraint that the keys in the nodes are ordered, a balanced binary tree adds on top of that a constraint on the difference between the lengths of different branches.

Similarly, a trie is a N-ary tree with additional constrains that determine how the keys are managed.


Let's try a definition of what a trie is:

A trie is an efficient data structure used to implement a dictionary in which keys are sequences lexicographically. The implementation uses an N-ary tree where the branching factor is the range of valid values for each element in the key sequence[1] and each node may or not hold a value, but always holds a subsequence of the key being stored [2]. For each node in the tree, the concatenation of the subsequences of keys stored in the nodes from the root to any given node represent the key for the value stored, if the node holds a value, and/or a common prefix for all nodes in this subtree.

This layout of data allows for linear lookups on the size of the keys, and sharing the prefix allows for compact representations for many natural languages (like Spanish, where different forms of each verb differ only on the last few suffix characters).

1: That keys are sequences is an important premise, as the main advantage of the tries is that they split the key into different nodes along the path.

2: Depending on the implementation each node might maintain a single element (character) from the sequence or a combination.

like image 86
David Rodríguez - dribeas Avatar answered Sep 29 '22 00:09

David Rodríguez - dribeas