Skip to content

TrieNode

Kal Ahmed edited this page Sep 26, 2016 · 1 revision

Home > Tries > Trie > TrieNode

TrieNode

A TrieNode is a node in a Trie, it provides methods for accessing data stored at the current node but also at all descendant nodes.

Usage

The following gives examples of how to use a TrieNode, the examples assume a Trie mapping from string to int as in the examples on the main Trie page.

You can get basic information about the node like so:

//Get the value at the Node (if any)
int value = node.Value;

//Get the key bit at the Node
char keybit = node.KeyBit;

//Is this the root node?
bool root = node.IsRoot;

//Is this node a leaf?
bool leaf = node.IsLeaf;

//Is there a value present at the node?
bool hasValue = node.HasValue;

//Get the count of immediate children
int childCount = node.Count;

//Get the count of all descendants
int count = node.CountAll;

While these are all useful the more interesting stuff are the ways of accessing all the data at a node. For example we can get all the values associated with the node and its descendants, this is important for implementing prefix searches:

IEnumerable<String> values = node.Values;
//Enumerate over as you wish...

Though perhaps the most interesting functionality is the ability to manipulate the tree structure relative to this node. We can add/remove children:

//Add a child with key bit x
node.AddChild('x');

//Remove the child with key bit y
node.RemoveChild('y');

Or we can trim the tree below the current node:

//Trim children more than one level below us
node.Trim(1);

Clone this wiki locally