wannajmi
asked on
Binary Tree
Hi,
Does anyone had a VB code that can read a binary tree and manipulate it (edit, delete and so on...)
Does anyone had a VB code that can read a binary tree and manipulate it (edit, delete and so on...)
http://www.freevbcode.com/ShowCode.Asp?ID=1602
http://www.vbwm.com/art_2001/avltree08/ is a nice balanced binary tree...
or here is a heap implementation:
Option Explicit
Private Type HeapItem
Key As Variant
Data As Variant
End Type
Dim Items() As HeapItem
Dim lSize As Long ' Number of items in the heap
Public Property Get Count() As Long
Count = lSize
End Property
Private Sub RebuildHeap(ByVal Root As Long)
' Converts a semiheap rooted at index Root into a heap
' Recursively trickle the item at index Root down to
' its proper position by swapping it with its larger
' child, if that child is larger then the item.
' If the item is a leaf, nothing needs to be done
Dim ChildIndex As Long
Dim RightChildIndex As Long
' If the root is not a leaf and the root's value is less than the larger
'of the values of the root's children.
ChildIndex = 2 * Root + 1 ' Index of root's left child, if any
If ChildIndex < lSize Then
' Root is not a leaf.
' So it has a leaf child at child
RightChildIndex = ChildIndex + 1 ' Index of right child, if any.
' If root has a right child, find larger child
If (RightChildIndex < lSize) And _
(Items(RightChildIndex).Ke y > Items(ChildIndex).Key) Then
ChildIndex = RightChildIndex ' Index of larger child
End If
' If the root's value is smaller than the value
' in the larger child, swap values
If Items(Root).Key < Items(ChildIndex).Key Then
Dim Temp As HeapItem
Temp = Items(Root)
Items(Root) = Items(ChildIndex)
Items(ChildIndex) = Temp
' Transform new subtree into a heap
RebuildHeap ChildIndex
End If
End If
End Sub
' Returns and removes the
' item with the largest key in the heap.
' The largest item will be at
' the heap's root.
Public Function Remove() As Variant
'Swaps the last item in the heap with the root
'and trickles it down to its proper position.
If lSize > 0 Then
Remove = Items(0).Data
lSize = lSize - 1
Items(0) = Items(lSize)
RebuildHeap 0
ReDim Preserve Items(lSize)
Else
Remove = Empty ' Heap empty
End If
End Function
' Adds an item and the key used
' to sort the heap by.
Public Sub Add(ByVal Key As Variant, ByVal Data As Variant)
'Inserts the new item after the last item in the heap
'and trickles it up to its proper position.
'Place the new item at the end of the heap
ReDim Preserve Items(lSize) ' Make room for the new item.
Items(lSize).Key = Key
Items(lSize).Data = Data
Dim Place As Long, Parent As Long
' Trickle new item up to its proper position
Place = lSize
Parent = (Place - 1) / 2
Do While Parent >= 0 And (Items(Place).Key > Items(Parent).Key)
' swap Items(Place) and Items(Parent)
Dim Temp As HeapItem
Temp = Items(Parent)
Items(Parent) = Items(Place)
Items(Place) = Temp
Place = Parent
Parent = (Place - 1) / 2
Loop
lSize = lSize + 1
End Sub
Dim Heap As clsHeap
Set Heap = New clsHeap
With Heap
.Add 2, "Bat"
.Add 1, "Apple"
.Add 5, "Cat"
.Add 4, "Dog"
Do While .Count > 0
MsgBox .Remove
Loop
End With
or here is a heap implementation:
Option Explicit
Private Type HeapItem
Key As Variant
Data As Variant
End Type
Dim Items() As HeapItem
Dim lSize As Long ' Number of items in the heap
Public Property Get Count() As Long
Count = lSize
End Property
Private Sub RebuildHeap(ByVal Root As Long)
' Converts a semiheap rooted at index Root into a heap
' Recursively trickle the item at index Root down to
' its proper position by swapping it with its larger
' child, if that child is larger then the item.
' If the item is a leaf, nothing needs to be done
Dim ChildIndex As Long
Dim RightChildIndex As Long
' If the root is not a leaf and the root's value is less than the larger
'of the values of the root's children.
ChildIndex = 2 * Root + 1 ' Index of root's left child, if any
If ChildIndex < lSize Then
' Root is not a leaf.
' So it has a leaf child at child
RightChildIndex = ChildIndex + 1 ' Index of right child, if any.
' If root has a right child, find larger child
If (RightChildIndex < lSize) And _
(Items(RightChildIndex).Ke
ChildIndex = RightChildIndex ' Index of larger child
End If
' If the root's value is smaller than the value
' in the larger child, swap values
If Items(Root).Key < Items(ChildIndex).Key Then
Dim Temp As HeapItem
Temp = Items(Root)
Items(Root) = Items(ChildIndex)
Items(ChildIndex) = Temp
' Transform new subtree into a heap
RebuildHeap ChildIndex
End If
End If
End Sub
' Returns and removes the
' item with the largest key in the heap.
' The largest item will be at
' the heap's root.
Public Function Remove() As Variant
'Swaps the last item in the heap with the root
'and trickles it down to its proper position.
If lSize > 0 Then
Remove = Items(0).Data
lSize = lSize - 1
Items(0) = Items(lSize)
RebuildHeap 0
ReDim Preserve Items(lSize)
Else
Remove = Empty ' Heap empty
End If
End Function
' Adds an item and the key used
' to sort the heap by.
Public Sub Add(ByVal Key As Variant, ByVal Data As Variant)
'Inserts the new item after the last item in the heap
'and trickles it up to its proper position.
'Place the new item at the end of the heap
ReDim Preserve Items(lSize) ' Make room for the new item.
Items(lSize).Key = Key
Items(lSize).Data = Data
Dim Place As Long, Parent As Long
' Trickle new item up to its proper position
Place = lSize
Parent = (Place - 1) / 2
Do While Parent >= 0 And (Items(Place).Key > Items(Parent).Key)
' swap Items(Place) and Items(Parent)
Dim Temp As HeapItem
Temp = Items(Parent)
Items(Parent) = Items(Place)
Items(Place) = Temp
Place = Parent
Parent = (Place - 1) / 2
Loop
lSize = lSize + 1
End Sub
Dim Heap As clsHeap
Set Heap = New clsHeap
With Heap
.Add 2, "Bat"
.Add 1, "Apple"
.Add 5, "Cat"
.Add 4, "Dog"
Do While .Count > 0
MsgBox .Remove
Loop
End With
ASKER
I have downloaded the source code from this site but it seem not to work ? (or I don't know how to make it work)
http://www.freevbcode.com/ShowCode.Asp?ID=1602
For shaka913
Could you please tell me more on how the code that you gave should be implemented. (Should I create a form, listbox or something)
http://www.freevbcode.com/ShowCode.Asp?ID=1602
For shaka913
Could you please tell me more on how the code that you gave should be implemented. (Should I create a form, listbox or something)
The code on freevbcode doesn't work, not sure why it is up there.
as for making my code work, the last little block of code shows how to use it...
Dim Heap As clsHeap
Set Heap = New clsHeap
With Heap
.Add 2, "Bat"
.Add 1, "Apple"
.Add 5, "Cat"
.Add 4, "Dog"
Do While .Count > 0
MsgBox .Remove
Loop
End With
Here you are making a heap (a balanced btree)
and using the with command
then you are adding 4 elements
then it is looping and removing them to and displaying them all at the same time.
How are you using a binary tree?
as for making my code work, the last little block of code shows how to use it...
Dim Heap As clsHeap
Set Heap = New clsHeap
With Heap
.Add 2, "Bat"
.Add 1, "Apple"
.Add 5, "Cat"
.Add 4, "Dog"
Do While .Count > 0
MsgBox .Remove
Loop
End With
Here you are making a heap (a balanced btree)
and using the with command
then you are adding 4 elements
then it is looping and removing them to and displaying them all at the same time.
How are you using a binary tree?
ASKER
I am using a binary tree as a phylogenetic tree (evolution Tree)for my project in bioinformatics. I am not from a strong computer background, I would apreciated if you could give me a detail on how your code should be implemented in VB - thanks
ok.
I assume the information you have for this tree is coming from either user input or from a database. I will cover user input in an example that I will send to you via email tonight, please send me your email, mine is shaka913@hotmail.com
The only way to really cover this is a full blown example program. If you have a database you would rather me build the tree with, i will need you to email me that instead.
thanks
I assume the information you have for this tree is coming from either user input or from a database. I will cover user input in an example that I will send to you via email tonight, please send me your email, mine is shaka913@hotmail.com
The only way to really cover this is a full blown example program. If you have a database you would rather me build the tree with, i will need you to email me that instead.
thanks
Hey, i noticed that you have several questions about this, If I combine all of those answers into code, will you give me all of those points?
ASKER
yup, I surely will ...
:)
:)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.