Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / Visual Basic

Another Simple VB.NET Binary Tree

Rate me:
Please Sign up or sign in to vote.
1.25/5 (5 votes)
24 Apr 2006CPOL1 min read 25.3K   174   6  
A simple binary tree with type constraints and a base class.

Introduction

This is a simple binary tree class that I wrote while writing a PDA program. It should work on any platform supporting the .NET Framework, and should be simple to port to any other .NET language. To use it, simply have your classes inherit from BinaryBase and then you can add your class to the tree. Be sure to only use one type of object per tree, or it will throw an exception.

For example:

VB
Public Class MyClass Inherits BinaryBase

    Private m_ID as integer

    Public Overrides ReadOnly Property ID() As Integer
        Get
            Return m_ID
        End Get
    End Property
End Class

...

dim theObject as New MyClass
dim theTree as New BinaryTree(theObject)

Now you can add as many new MyClass objects as you want to the tree by calling AddData:

VB
dim theOtherObject as New MyClass
theTree.AddData(theOtherObject)

The tree uses the inherited ID property to put the objects into the proper order and to find items, so be sure that ID is always unique; for example, the primary key in a SQL table. Also, the tree does type checking to be sure you don't mismatch items since that would mean the possibility of two different IDs which could mess things up, so be sure to use different trees for different types.

If you have any questions or comments, please feel free to contact me. I am busy a lot of the time, but I will try to get back to you as soon as possible ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.ruskin.com
United States United States
PC Programmer/Analyst

Comments and Discussions

 
-- There are no messages in this forum --