Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Tools

Algorithm Alley


A Real Program

Dr. Dobb's Journal June 1998

A Real Program


Robert Sedgewick and I described ternary search trees in the April 1998 issue of DDJ. When we worked on the data structure, we were both pleasantly surprised to find that a recursive search function was as fast as an iterative function (under optimizing compilers). The run times for the two functions are shown in the first two rows of Table 3. We therefore assumed that our other recursive search functions were competitive with the iterative versions. After performing the experiments in this article, I decided to go back and test the assumption.

The pmsearch function in (Listing Two) performs a partial-match search in a tree. The query can include dots as "don't care" characters, so ".at" matches bat, cat, and a dozen other three-letter words. pmsearch calls itself in three distinct places, and returns to do more work after two of those calls.

The function pmsearch2 is in Listing Three. It is almost twice as long as function pmsearch, but only a bit harder to understand. The new code has two primary blocks, depending on whether the current character is a dot. Both of the cases ended with a tail recursion, so I explicitly transformed that to a while loop. The speedups ranged from a third to a factor of three, which I felt was worth a dozen lines of code and a couple hours of programming. I took a similar approach to a function for near-neighbor searching in ternary search trees, with similar results in code length and run time. The code for these experiments is available electronically; see "Resource Center," page 3.

-- J.B.

Back to Main Article

Listing Two

  void pmsearch(Tptr p, char *s)
  {   if (!p) return;
      nodecnt++;
      if (*s == '.' || *s < p->splitchar)
          pmsearch(p->lokid, s);
      if (*s == '.' || *s == p->splitchar)
          if (p->splitchar && *s)
              pmsearch(p->eqkid, s+1);
      if (*s == 0 && p->splitchar == 0)
          srcharr[srchtop++] =
              (char *) p->eqkid;
      if (*s == '.' || *s > p->splitchar)
          pmsearch(p->hikid, s);
  }

Back to Text

Listing Three

 void pmsearch2(Tptr p, char *s)
 {   while (p) {
         nodecnt++;
         if (*s == '.') {
             pmsearch2(p->lokid, s);
             pmsearch2(p->hikid, s);
             if (p->splitchar == 0) return;
             p = p->eqkid;
             ++s;
         } else {
             if (*s < p->splitchar)
                 p = p->lokid;
             else if (*s > p->splitchar)
                 p = p->hikid;
             else /* *s == p->splitchar */ {
                 if (*s == 0) {
                     if (p->splitchar == 0)
                         srcharr[srchtop++] =
                             (char *) p->eqkid;
                     return;
                 }
                 p = p->eqkid;
                 ++s;
             }
         }
     }
 }

Back to Text


Copyright © 1998, Dr. Dobb's Journal


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.