Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:45+00:00 2026-06-15T07:35:45+00:00

I want to modify an recursive function from an ‘ternary search tree’ library (

  • 0

I want to modify an recursive function from an ‘ternary search tree’ library (sourceforge & http://code.google.com/p/ternary-search-tree/).
The default behaviour is to search in an ternary search tree for all occurences of strings that match an specified wildcard string.
i.e. having ‘KEY’, ‘KE1’, ‘KE2’ in the tree would find all entrys if I search for ‘KE*’.
But I need the opposite behaviour – search in an ternary search tree (which contains wildcards) all entrys that match an specified string.
i.e. having ‘KE*’, ‘KEY’, ‘K*’ in the tree should find all entrys if I search for ‘KEY’.

A tree/node is defined as following:

typedef struct TstNode {
    TstNode( char c ) : splitChar(c), left(0), right(0), mid(0){
    }
    char splitChar;
    TstTree left, right;
    union {
        TstTree mid;
        int index;
    };
} tstNode;

And the function with the default behaviour:

template <class Object>
void TernarySearchTree<Object>::partialMatchSearch(TstTree tree, const char *key)
{
    if (!tree) return;

    // partial match left
    if (*key == '?' || *key == '*' || *key < tree->splitChar)
    {
        partialMatchSearch( tree->left, key );
    }
    // partial match middle
    if (*key == '?' || *key == '*' || *key == tree->splitChar)
    {
        if ( tree->splitChar && *key )
        {
            if ( *key == '*' )
            {
                partialMatchSearch( tree->mid, key );
            }
            else
            {
                partialMatchSearch( tree->mid, key+1 ); // search next pattern char
            }
        }
    }
    if ( ( *key == 0 ||  *key == '*' ) && tree->splitChar == 0 )
    {
        pmVectorPtr->add( tree->index );
    }

    if (*key == '?' || *key == '*' || *key > tree->splitChar)
    {
        partialMatchSearch( tree->right, key );
    }
}

pmVectorPtr is an Pointer to an Vector of int’s and the function get’s called with the root-element and the searchkey as argument. I already tried to adapt that, but can’t get my head around it yet. My own code:

template <class Object>
void TernarySearchTree<Object>::partialMatchSearchInverted(TstTree tree, const char *key)
{
    if (!tree) return;

    if((tree->splitChar == '*') && ( *key != 0 )){
        partialMatchSearchInverted( tree, key+1 );
    }

    if( *key != 0 ){
        if (*key < tree->splitChar){
            partialMatchSearchInverted( tree->left, key );
        }
        if (*key > tree->splitChar){
            partialMatchSearchInverted( tree->right, key );
        }
    }
    if ((*key == tree->splitChar) || (tree->splitChar == '*')){
        if ( tree->splitChar || *key ){
            partialMatchSearchInverted( tree->mid, key+1 ); // search next pattern char
        }
    }
    if ( ( *key == 0 ) && ( tree->splitChar == 0 ) ){
        pmVectorPtr->add( tree->index );
    }
}

I’ve coded this with extensive use of the debugger and as far as I can tell, it ‘seems’ to work (even if the wildcard is at beginning or mid of a string). BUT if I add to example: ‘K*’ and ‘Ke*’ to the tree, it would find only one solution (in this case Ke*) for ‘Key’. If I remove ‘Ke*’ from the tree, it finds ‘K*’ for an search query of ‘Key’. I still don’t get why.

Any ideas about that?


Appendix (my testcase):

#include <iostream>
#include "ternarySearchTree/ternarySearchTree.h"

int main(int argc, char *argv[]){
    TernarySearchTree<string> tst;
    Vector< TstItem<String> > itemVector;
    {
        TstItem<String> item( "Ke*", "Value" );
        itemVector.add( item );
    }
    {
        TstItem<String> item( "K*", "Value" );
        itemVector.add( item );
    }
    {
        TstItem<String> item( "Ka*", "Value" );
        itemVector.add( item );
    }
    tst.buildBalancedTree(itemVector);

    Vector<int> matches = tst.partialMatchSearchInverted("Key");// will only find Ke* (wrong - it should find Ke* and K*), if I remove Ke* above it would find K* (right), if I remove that also it would find nothing (also right)
    for (unsigned j=0;j<matches.count();j++)
    {
        std::cout<<"Matching: "<< tst.getKey( matches[j] ) <<" -  "<< tst.getValue( matches[j] )->c_str()<<std::endl;
    }
    std::cout<<"total matches "<< matches.count()<<std::endl;
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:35:46+00:00Added an answer on June 15, 2026 at 7:35 am

    Ok, I could finally track down my issue: I still hadn’t understand how EXACTLY a ternary tree works at all. Because of that, I didn’t visit the left and right nodes when an wildcard could be somewhere inside these branches (sadly that’s almost everytime). So my final algorithm is MUCH slower than a search in an ternary tree should be at all (I’m afraid the complexity would be probably something like O(n)), but at least I got it to work.

    So it seems this data structure isn’t suitable for my needs (searching for wildcard strings) – I would probably get the same speed with an linear list. However, if someone with similiar problems will find this question some day, here’s my code (but I don’t suggest to use it in real applications, cause it’s slow and I guess there are other structures around, that can handle these things much better):

    template <class Object>
    void TernarySearchTree<Object>::partialMatchSearchInverted(TstTree tree, const char *key)
    {
        if (!tree) return;
        if((tree->splitChar == '*') && ( *key != 0 )){
            partialMatchSearchInverted( tree, key+1 ); // wildcard occured, search same node with next key
        }
        if( *key != 0 ){
            if (*key < tree->splitChar){
                partialMatchSearchInverted( tree->left, key );
            }else if('*' < tree->splitChar){ // a wildcard could be in this branch
                partialMatchSearchInverted( tree->left, key );
            }
            if (*key > tree->splitChar){
                partialMatchSearchInverted( tree->right, key );
            }else if('*' > tree->splitChar){ // a wildcard could be here too
                partialMatchSearchInverted( tree->right, key );
            }
        }
        if ((*key == tree->splitChar) || (tree->splitChar == '*')){
            if (*key != 0){
                partialMatchSearchInverted( tree->mid, key+1 ); // search next pattern char
            }
        }
        if ( ( *key == 0 ) && ( tree->splitChar == 0 ) ){
            pmVectorPtr->add( tree->index );
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to modify my function that strips the tag from the value of
I want to modify the source code of Nginx (http://nginx.org/download/nginx-0.7.67.tar.gz) so when it serves
I have this code about binary search tree , I want Calculated efficancy for
I want to modify the below code to read the nodes from a text
I want to modify some code in the Eclipse JDT itself (In the Debug
I want to modify a view from Action Helper in Zend Framework in preDispatch()
I want to modify globals declared in java SDK via an NDK C function,
I want to modify this code which works pretty good but (or I don't
I want to modify a local variable in a function of extension method. See
I want to modify FTP so that it recovers from the last session during

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.