Tries
Java
Node for Trie
TrieNode.java
import java.util.ArrayList;
public class TrieNode {
char letter;
int value;
TrieNode parent = null;
ArrayList<TrieNode> children = new ArrayList<TrieNode>();
public TrieNode(char c, int v) {
letter = c;
value = v;
}
void addString(String s, int y) {
System.out.print("Adding String " + "\"" + s + "\"...");
System.out.println();
System.out.println();
TrieNode n = this;
boolean noMoreMatching = false;
while (!noMoreMatching) {
int i;
int childrenSize = n.children.size();
for (i = 0; i < n.children.size(); i++) {
if (n.children.get(i).letter == s.charAt(0)) {
n = n.children.get(i);
s = s.substring(1, s.length());
break;
}
}
if (i == childrenSize) {
noMoreMatching = true;
}
}
if (s.length() > 0) {
addRemainingString(n, s, y);
}
printTrie();
}
void addRemainingString(TrieNode n, String s, int y) {
for (int i = 0; i < s.length() - 1; i++) {
TrieNode m = n.addChild(s.charAt(i), 0);
n = m;
}
n.addChild(s.charAt(s.length() - 1), y);
}
TrieNode addChild(char c, int v) {
TrieNode child = new TrieNode(c, v);
children.add(child);
child.setParent(this);
return child;
}
void setParent(TrieNode newParent) {
parent = newParent;
}
void printTrie() {
TrieNode n = this;
printSubTree(n, 0);
System.out.println();
}
int printSubTree(TrieNode n, int level) {
for (int a = 0; a < level; a++) {
System.out.print("---");
}
System.out.print(n.letter);
if (n.value != 0) {
System.out.print(" " + n.value);
}
System.out.println();
if (n.children.size() == 0) {
return n.letter;
}
int size = n.children.size();
level++;
for (int i = 0; i < size; i++) {
printSubTree(n.children.get(i), level);
}
return -1;
}
}
Implementation of Trie
ImplementTrie.java
public class ImplementTrie {
public static void main(String[] args) {
System.out.println("Initializing Trie");
TrieNode trie = createTrie();
trie.printTrie();
trie.addString("as", 2);
trie.addString("assign", 8);
trie.addString("chocolate", 6);
trie.addString("nat", 7);
trie.addString("assignment", 5);
trie.addString("nation", 3);
trie.addString("national", 1);
trie.addString("assignments", 9);
}
public static TrieNode createTrie() {
TrieNode root = new TrieNode('.', 0);
return root;
}
}
C#
Node for Trie
TrieNode.cs
using System;
using System.Collections.Generic;
public class TrieNode
{
char letter;
int value;
TrieNode parent = null;
List<TrieNode> children = new List<TrieNode>();
public TrieNode(char c, int v)
{
letter = c;
value = v;
}
public void addString(String s, int y)
{
Console.Write("Adding String " + "\"" + s + "\"...");
Console.WriteLine();
Console.WriteLine();
TrieNode n = this;
Boolean noMoreMatching = false;
while (!noMoreMatching)
{
int i;
int childrenSize = n.children.Count;
for (i = 0; i < n.children.Count; i++)
{
if (n.children[i].letter == s[0])
{
n = n.children[i];
s = s.Substring(1, s.Length - 1);
break;
}
}
if (i == childrenSize)
{
noMoreMatching = true;
}
}
if (s.Length > 0)
{
addRemainingString(n, s, y);
}
printTrie();
}
public void addRemainingString(TrieNode n, String s, int y)
{
for (int i = 0; i < s.Length - 1; i++)
{
TrieNode m = n.addChild(s[i], 0);
n = m;
}
n.addChild(s[s.Length - 1], y);
}
public TrieNode addChild(char c, int v)
{
TrieNode child = new TrieNode(c, v);
children.Add(child);
child.setParent(this);
return child;
}
public void setParent(TrieNode newParent)
{
parent = newParent;
}
public void printTrie()
{
TrieNode n = this;
printSubTree(n, 0);
Console.WriteLine();
}
public int printSubTree(TrieNode n, int level)
{
for (int a = 0; a < level; a++)
{
Console.Write("---");
}
Console.Write(n.letter);
if (n.value != 0)
{
Console.Write(" " + n.value);
}
Console.WriteLine();
if (n.children.Count == 0)
{
return n.letter;
}
int size = n.children.Count;
level++;
for (int i = 0; i < size; i++)
{
printSubTree(n.children[i], level);
}
return -1;
}
}
Implementation of Trie
ImplementTrie.cs
using System;
public class ImplementTrie
{
public static void Main()
{
Console.WriteLine("Initializing Trie");
TrieNode trie = createTrie();
trie.printTrie();
trie.addString("as", 2);
trie.addString("assign", 8);
trie.addString("chocolate", 6);
trie.addString("nat", 7);
trie.addString("assignment", 5);
trie.addString("nation", 3);
trie.addString("national", 1);
trie.addString("assignments", 9);
}
public static TrieNode createTrie()
{
TrieNode root = new TrieNode('.', 0);
return root;
}
}
Console Output:
ImplementList.cpp
Initializing Trie
.
Adding String "as"...
.
---a
------s 2
Adding String "assign"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
Adding String "chocolate"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
Adding String "nat"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
---n
------a
---------t 7
Adding String "assignment"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---------------------m
------------------------e
---------------------------n
------------------------------t 5
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
---n
------a
---------t 7
Adding String "nation"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---------------------m
------------------------e
---------------------------n
------------------------------t 5
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
---n
------a
---------t 7
------------i
---------------o
------------------n 3
Adding String "national"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---------------------m
------------------------e
---------------------------n
------------------------------t 5
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
---n
------a
---------t 7
------------i
---------------o
------------------n 3
---------------------a
------------------------l 1
Adding String "assignments"...
.
---a
------s 2
---------s
------------i
---------------g
------------------n 8
---------------------m
------------------------e
---------------------------n
------------------------------t 5
---------------------------------s 9
---c
------h
---------o
------------c
---------------o
------------------l
---------------------a
------------------------t
---------------------------e 6
---n
------a
---------t 7
------------i
---------------o
------------------n 3
---------------------a
------------------------l 1