Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / Visual Basic

Treaps in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.09/5 (10 votes)
31 Aug 2004CPOL4 min read 78.3K   635   30  
A Treap implementation in Visual Basic.NET.
'/<summary>
'/ The TreapNode class encapsulates a node in the treap
'/</summary>

Imports System.Text

Namespace TreapVB

    Public Class TreapNode 

        ' key provided by the calling class
        Private Dim ordKey      As IComparable
        ' the data or value associated with the key
        Private Dim objData     As Object
        ' random priority to balance the tree
        Private Dim intPriority As Integer
        ' left node of the tree
        Private Dim tnLeft      As TreapNode
        ' right node of the tree
        Private Dim tnRight     As TreapNode

        '/<summary>
        '/Key 
        '/</summary>
        Public Property Key()   As IComparable
            Get
                Return ordKey
            End Get

            Set (ByVal Value As IComparable)
                ordKey = value
            End Set
        End Property
        '/<summary>
        '/Data
        '/</summary>
        Public Property Data()  As Object
            Get
                Return objData
            End Get

            Set (ByVal Value As Object)
                objData = value
            End Set
        End Property
        '/<summary>
        '/Priority
        '/</summary>
        Public Property Priority() As Integer
            Get
                Return intPriority
            End Get

            Set (ByVal Value As Integer)
                intPriority = value
            End Set
        End Property
        '/<summary>
        '/Left
        '/</summary>
        Public Property Left()  As TreapNode
            Get
                Return tnLeft
            End Get

            Set (ByVal Value As TreapNode)
                tnLeft = value
            End Set
        End Property
        '/<summary>
        '/ Right
        '/</summary>
        Public Property Right() As TreapNode
            Get
                Return tnRight
            End Get

            Set (ByVal Value As TreapNode)
                tnRight = value
            End Set        
        End Property
        '/<summary>
        '/ RotateLeft
        '/ Rebalance the tree by rotating the nodes to the left
        '/</summary>
        Public Function RotateLeft() As TreapNode

            Dim temp As TreapNode    = Right 
            Right                   = Right.Left
            temp.Left               = Me
     
            Return temp

        End Function
        '/<summary>
        '/ RotateRight
        '/ Rebalance the tree by rotating the nodes to the right
        '/</summary>
        Public Function RotateRight() As TreapNode

            Dim temp As TreapNode    = Left 
            Left                    = Left.Right
            temp.Right              = Me
     
            Return temp

        End Function
        '/<summary>
        '/ DeleteRoot
        '/ If one of the children is an empty subtree, remove the root and put the other
        '/ child in its place. If both children are nonempty, rotate the treapTree at
        '/ the root so that the child with the smallest priority number comes to the
        '/ top, then delete the root from the other subtee.
        '/
        '/ NOTE: This method is recursive
        '/</summary>
        Public Function DeleteRoot() As TreapNode 

            Dim temp As TreapNode
     
            If(Left Is Nothing)
                Return Right
            End If
     
            If(Right Is Nothing)
                Return Left
            End If
     
            If(Left.Priority < Right.Priority)
                temp        = RotateRight()
                temp.Right  = DeleteRoot()
            Else _
                temp        = RotateLeft()
                temp.Left   = DeleteRoot()
            End If

            Return temp

        End Function
    End Class
End NameSpace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Roy is a software developer who digs all aspects of software development, from design and architecture to implementation.

Comments and Discussions