I have a tree search algorithm that uses multiple keys and each node has a sub tree which is searched with the next key in the list of keys until the keys run out and the data being searched is in that end node.
my code is working but I don't know what to call it. I thought it was a Ternary Tree until I read Wicki page and that doesn't seem to be it. So I just don't know what to call it.
here's my class. works like a binary tree with no limit on the number of keys where the set of keys are sent to the search/Insert functions as a list. Each time one key finds a node, the next key is stripped off the list and that node's "Next key Tree" repeats the process until it runs out of keys and sends back the data. My thinking is I can use it to mark exact searches of categories like "first name", "second name", "occupation", "city". they are entered in the same sequence and any sub-tree can be traversed. still not sure how much better this is than a regular binary-tree for strings. I have another exact version that has Integer keys that might come in more handy.
public class classTernaryTree_StringKey
{
classTernaryTree_StringKey_Node cRoot = null;
public void Insert(ref object data, List<string> lstKeys)
{
classTernaryTree_StringKey cMyRef = this;
_Insert(ref cMyRef, ref data, lstKeys);
}
static void _Insert(ref classTernaryTree_StringKey cTree, ref object data, List<string> lstKeys)
{
if (cTree.cRoot == null)
{
cTree.cRoot = new classTernaryTree_StringKey_Node();
cTree.cRoot.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cTree.cRoot.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cTree.cRoot.data = data;
return;
}
}
classTernaryTree_StringKey_Node cNode = cTree.cRoot;
do
{
int intComparison = string.Compare(lstKeys[0], cNode.key);
if (intComparison > 0)
{
if (cNode.Right != null)
cNode = cNode.Right;
else
{
cNode.Right = new classTernaryTree_StringKey_Node();
cNode.Right.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.Right.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.Right.data = data;
return;
}
}
}
else if (intComparison < 0)
{
if (cNode.Left != null)
cNode = cNode.Left;
else
{
cNode.Left = new classTernaryTree_StringKey_Node();
cNode.Left.key = lstKeys[0];
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.Left.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.Left.data = data;
return;
}
}
}
else
{
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
{
cNode.NextTree.Insert(ref data, lstKeys);
return;
}
else
{
cNode.data = data;
return;
}
}
} while (true);
}
public object Search(List<string> lstKeys)
{
classTernaryTree_StringKey cMyRef = this;
return _Search(ref cMyRef, lstKeys);
}
static object _Search(ref classTernaryTree_StringKey cTree, List<string> lstKeys)
{
if (cTree.cRoot == null) return null;
classTernaryTree_StringKey_Node cNode = cTree.cRoot;
do
{
int intComparison = string.Compare(lstKeys[0], cNode.key);
if (intComparison > 0)
{
if (cNode.Right != null)
cNode = cNode.Right;
else
return null;
}
else if (intComparison < 0)
{
if (cNode.Left != null)
cNode = cNode.Left;
else
return null;
}
else
{
lstKeys.RemoveAt(0);
if (lstKeys.Count > 0)
return cNode.NextTree.Search(lstKeys);
else
return cNode.data;
}
} while (true);
}
}
public class classTernaryTree_StringKey_Node
{
public string key = "";
public classTernaryTree_StringKey_Node Left = null;
public classTernaryTree_StringKey_Node Right = null;
public classTernaryTree_StringKey NextTree = new classTernaryTree_StringKey();
public object data = null;
}
The data structure you have here does actually look pretty similar to a ternary search tree, just with strings labeling the nodes rather than individual characters.
A ternary search tree is a way of encoding a trie data structure. Tries are trees representing sequences of elements. Usually, those elements are individual characters, but they could in principle be anything. Your data structure is essentially a ternary search tree where you store strings rather than characters at each node.