Skip to main content

Detect next node at same level of a key in binary tree in vb.net

Next node of same level of tree

Vb program for Detect next node at same level of a key in binary tree. Here more solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Find the next node of the key at the same 
'  level in the binary tree

'  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  status As Integer
    Public  result As TreeNode
    Public Sub New()
        '  Set initial values
        Me.root = Nothing
        Me.status = -1
        Me.result = Nothing
    End Sub
    '  Method which are find next node of same level
    Public Sub findNextNode(ByVal temp As TreeNode, 
                            ByVal level As Integer, 
                            ByVal node As Integer)
        if (temp IsNot Nothing) Then
            if (Me.status = -1 AndAlso temp.data = node) Then
                '  When found node value
                Me.status = level
            ElseIf (Me.status = level AndAlso 
                    Me.result  Is  Nothing) Then
                '  Next node of same level
                Me.result = temp
            End If
            '  Visit to left subtree
            Me.findNextNode(temp.left, level + 1, node)
            '  Visit to right subtree
            Me.findNextNode(temp.right, level + 1, node)
        End If
    End Sub
    '  This are handling the request of to finding next
    '  node in same level by given node
    Public Sub nextNode(ByVal node As Integer)
        '  Reset the result indicator
        Me.status = -1
        Me.result = Nothing
        '  Find next node
        Me.findNextNode(Me.root, 0, node)
        Console.WriteLine( vbLf &" Node : " + node.ToString())
        Console.Write(" Result : ")
        '  Test Case
        if (Me.status = -1) Then
            '  Case A
            '  When node values are not exist
            Console.WriteLine("Not found")
        ElseIf (Me.result  Is  Nothing) Then
            '  Case B
            '  When next node is not found
            Console.WriteLine("None")
        Else
            '  Case C
            '  When next node are exist
            Console.WriteLine(Me.result.data)
        End IF
    End Sub
    Public Shared Sub Main(ByVal args As String())
        '  Create new tree
        Dim tree As BinaryTree = New BinaryTree()
        '   Make A Binary Tree
        '  ---------------------
        '        1
        '       / \ 
        '      /   \ 
        '     /     \
        '    2       4
        '   /       / \
        '  /       /   \
        '  3      6     5
        '   \      \   /
        '    7      8 11
        '  Add node in Binary Tree
        tree.root = New TreeNode(1)
        tree.root.left = New TreeNode(2)
        tree.root.left.left = New TreeNode(3)
        tree.root.left.left.right = New TreeNode(7)
        tree.root.right = New TreeNode(4)
        tree.root.right.right = New TreeNode(5)
        tree.root.right.right.left = New TreeNode(11)
        tree.root.right.left = New TreeNode(6)
        tree.root.right.left.right = New TreeNode(8)
        '  Testing
        tree.nextNode(3)
        tree.nextNode(7)
        tree.nextNode(6)
        tree.nextNode(5)
        tree.nextNode(8)
        tree.nextNode(1)
    End Sub
End Class

Output

 Node : 3
 Result : 6

 Node : 7
 Result : 8

 Node : 6
 Result : 5

 Node : 5
 Result : None

 Node : 8
 Result : 11

 Node : 1
 Result : None




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