EarthWeb
Find the lowest price for a variety of products:


Featured Categories:
Computers
Digital Cameras
Home Theatre
Video Games
Laptops
Software
Electronics
PDA's

Developer.com
Shop for Software
CodeGuru Sites
Visual C++ / MFC / C++
.NET (C# and more)
Visual Basic
Gamelan (Java)
JARs (Java Applets)
Developer.com

submission guidelines

Interact
Discussion Boards
Newsletters (subscribe)
Guestbook
Recommend Us!

Of Interest
Books on .NET
Book Reviews
Newsletters (archived)
About Us
Authors 

Article Sections
C++
algorithms & formulas
c++ & mfc
date & time
managed C++ 
string
COM-based Technologies
atl & wtl
com & activex
com+
shell programming
Controls
button control
combobox
edit control
imagelist control
listbox control
listview control
menu
other controls
property sheet
rich edit control
static control
status bar
toolbar
treeview control
Data
database
miscellaneous
Frameworks
ui & printing frameworks
Graphics & Multimedia
bitmaps & palettes
directx
gdi
multimedia
opengl
Internet & Networking
ie programming
internet protocols
isapi
network protocols
Miscellaneous
miscellaneous
samples
Visual Studio
debugging
add-ins & macros
editor tips
Windows Programming
ce
clipboard
dll
file & folder
help systems
printing
win32
system
Windows & Dialogs
console
dialog
docking window
doc/view
splitter

[Internet Jobs]
internet.commerce
Be a Commerce Partner
Internet Jobs
Shop For Computers
Find a Consultant
Reach IT Professionals
IT Training
Register Domains
Submit Your Site
Promote Your Website
Website Monitoring
Business Search


Compare products, prices, and stores at Hardware Central!

Computers
Desktops, Mac & PC Notebooks, Monitors, Scanners, Webcams, PDA's, more...

Software
Creativity Applications, Programming Tools, Internet & Communication Applications, more...

Electronics
Digital Cameras & Accessories, GPS devices & Accessories, Camcorders, MP3 Players & Accessories, more...

Get the best price on Microsoft Visual Studio .NET Professional Edition or search for other development tools





   

Sample of a Binary Tree Search


This article was contributed by Jose Garcia Garcia.

Environment: All C++ environments

The purpose of this article is show a method for binary tree searching with large numbers of strings.

I wrote a function that works with a list of pointers to alphabetically ordered strings. It can be a list of people, a dictionary, and so forth.

The function takes four arguments. The first is a pointer to the string we want to search. The next is a pointer to the first char pointer of some big list of char pointers. The next argument is the low subindex of the char pointers array and the last argument is the high subindex of the char pointers array.

The last two arguments allow the users to specify a subset of the char pointer's array to search the desired string. The function supposes that the array of char pointers is alphabetically ordered; this means that the first char pointer points to some string, the next char pointer points to some major string, and so on, from minor alphabetically to major alphabetically.

#define MAX_SRCH_LEVELS 32

/* BinaryTreeSearchEx
 *      Searches a character string in a list of alphabetic strings
 *      Searching is made within the limits of FirsItem to LastItem
 *      using a binary tree searching method.
 *
 * Arguments:
 *      Desired     a pointer to the string we want to find
 *      PtrList     a pointer to the first char pointer of the list
 *      FirstItem   the low subindex limit of PtrList array of
 *                  pointers
 *      LastItem    the high subindex limit of PtrList array of
 *                  pointers
 *
 *      The function supposes that pointers in PtrList point to
 *          alphabetically ordered strings
 *      FirstItem and LastItem allow the user to find in a subset
 *          of PtrList array of pointers
 *      The lowest index value is 0; that is, the first pointer
 *          of PtrList
 *      The highest index value is (4 294 967 294 - 1)
 *
 * Methods:
 *      Searching by binary tree
 *      Maximum number of parsings allowed are 32
 *
 * Returns:
 *      When found returns index value
 *      When not found returns 0xFFFFFFFF
 */
DWORD BinaryTreeSearchEx(char *Desired, char **PtrList,
                         DWORD FirstItem, DWORD LastItem)
{
  DWORD LowIndex = FirstItem;
  DWORD HighIndex = LastItem;
  DWORD SrchIndex;
  DWORD Amplitude;
  int cmpValue;
  int count;

  // binary tree search loop
  for (count = 0; count < MAX_SRCH_LEVELS; count++)
  {
    // Calculates the amplitude of this search level
    Amplitude = (HighIndex - LowIndex) + 1;
    if (Amplitude > 0)
    {
      SrchIndex = LowIndex + (Amplitude / 2);
    }
    else
    {
      SrchIndex = LowIndex;
    }

    // equal in length and all characters
    if ((cmpValue = strcmp(Desired, PtrList[SrchIndex])) == 0)
                           return (SrchIndex);

    // desired string is equal to the beginning of source string
    if (strstr(PtrList[SrchIndex], Desired) == PtrList[SrchIndex])
                           return (SrchIndex);

    // when HighIndex is equal to LowIndex, we must break the loop
    if (HighIndex == LowIndex) break;

    // if minor, set new limits
    if (cmpValue < 0)
    {
      if (SrchIndex > 0)
      {
        HighIndex = SrchIndex - 1;
      }
      else
      {
        HighIndex = 0;
      }
    }

    // if major, set new limits
    if (cmpValue > 0)
    {
      if (SrchIndex < LastItem)
      {
        LowIndex = SrchIndex + 1;
      }
      else
      {
        LowIndex = LastItem;
      }
    }

  // Next search level
  }

  // Function returns without success
  return (0xFFFFFFFF);
}

History

Date Posted: December 3, 2002

Comments:

Add Comment


Compare products, prices, and stores at Hardware Central!

Computers
Desktops, Mac & PC Notebooks, Monitors, Scanners, Webcams, PDA's, more...

Software
Creativity Applications, Programming Tools, Internet & Communication Applications, more...

Electronics
Digital Cameras & Accessories, GPS devices & Accessories, Camcorders, MP3 Players & Accessories, more...
Get the best price on Microsoft Visual Studio .NET Professional Edition or search for other development tools








Copyright 2003 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.
Advertise on EarthWeb
http://www.earthweb.com/