Inorder traversal of binary tree with recursion in vb.net

Vb program for Inorder traversal of binary tree with recursion. Here problem description and explanation.

' Include namespace system
Imports System 
'  Vb.net Program for
'  inorder tree traversal of a Binary 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
    '  Display Inorder view of binary tree
    Public Sub inorder(ByVal node As TreeNode)
        if (node IsNot Nothing) Then 
            '  Visit left subtree
            Me.inorder(node.left)
            ' Print node value
            Console.Write("  " + node.data.ToString())
            '  Visit right subtree
            Me.inorder(node.right)
        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
        '    ----------------
        '        15
        '       /  \
        '      24   54
        '     /    /  \
        '    35   62   13
        '  Add tree TreeNode
        tree.root = New TreeNode(15)
        tree.root.left = New TreeNode(24)
        tree.root.right = New TreeNode(54)
        tree.root.right.right = New TreeNode(13)
        tree.root.right.left = New TreeNode(62)
        tree.root.left.left = New TreeNode(35)
        '  Display Tree Node
        tree.inorder(tree.root)
    
    End Sub

End Class

Output

  35  24  15  62  54  13


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







© 2021, kalkicode.com, All rights reserved