473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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.Length 0 Then
Return True
Else
Return False
End If
End Function

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

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

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

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class

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

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

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("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.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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 BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(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******@hotmail.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
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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.Length 0 Then
Return True
Else
Return False
End If
End Function

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

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

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

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub

End Class
Dec 28 '06 #2
Doh!

Clicked send too soon. I had the trees backwards:

Dim bstLedenLijst As New BinarySearchTree(Of Persoon)
Dim bstLidnummer As New BinarySearchTree(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******@hotmail.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
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
**************************************************
Class BST
**************************************************
Public Class BinarySearchTree
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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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.Length 0 Then
Return True
Else
Return False
End If
End Function

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

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

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

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
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 BinarySearchTree(Of T) is
type safe and performant. performant in that the data will not be boxed. (in
the case of BinarySearchTree(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.netwrote in message
news:eI**************@TK2MSFTNGP04.phx.gbl...
BenCoo,
Interesting homework assignment; I miss those days...

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

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("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.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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 BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(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******@hotmail.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
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************************************************* *
Class BST
************************************************* *
Public Class BinarySearchTree
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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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.Length 0 Then
Return True
Else
Return False
End If
End Function

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

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

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

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
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.txtZoek.Text, "")

Console.WriteLine(bstLedenLijst.Search(zoekPersoon ))

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

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

bstLidnummer.Add("5")
txtOutput = bstLidnummer.Search("3") & vbCrLf

bstLedenLijst.Add(New Persoon("Nicole", "Kidman"))

txtOutput &= bstLedenLijst.Search("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.Search. I would recommend you change
BinarySearchTree to a Generic class; changing this obscure runtime error
into an obvious compile time error!

Something like:

Public Class BinarySearchTree(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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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 BinarySearchTree(Of String)
Dim bstLidnummer As New BinarySearchTree(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******@hotmail.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
BinarySearchTree"
it works fine.

Thanks for any help on this,

Benny

My BST-code lookes lokes this :
************************************************* *
Class BST
************************************************* *
Public Class BinarySearchTree
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.Add(data)
End If
Else
If Me.mRightNode Is Nothing Then
Me.mRightNode = New TreeNode(data)
Else
Me.mRightNode.Add(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.Length 0 Then
Return True
Else
Return False
End If
End Function

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

Public Overloads Function CompareTo(ByVal obj As Object) As Integer
Implements System.IComparable.CompareTo
Return Me.mVnaam.CompareTo(CType(obj, Persoon).Vnaam)
End Function
End Class
**********************************
The Form :
**********************************
Public Class frmBinarySearchTree

Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExecute.Click
Dim bstLedenLijst As New BinarySearchTree
Dim bstLidnummer As New BinarySearchTree
Dim txtOutput As String = ""

bstLidnummer.Add("5")
bstLidnummer.Add("2")
bstLidnummer.Add("1")
bstLidnummer.Add("4")
bstLidnummer.Add("3")
txtOutput = bstLidnummer.Search("3") & vbCrLf

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

txtOutput &= bstLedenLijst.Search("Demi") ' & vbCrLf

Me.txtOutput.Text = txtOutput

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Application.Exit()
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
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
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...
4
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,...
4
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 ...
15
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...
1
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...
4
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...
11
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.