Binary Search


 
Noviway > Source Code > Binary Search

Download source code

Today I will show you how to search data using the binary tree algorithm.
It may not be the fastest algorithm you've ever seen but it's so easy to use and better than using .NET's simple structures.

First we need to figure out how we sort items in our tree.
It doesn't matter if the data type is a string or a number as long as we decide how to sort items.
To do so, we create a class derived from IComparer. We add a 'Compare' function which returns 0 if the first item is equal to the second one, 1 if greater and -1 if smaller.

Create the binary search object which will accept the comparer class you created before.

BinaryIndex index = new BinaryIndex( new TestComparer(), true );

Add several items to this object, in our case it's a number:

index.Add( 10 );
index.Add( 2 );
index.Add( 21 );


To search the tree just call the Search method which will return an ArrayList object of the results:

ArrayList results = index.Search( 4 );

 


NAVIGATION MENU
 
Statistics
TCP/IP Sniffer ( Basic )
Thread Pooling
HTTP Browsing
Winsock - Client & Server
Web Crawler
Binary Search
HTML To XML Converter
 
LATEST NEWS & EVENTS
 
Jul 01, 2009
Statistics - Bug fix. The project has also been converted to VS 2008.
 
May 28, 2009
Still working on the layout. Hag Sameah...
 
SPONSERED LINKS
 
Goldberg & Co - Management consulting be CEOs