Ternary search trees are very common in the text editing area. They could be used to implement Auto complete feature, spell checking, Partial-Match searching, Near-Neighbor Searching & many many other options.
The reason behind them being famous is their space efficiency compared to tries (Although tries are faster) & their flexibility (Compared to Hashtables).
Ternary search trees have three pointers in their data node : lokid
the left kid, hikid
the right kid & midkid
the middle kid.
The thing that I'm missing is why do we need the middle kid ? What is the particularity of this pointer ? In which things it would be better to have that pointer rather than not having it at all ?
I was drawing some trees & i think we can realize the same thing using only 2 pointers (A binary search tree) with insertions going to the left kid if current character in the string to insert is equal or less than the character on the current node and insertions going to the right the other way around.
The following picture shows a binary tree generated from inserting words: KARIM, KARAS, SARAS.
The insertion algorithm of the above tree could be something similar to the following pseudo code :
insert(root, word){
if(!root) {
root = new node()
root->char = *(word++)
}
if(*word > root->char)
insert(root->right, word)
else {
if(*word == root->char)
insert(root->left, word++)
else
insert(root->left, word)
}
}
What could possibly be wrong with the above conception ? Am i missing something about the ternary trees which makes them superior to binary trees ?