Solved

Binary Trees in VB

Posted on 1998-09-01
5
468 Views
Last Modified: 2008-02-01
Simple question, hard asnwer.
How do you create, organise and run bianry trees in visual basic if you have no pointers availble to you?
0
Comment
Question by:Nniol
5 Comments
 
LVL 14

Expert Comment

by:waty
Comment Utility
You could use following classes :

'  Used with the clsLinkedList class.

Private m_next As class_ListItem
Private m_prev As class_ListItem
Private m_data As Variant

Public Property Set ItemData(v As Variant)
   Set m_data = v
End Property

Public Property Get NextItem() As class_ListItem
    Set NextItem = m_next
End Property

Public Property Set NextItem(ITem As class_ListItem)
    Set m_next = ITem
End Property

Public Property Get PrevItem() As class_ListItem
    Set PrevItem = m_prev
End Property

Public Property Set PrevItem(ITem As class_ListItem)
    Set m_prev = ITem
End Property

Public Property Get ItemData() As Variant
    If (VarType(m_data) = vbObject) Then
        Set ItemData = m_data
    Else
        ItemData = m_data
    End If
End Property

Public Property Let ItemData(v As Variant)
    If (VarType(v) = vbObject) Then
        Set m_data = v
    Else
        m_data = v
    End If
End Property


Option Explicit

'
'  A simple doublely linked list class
'

Private m_head As class_ListItem
Private m_tail As class_ListItem
Private m_cur  As class_ListItem

Private m_count As Long
'
'  Adds ItemData to the head of the linked list
'
Public Sub AddFirst(ItemData As Variant)
    Dim ITem As New class_ListItem

    If VarType(ItemData) = vbObject Then
       Set ITem.ItemData = ItemData
    Else
       ITem.ItemData = ItemData
    End If

    If (m_head Is Nothing) Then
        Set m_head = ITem
        Set m_tail = ITem
        Set m_cur = ITem
    Else
        Set ITem.NextItem = m_head
        Set ITem.PrevItem = Nothing
        Set ITem.NextItem.PrevItem = ITem
        Set m_head = ITem
    End If
   
    m_count = m_count + 1
End Sub

'
'  Adds ItemData to the end of the linked list.
'
Public Sub AddLast(ItemData As Variant)
    Dim ITem As New class_ListItem

    If VarType(ItemData) = vbObject Then
       Set ITem.ItemData = ItemData
    Else
       ITem.ItemData = ItemData
    End If

    If (m_tail Is Nothing) Then
        Set m_head = ITem
        Set m_tail = ITem
        Set m_cur = ITem
    Else
        Set ITem.PrevItem = m_tail
        Set ITem.NextItem = Nothing
        Set ITem.PrevItem.NextItem = ITem
        Set m_tail = ITem
    End If
   
    m_count = m_count + 1
End Sub

'
'  Returns the number of items in the linked list.
'
Property Get count() As Long
   count = m_count
End Property

'
'  Returns the current item.
'
Property Get CurrentItem() As Variant
   If m_cur Is Nothing Then
      CurrentItem = Null
   Else
      If VarType(m_cur.ItemData) = vbObject Then
         Set CurrentItem = m_cur.ItemData
      Else
         CurrentItem = m_cur.ItemData
      End If
   End If
End Property

'
'  Sets the current item.
'
Property Let CurrentItem(ItemData As Variant)
   If Not m_cur Is Nothing Then
      m_cur.ItemData = ItemData
   End If
End Property
Property Set CurrentItem(ItemData As Variant)
   If Not m_cur Is Nothing Then
      Set m_cur.ItemData = ItemData
   End If
End Property
'
'  Inserts ItemData after the current item in the list.
'
Public Sub InsertAfter(ItemData As Variant)
    Dim ITem As New class_ListItem

    If VarType(ItemData) = vbObject Then
       Set ITem.ItemData = ItemData
    Else
       ITem.ItemData = ItemData
    End If

    If (m_cur Is Nothing) Then
        Set m_head = ITem
        Set m_tail = ITem
        Set m_cur = ITem
    Else
        Set ITem.NextItem = m_cur.NextItem
        Set ITem.PrevItem = m_cur
        Set m_cur.NextItem = ITem
        'Add the following line.
        Set m_cur.NextItem.PrevItem = ITem
       
        If (m_cur.NextItem Is Nothing) Then
            Set m_tail = m_cur
        End If
    End If
   
    m_count = m_count + 1
End Sub

'
'  Delete's the
'
Public Sub DeleteAll()
   Dim M As class_ListItem
   Dim m2 As class_ListItem
   
   M = m_head
   
   Do While Not (M Is Nothing)
      Set m2 = M.NextItem
      Set M.NextItem = Nothing
      Set M.PrevItem = Nothing
      Set M = m2
   Loop
   
   m_head = Nothing
   m_tail = Nothing
   m_cur = Nothing
   m_count = 0
End Sub
   
Public Sub DeleteCurrent()
    Dim tmp As class_ListItem

    If (m_cur Is Nothing) Then
        Exit Sub
    End If

    If (m_cur.PrevItem Is Nothing) Then
        '
        ' Delete head of list
        '
        Set m_head = m_cur.NextItem
        If (m_head Is Nothing) Then
            '
            ' Also deleting tail, list becomes empty
            '
            Set m_tail = Nothing
            Set m_cur = Nothing
        Else
            Set m_head.PrevItem = Nothing
            Set m_cur = m_head
        End If
    ElseIf (m_cur.NextItem Is Nothing) Then
        '
        ' Deleting end of list
        '
        Set m_tail = m_cur.PrevItem
        If (m_tail Is Nothing) Then
            '
            ' Also deleting head, list becomes empty
            '
            Set m_head = Nothing
            Set m_cur = Nothing
        Else
            Set m_cur = m_tail
            Set m_cur.NextItem = Nothing
        End If
    Else
        '
        ' Delete somewhere inside of list
        '
        Set tmp = m_cur.NextItem
        Set m_cur.PrevItem.NextItem = m_cur.NextItem
        Set m_cur.NextItem.PrevItem = m_cur.PrevItem
        Set m_cur = tmp
    End If
   
    m_count = m_count - 1
End Sub
'
'  Return's the first item in the list.
'
Public Function FirstItem() As Variant
    If (m_head Is Nothing) Then
        FirstItem = Null
    Else
        If (VarType(m_head.ItemData) = vbObject) Then
            Set FirstItem = m_head.ItemData
        Else
            FirstItem = m_head.ItemData
        End If
        Set m_cur = m_head
    End If
End Function


'
'  Returns the next item in the list.
'
Public Function NextItem() As Variant
    If (m_cur Is Nothing) Then
        NextItem = Null
        Debug.Print "First Null"
    Else
        If (m_cur Is Nothing) Then
            NextItem = Null
        Else
            Set m_cur = m_cur.NextItem
            If (VarType(m_cur.ItemData) = vbObject) Then
                Set NextItem = m_cur.ItemData
            Else
                NextItem = m_cur.ItemData
            End If
        End If
    End If
End Function

'
' Returns the last item in the list.
'
Public Function LastItem() As Variant
    If (m_tail Is Nothing) Then
        LastItem = Null
    Else
        Set m_cur = m_tail
        If (VarType(m_cur.ItemData) = vbObject) Then
            Set LastItem = m_cur.ItemData
        Else
            LastItem = m_cur.ItemData
        End If
    End If
End Function

'
'  Returns the previous item in the list.
'
Public Function PrevItem() As Variant
    If (m_cur Is Nothing) Then
        PrevItem = Null
    Else
        If (m_cur.PrevItem Is Nothing) Then
            PrevItem = Null
        Else
            Set m_cur = m_cur.PrevItem
            If (VarType(m_cur.ItemData) = vbObject) Then
                Set PrevItem = m_cur.ItemData
            Else
                PrevItem = m_cur.ItemData
            End If
        End If
    End If
End Function


0
 
LVL 4

Expert Comment

by:mcix
Comment Utility
If you really want to know...  Buy this book.

Title: Visual Basic Algorithms
       by Kenneth Stephens
ISBN:  0471242683
0
 
LVL 6

Expert Comment

by:anthonyc
Comment Utility
VB Does allow "pointers".   Not like in C, where you can add to them to move your memory pointer up, but an object reference is considered a pointer to an object.  Unless you are using Orbix or Corba, but that's another story for another day.

Waty's solution , after a quick browse looks good.  I have a activeX dll that implements a binary tree if you would like it.  It also allows you to produce an AVL tree (balanced tree).  This was all done in VB5


0
 

Author Comment

by:Nniol
Comment Utility
for anthonyc,

is it possibnle to have this activeX dll which implements binary trees.


Nniol
0
 
LVL 2

Accepted Solution

by:
AllenC_Jr earned 200 total points
Comment Utility
I Am not really sure what you ment but here is how to save & load a tree view from a binary file
I think Though I am way off...
Type BinaryNode
      NodeKey as String
      NodeText as String
      Expanded as Boolean
      Selected as Boolean
      ParentKey as String
End Type
Type BinaryTreeView
Nodes() as BinaryNode
ToolTipText as String
End Type
Private Sub SaveTreeView(Filename$, TreeView as TreeView)
Dim BTView as BinaryTreeView
Redim BTView.Nodes(1 to Treeview.Nodes.Count)
For i = 1 to TreeView.Nodes.Count
BTView.Nodes(I).Key = TreeView.Nodes(I).Key
BTView.Nodes(I).Text = TreeView.Nodes(I).Text
BTView.Nodes(I).Selected = TreeView.Nodes(I).Selected
BTView.Nodes(I).Expanded = TreeView.Nodes(I).Expanded
If not TreeView.Nodes(I).Parent is nothing then
BTView.Nodes(I).ParentKey = TreeView.Nodes(I).Parent.Key
end if
Next
BTView.ToolTipText = TreeView.ToolTipText
Open Filename For Binary As #1
Put #1, 1, BTView
Close #1
End Sub

Private Sub LoadTreeView(Filename$, TreeView as TreeView)
Dim BTView as BinaryTreeView
Open Filename For Binary As #1
Get #1, 1, BTView
Close #1
For i = 1 to UBound(BTView.Nodes)
      If BTView.Nodes(I).ParentKey = "" then
            TreeView.Nodes.Add , , BTView.Nodes(I).Key, BTView.Nodes(I).Text
      Else
            TreeView.Nodes.Add BTView.Nodes(I).ParentKey, tvwChild, BTView.Nodes(I).Key, BTView.Nodes(I).Text
      End if
Next
End Sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA color chart bars 12 64
Excel object stays open 19 64
Excel VBA combine two working workbooks 8 37
Adding to a VBA? 6 46
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now