Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Another Simple VB.NET Binary Tree

1.25/5 (5 votes)
24 Apr 2006CPOL1 min read 1   176  
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)