Ternary search tree Simple Python implementation

“From Wikipedia”

Ternary search tree is a special trie data structure where the child nodes of a standard trie are ordered as a binary search tree. Ternary search trees are effective for many applications involving mapping strings to other values as well as completing near-neighbor lookups and other string-related queries. In addition, ternary search trees are a more space efficient (at the cost of speed) equivalent of typical tries. Further, ternary search trees can be used any time a hashtable would be used to store strings.

The figure below shows a ternary search tree with the strings “as”, “at”, “cup”, “cute”, “he”, “i” and “us”:

Thanks to Wikipedia. :)

Python is an easy language to implement ideas and algorithms. Here is a simple implementation of Ternary search tree.

Lets roll..!!
First thing we need is a class to represent one character or one node. Let’s call it Node and define some member function and variables for it.

(You better read the theory part of Ternary Search Tree in wikipedia before reading the rest)

__init__ : This is how a constructor looks like in python. In c++ its class name itself (before 4 years).

So here defined 5 member variables : ch For storing the character the node is representing. For TST (ternary search tree) we need three pointer (You can think of C pointers concept). One for storing address/handle of node whose ch value is lesser than the what it stores and right for nodes storing address/handle of node whose ch value is greater than what it holds. And the center member hold the address of the node which store the character in the next level. Its little tricky lets say it like this left and right points to nodes where are is same level to the current node. We will come back to it after some time. And flag member variabe is for saying that this is the last character of a valid word, its a flag says that from here to top its a valid word.

So we created the skeleton of a node object, now we want to add methods to use TST.
Method to Add a node to the TST.

Three arguments to method they are : self (the object itself method is calling ), string (Word (or rest of word) add to TST), node (Currently where we are)

Method to search a string in TST- check whether a particular string is there or not

The next methods are for Auto completion tasks, like if we give string app as input, then function should return app, application, apple, applet etc(Provided all this words are in TST).

Method do a DFS in TST : spdfs

Driver search method this method is using spdfs to print auto completion candidates
Method search

Lets add some code for Data populating and to use TST.

Ternary search tree can be consider as Less memory equivalent Trie and not speedy as Trie.

Whole Code here without comments (Oh!! Thanks god :) ):

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>