Skip to content

Commit 1e3f839

Browse files
Basic Search Ternary Search Tree Implementation
1 parent f646ffd commit 1e3f839

File tree

3 files changed

+167
-0
lines changed

3 files changed

+167
-0
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DataStructures.Trees
6+
{
7+
public class TernarySearchTree
8+
{
9+
public TernaryTreeNode Root { get; private set; }
10+
11+
public void Insert(string word)
12+
{
13+
if (string.IsNullOrWhiteSpace(word))
14+
throw new Exception("Inputted value is empty");
15+
16+
if (Root == null)
17+
Root = new TernaryTreeNode(null, word[0], word.Length == 1);
18+
19+
WordInsertion(word);
20+
}
21+
22+
public void Insert(string[] words)
23+
{
24+
foreach (var word in words)
25+
{
26+
Insert(word);
27+
}
28+
}
29+
30+
void WordInsertion(string word)
31+
{
32+
int index = 0;
33+
TernaryTreeNode currentNode = Root;
34+
35+
while (index < word.Length)
36+
currentNode = ChooseNode(currentNode, word, ref index);
37+
}
38+
39+
TernaryTreeNode ChooseNode(TernaryTreeNode currentNode, string word, ref int index)
40+
{
41+
//Center Branch
42+
if (word[index] == currentNode.Value)
43+
{
44+
index++;
45+
46+
if (currentNode.GetMiddleChild == null)
47+
InsertInTree(currentNode.AddMiddleChild(word[index], word.Length == index + 1), word, ref index);
48+
49+
50+
return currentNode.GetMiddleChild;
51+
}
52+
//Right Branch
53+
else if (word[index] > currentNode.Value)
54+
{
55+
if (currentNode.GetRightChild == null)
56+
InsertInTree(currentNode.AddRightChild(word[index], word.Length == index + 1), word, ref index);
57+
58+
return currentNode.GetRightChild;
59+
}
60+
//Left Branch
61+
else
62+
{
63+
if (currentNode.GetLeftChild == null)
64+
InsertInTree(currentNode.AddLeftChild(word[index], word.Length == index + 1), word, ref index);
65+
66+
return currentNode.GetLeftChild;
67+
}
68+
}
69+
70+
void InsertInTree(TernaryTreeNode currentNode, string word, ref int currentIndex)
71+
{
72+
int length = word.Length;
73+
74+
currentIndex++;
75+
var currNode = currentNode;
76+
for (int i = currentIndex; i < length; i++)
77+
currNode = currNode.AddMiddleChild(word[i], word.Length == currentIndex + 1);
78+
79+
currentIndex = length;
80+
}
81+
}
82+
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace DataStructures.Trees
6+
{
7+
public class TernaryTreeNode
8+
{
9+
public virtual TernaryTreeNode Parent { get; private set; }
10+
protected virtual TernaryTreeNode[] childs { get; set; }
11+
12+
public virtual TernaryTreeNode GetLeftChild { get { return childs[0]; } }
13+
public virtual TernaryTreeNode GetMiddleChild { get { return childs[1]; } }
14+
public virtual TernaryTreeNode GetRightChild { get { return childs[2]; } }
15+
16+
public virtual bool FinalLetter { get; set; }
17+
public virtual char Value { get; set; }
18+
19+
public TernaryTreeNode(TernaryTreeNode parent)
20+
{
21+
this.Parent = parent;
22+
childs = new TernaryTreeNode[3];
23+
}
24+
25+
public TernaryTreeNode(TernaryTreeNode parent, char value, bool isFinal)
26+
{
27+
this.Parent = parent;
28+
this.Value = value;
29+
this.FinalLetter = isFinal;
30+
childs = new TernaryTreeNode[3];
31+
}
32+
33+
public virtual TernaryTreeNode AddLeftChild(char value, bool isFinal)
34+
{
35+
childs[0] = new TernaryTreeNode(this, value, isFinal);
36+
return childs[0];
37+
}
38+
public virtual TernaryTreeNode AddRightChild(char value, bool isFinal)
39+
{
40+
childs[2] = new TernaryTreeNode(this, value, isFinal);
41+
return childs[2];
42+
}
43+
public virtual TernaryTreeNode AddMiddleChild(char value, bool isFinal)
44+
{
45+
childs[1] = new TernaryTreeNode(this, value, isFinal);
46+
return childs[1];
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using DataStructures.Trees;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace UnitTest.DataStructuresTests
8+
{
9+
public static class TernarySearchTreeTest
10+
{
11+
12+
// c
13+
// / | \
14+
// a u h
15+
// | | | \
16+
// t t e u
17+
// / / | / |
18+
// s p e i s
19+
[Fact]
20+
public static void DoTest()
21+
{
22+
string[] words = new string[] { "cute", "cup", "at", "as", "he", "us", "i" };
23+
24+
TernarySearchTree tree = new TernarySearchTree();
25+
26+
tree.Insert(words);
27+
28+
Assert.Equal('c', tree.Root.Value);
29+
Assert.Equal('h', tree.Root.GetRightChild.Value);
30+
Assert.Equal('e', tree.Root.GetRightChild.GetMiddleChild.Value);
31+
Assert.Equal('p', tree.Root.GetMiddleChild.GetMiddleChild.GetLeftChild.Value);
32+
Assert.Equal('s', tree.Root.GetLeftChild.GetMiddleChild.GetLeftChild.Value);
33+
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)