473,699 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary Search Tree - CompareTo Error

Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New BinarySearchTre e"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
*************** *************** *************** *****
Class BST
*************** *************** *************** *****
Public Class BinarySearchTre e
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*************** **************
The class Person
*************** **************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam () And Me.isValidAnaam () Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Return Me.mVnaam.Compa reTo(CType(obj, Persoon).Vnaam)
End Function
End Class
*************** *************** ****
The Form :
*************** *************** ****
Public Class frmBinarySearch Tree

Private Sub btnExecute_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExecute.Clic k
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
bstLidnummer.Ad d("2")
bstLidnummer.Ad d("1")
bstLidnummer.Ad d("4")
bstLidnummer.Ad d("3")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))
bstLedenLijst.A dd(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.A dd(New Persoon("Demi", "Moore"))
bstLedenLijst.A dd(New Persoon("Julia" , "Roberts"))
bstLedenLijst.A dd(New Persoon("Andie" , "MacDowell" ))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Me.txtOutput.Te xt = txtOutput

End Sub

Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExit.Click
Application.Exi t()
End Sub

End Class

Dec 28 '06 #1
4 2169
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf
Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.S earch. I would recommend you change
BinarySearchTre e to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTre e(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
Dim bstLedenLijst As New BinarySearchTre e(Of String)
Dim bstLidnummer As New BinarySearchTre e(Of Persoon)
NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotma il.comwrote in message
news:CN******** **************@ phobos.telenet-ops.be...
Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTre e"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
*************** *************** *************** *****
Class BST
*************** *************** *************** *****
Public Class BinarySearchTre e
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*************** **************
The class Person
*************** **************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam () And Me.isValidAnaam () Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Return Me.mVnaam.Compa reTo(CType(obj, Persoon).Vnaam)
End Function
End Class
*************** *************** ****
The Form :
*************** *************** ****
Public Class frmBinarySearch Tree

Private Sub btnExecute_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExecute.Clic k
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
bstLidnummer.Ad d("2")
bstLidnummer.Ad d("1")
bstLidnummer.Ad d("4")
bstLidnummer.Ad d("3")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))
bstLedenLijst.A dd(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.A dd(New Persoon("Demi", "Moore"))
bstLedenLijst.A dd(New Persoon("Julia" , "Roberts"))
bstLedenLijst.A dd(New Persoon("Andie" , "MacDowell" ))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Me.txtOutput.Te xt = txtOutput

End Sub

Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExit.Click
Application.Exi t()
End Sub

End Class
Dec 28 '06 #2
Doh!

Clicked send too soon. I had the trees backwards:

Dim bstLedenLijst As New BinarySearchTre e(Of Persoon)
Dim bstLidnummer As New BinarySearchTre e(Of String)

Of course this demonstrated the immediate & obvious compile time error I
suggested ;-)

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotma il.comwrote in message
news:CN******** **************@ phobos.telenet-ops.be...
Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTre e"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
*************** *************** *************** *****
Class BST
*************** *************** *************** *****
Public Class BinarySearchTre e
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
*************** **************
The class Person
*************** **************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam () And Me.isValidAnaam () Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Return Me.mVnaam.Compa reTo(CType(obj, Persoon).Vnaam)
End Function
End Class
*************** *************** ****
The Form :
*************** *************** ****
Public Class frmBinarySearch Tree

Private Sub btnExecute_Clic k(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExecute.Clic k
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
bstLidnummer.Ad d("2")
bstLidnummer.Ad d("1")
bstLidnummer.Ad d("4")
bstLidnummer.Ad d("3")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))
bstLedenLijst.A dd(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.A dd(New Persoon("Demi", "Moore"))
bstLedenLijst.A dd(New Persoon("Julia" , "Roberts"))
bstLedenLijst.A dd(New Persoon("Andie" , "MacDowell" ))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Me.txtOutput.Te xt = txtOutput

End Sub

Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExit.Click
Application.Exi t()
End Sub

End Class
Dec 28 '06 #3
Reviewing this, one more change:

Public Function Search(ByVal data As T) As T
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As T

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

Note that Search returns type T; this ensures that BinarySearchTre e(Of T) is
type safe and performant. performant in that the data will not be boxed. (in
the case of BinarySearchTre e(Of Integer))

Although I would recommend defining Search as:

Public Function Search(ByVal match As Predicate(Of T)) As T

Or even:

Public Delegate Function Predicate(Of T, V)(ByVal obj As T, ByVal value
As V) As Boolean

Public Function Search(Of V)(ByVal value As V, ByVal match As
Predicate(Of T, V)) As T

Which I will leave as an exercise for you to complete...

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Jay B. Harlow" <Ja************ @tsbradley.netw rote in message
news:eI******** ******@TK2MSFTN GP04.phx.gbl...
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
> Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.S earch. I would recommend you change
BinarySearchTre e to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTre e(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
> Dim bstLedenLijst As New BinarySearchTre e(Of String)
Dim bstLidnummer As New BinarySearchTre e(Of Persoon)

NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotma il.comwrote in message
news:CN******** **************@ phobos.telenet-ops.be...
>Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTr ee"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************** *************** *************** ******
Class BST
************** *************** *************** ******
Public Class BinarySearchTre e
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
************** ***************
The class Person
************** ***************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam () And Me.isValidAnaam () Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Return Me.mVnaam.Compa reTo(CType(obj, Persoon).Vnaam)
End Function
End Class
************** *************** *****
The Form :
************** *************** *****
Public Class frmBinarySearch Tree

Private Sub btnExecute_Clic k(ByVal sender As System.Object, ByVal e As
System.EventAr gs) Handles btnExecute.Clic k
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
bstLidnummer.Ad d("2")
bstLidnummer.Ad d("1")
bstLidnummer.Ad d("4")
bstLidnummer.Ad d("3")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))
bstLedenLijst.A dd(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.A dd(New Persoon("Demi", "Moore"))
bstLedenLijst.A dd(New Persoon("Julia" , "Roberts"))
bstLedenLijst.A dd(New Persoon("Andie" , "MacDowell" ))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Me.txtOutput.Te xt = txtOutput

End Sub

Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles btnExit.Click
Application.Exi t()
End Sub

End Class
Dec 28 '06 #4
Thank you for your quick response; I solved afther the poste was made the
promlem this way ..

Dim zoekPersoon As New Persoon(Me.txtZ oek.Text, "")

Console.WriteLi ne(bstLedenLijs t.Search(zoekPe rsoon))

"Jay B. Harlow" <Ja************ @tsbradley.nets chreef in bericht
news:eI******** ******@TK2MSFTN GP04.phx.gbl...
BenCoo,
Interesting homework assignment; I miss those days...

Anyway to answer your question:
> Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Ah! There's the Rub!

bstLedenLijst contains a collection of Persoon objects, you are attempting
to compare Persoon objects to a String object! You need to pass a Persoon
object to bstLedenLijst.S earch. I would recommend you change
BinarySearchTre e to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTre e(Of T As IComparable)

Private Class TreeNode
Private mData As T
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As T)
Me.mData = data
End Sub

Public ReadOnly Property Data() As T
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As T)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As T)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As T) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As T, ByVal node As TreeNode) As
Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class

Then when you define your trees, you give the type of tree they are!
> Dim bstLedenLijst As New BinarySearchTre e(Of String)
Dim bstLidnummer As New BinarySearchTre e(Of Persoon)

NOTE: You need VS 2005 for generics.

--
Hope this helps
Jay B. Harlow
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"BenCoo" <te******@hotma il.comwrote in message
news:CN******** **************@ phobos.telenet-ops.be...
>Hello,

In a Binary Search Tree I get the error : Object must be of type String

if I run the form only with the "Dim bstLidnummer As New
BinarySearchTr ee"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************** *************** *************** ******
Class BST
************** *************** *************** ******
Public Class BinarySearchTre e
Private Class TreeNode
Private mData As IComparable
Private mLeftNode As TreeNode
Private mRightNode As TreeNode

Public Sub New(ByVal data As IComparable)
Me.mData = data
End Sub

Public ReadOnly Property Data() As IComparable
Get
Return Me.mData
End Get
End Property

Public Property LeftNode() As TreeNode
Get
Return Me.mLeftNode
End Get
Set(ByVal value As TreeNode)
Me.mLeftNode = value
End Set
End Property

Public Property RightNode() As TreeNode
Get
Return Me.mRightNode
End Get
Set(ByVal value As TreeNode)
Me.mRightNode = value
End Set
End Property

Public Sub Add(ByVal data As IComparable)
If data.CompareTo( Me.mData) <= 0 Then
If Me.mLeftNode Is Nothing Then
Me.mLeftNode = New TreeNode(data)
Else
Me.mLeftNode.Ad d(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.A dd(data)
End If
End If
End Sub
End Class

Private mRoot As TreeNode

Public Sub Add(ByVal data As IComparable)
If mRoot Is Nothing Then
mRoot = New TreeNode(data)
Else
mRoot.Add(data)
End If
End Sub

Public Function Search(ByVal data As IComparable) As Object
Return Me.Search(data, Me.mRoot)
End Function

Private Function Search(ByVal data As IComparable, ByVal node As
TreeNode) As Object

If node Is Nothing Then
Return Nothing
Else
Dim result As Integer = data.CompareTo( node.Data) '****** this
is
the error I get on this line :Object must be of type String.

If result = 0 Then
Return node.Data
ElseIf result < 0 Then
Return Me.Search(data, node.LeftNode)
Else
Return Me.Search(data, node.RightNode)
End If
End If

End Function
End Class
************** ***************
The class Person
************** ***************
Public Class Persoon
Inherits BusinessObject
Implements IComparable

Private mVnaam As String
Private mAnaam As String

Public Sub New(ByVal Vnaam As String, ByVal Anaam As String)
Me.mVnaam = Vnaam
Me.mAnaam = Anaam
End Sub

Public Property Vnaam() As String
Get
Return Me.mVnaam
End Get
Set(ByVal value As String)
mVnaam = value
End Set
End Property

Public Property Anaam() As String
Get
Return Me.mAnaam
End Get
Set(ByVal value As String)
Me.mAnaam = value
End Set
End Property

Public Overrides Function IsValid() As Boolean
If Me.isValidVnaam () And Me.isValidAnaam () Then
Return True
Else
Return False
End If
End Function

Private Function isValidVnaam() As Boolean
If Me.Vnaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Private Function isValidAnaam() As Boolean
If Me.Anaam.Trim.L ength 0 Then
Return True
Else
Return False
End If
End Function

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.ICompara ble.CompareTo
Return Me.mVnaam.Compa reTo(CType(obj, Persoon).Vnaam)
End Function
End Class
************** *************** *****
The Form :
************** *************** *****
Public Class frmBinarySearch Tree

Private Sub btnExecute_Clic k(ByVal sender As System.Object, ByVal e As
System.EventAr gs) Handles btnExecute.Clic k
Dim bstLedenLijst As New BinarySearchTre e
Dim bstLidnummer As New BinarySearchTre e
Dim txtOutput As String = ""

bstLidnummer.Ad d("5")
bstLidnummer.Ad d("2")
bstLidnummer.Ad d("1")
bstLidnummer.Ad d("4")
bstLidnummer.Ad d("3")
txtOutput = bstLidnummer.Se arch("3") & vbCrLf

bstLedenLijst.A dd(New Persoon("Nicole ", "Kidman"))
bstLedenLijst.A dd(New Persoon("Jamie-Lee", "Curtis"))
bstLedenLijst.A dd(New Persoon("Demi", "Moore"))
bstLedenLijst.A dd(New Persoon("Julia" , "Roberts"))
bstLedenLijst.A dd(New Persoon("Andie" , "MacDowell" ))

txtOutput &= bstLedenLijst.S earch("Demi") ' & vbCrLf

Me.txtOutput.Te xt = txtOutput

End Sub

Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventAr gs) Handles btnExit.Click
Application.Exi t()
End Sub

End Class

Dec 29 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2525
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
0
4249
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a random binary search tree. We've tried everything but are in the land of pointer hell. If someone could help it would be a huge help. our code follows. We've tried 2 diff methods the split() and splitter() functions #include <iostream> #include...
4
9016
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
4
4070
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
15
5090
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked lists. I'm totally new to binary searches by the way. Can anyone help me with the commented sections below? Much of the code such as functions and printfs has already been completed. Any help is greatly appreciated. Thanks,
1
2945
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template <typename Tclass Tree{ private: Tree<T*left;
4
11010
by: whitehatmiracle | last post by:
Hello I have written a program for a binary tree. On compiling one has to first chose option 1 and then delete or search. Im having some trouble with the balancing function. It seems to be going into an infinite loop, i think im messing up with the pointers. Heres the code. //press 1, first, Do not press it a second time!!!
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
7
3866
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
8703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8935
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8893
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7773
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.