Skip to main content

Sum of alternate leaf nodes in bst in vb.net

Sum of alternate leaf nodes

Vb program for Sum of alternate leaf nodes in bst. Here more solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Sum of alternate leaf nodes in bst
Public Class TreeNode
    Public  data As Integer
    Public  left As TreeNode
    Public  right As TreeNode
    Public Sub New(ByVal data As Integer)
        Me.data = data
        Me.left = Nothing
        Me.right = Nothing
    End Sub
End Class

public Class BinarySearchTree
    Public  root As TreeNode
    Public  alternate As Boolean
    Public Sub New()
        Me.root = Nothing
        Me.alternate = False
    End Sub
    '  Insert a new node element
    Public Sub addNode(ByVal data As Integer)
        '  Create a new node
        Dim node As TreeNode = New TreeNode(data)
        if (Me.root  Is  Nothing) Then
            '  When add first node in bst
            Me.root = node
        Else
            Dim find As TreeNode = Me.root
            '  Add new node to proper position
            while (find IsNot Nothing)
                if (find.data >= data) Then
                    if (find.left  Is  Nothing) Then
                        '  When left child empty
                        '  So add new node here
                        find.left = node
                        Return
                    Else
                        '  Otherwise
                        '  Visit to left sub-tree
                        find = find.left
                    End IF
                Else
                    if (find.right  Is  Nothing) Then
                        '  When right child empty
                        '  So add new node here.
                        find.right = node
                        Return
                    Else
                        '  Visit to right sub-tree
                        find = find.right
                    End IF
                End IF
            End While
        End IF
    End Sub
    Public Function  leafSum(ByVal node As TreeNode) As Integer
        if (node IsNot Nothing) Then
            if (node.left  Is  Nothing AndAlso 
                node.right  Is  Nothing) Then
                '  Case A
                '  When node is leaf node.
                '  Change status.
                Me.alternate = Not Me.alternate
                '  Check node is alternate or not.
                if (Me.alternate) Then
                    '  When get alternate node.
                    Return  node.data
                End If
            Else
                '  Case B
                '  When node is internal
                '  Visit left and right subtree and
                '  Find alternate node.
                Return  Me.leafSum(node.left) + 
                  Me.leafSum(node.right)
            End IF
        End If
        Return  0
    End Function
    Public Function  alternateLeafSum() As Integer
        '  Reset alternate leaf node status
        Me.alternate = False
        Return  Me.leafSum(Me.root)
    End Function
    Public Shared Sub Main(ByVal args As String())
        Dim tree As BinarySearchTree = New BinarySearchTree()
        ' 	Binary search tree
        '    -------------------
        '       5
        '      /  \  
        '     /    \ 
        '    /      \
        '   3        19
        '  / \     /   \
        ' 2   4   8     31
        '       / \    / \
        '      7   15 25  50  
        '  Add tree node
        tree.addNode(5)
        tree.addNode(3)
        tree.addNode(19)
        tree.addNode(2)
        tree.addNode(4)
        tree.addNode(8)
        tree.addNode(31)
        tree.addNode(7)
        tree.addNode(25)
        tree.addNode(15)
        tree.addNode(50)
        '  Test
        Console.WriteLine(tree.alternateLeafSum())
    End Sub
End Class

Output

34




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment