Skip to main content

Check for continuous binary tree in vb.net

Vb program for Check for continuous binary tree. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Check if binary tree is continuous tree
'  Using recursion

'  Binary Tree Node
Public Class TreeNode
    Public  data As Integer
    Public  left As TreeNode
    Public  right As TreeNode
    Public Sub New(ByVal data As Integer)
        '  Set node value
        Me.data = data
        Me.left = Nothing
        Me.right = Nothing
    End Sub
End Class

public Class BinaryTree
    Public  root As TreeNode
    Public Sub New()
        '  Set initial tree root
        Me.root = Nothing
    End Sub
    Public Function  absValue(ByVal num As Integer) As Integer
        if (num < 0) Then
            Return  -num
        End If
        Return  num
    End Function
    '  Check tree is continuous or not
    Public Function  isContinuous(ByVal node As TreeNode) As Boolean
        if (node IsNot Nothing) Then
            if ((node.left IsNot Nothing AndAlso 
                 Me.absValue(node.data - node.left.data) <> 1) OrElse 
            (node.right IsNot Nothing AndAlso 
                Me.absValue(node.data - node.right.data) <> 1)) Then
                '  Case
                '  When fail continuous tree rule
                Return  False
            End If
            if (Me.isContinuous(node.left) AndAlso 
                Me.isContinuous(node.right)) Then
                Return  True
            End If
            '  When node value is not satisfied
            '   continuous tree properties
            Return  False
        End If
        Return  True
    End Function
    Public Shared Sub Main(ByVal args As String())
        '  Create new tree
        Dim tree As BinaryTree = New BinaryTree()
        '   Binary Tree
        '   ------------
        '       5
        '     /   \
        '    4     4
        '   /     /  \
        '  3     5    3
        '   \
        '    2
        '  Add tree node
        tree.root = New TreeNode(5)
        tree.root.left = New TreeNode(4)
        tree.root.right = New TreeNode(4)
        tree.root.left.left = New TreeNode(3)
        tree.root.right.right = New TreeNode(3)
        tree.root.right.left = New TreeNode(5)
        tree.root.left.left.right = New TreeNode(2)
        if (tree.isContinuous(tree.root) = True) Then
            Console.WriteLine("Continuous Tree ")
        Else
            Console.WriteLine("Not Continuous Tree ")
        End IF
        '  Case 2
        '       5
        '     /   \
        '    4     4
        '   /     /  \
        '  3     5    3
        '   \
        '    1  <--- change value
        tree.root.left.left.right.data = 1
        if (tree.isContinuous(tree.root) = True) Then
            Console.WriteLine("Continuous Tree ")
        Else
            Console.WriteLine("Not Continuous Tree ")
        End IF
    End Sub
End Class

Output

Continuous Tree
Not Continuous Tree




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