Skip to main content

Top view of binary tree using recursion in vb.net

Vb program for Top view of binary tree using recursion. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Top view of binary tree using dfs

'  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  level As Integer
    
    Public Sub New()
        '  Set the initial value of root
        Me.root = Nothing
        Me.level = 0
    End Sub
    
    '  Print left side top nodes including root
    Public Sub printleftSide(ByVal node As TreeNode, 
                             ByVal depth As Integer)
        if (node IsNot Nothing) Then 
            if (Me.level < depth) Then 
                '  Print the resultant node
                Console.Write("  " + node.data.ToString())
                Me.level = depth
            End If

            '  Recursively
            '  Visit left subtree
            Me.printleftSide(node.left, depth + 1)
            '  Visit right subtree
            Me.printleftSide(node.right, depth - 1)
        End If

    
    End Sub
    '  Print right side top nodes
    
    Public Sub printRightSide(ByVal node As TreeNode, 
                              ByVal depth As Integer)
        if (node IsNot Nothing) Then 
            if (Me.level < depth) Then 
                '  Print the resultant node
                Console.Write("  " + node.data.ToString())
                Me.level = depth
            End If

            '  Recursively
            '  First visit right subtree
            Me.printRightSide(node.right, depth + 1)
            '  Then
            '  Visit left subtree
            Me.printRightSide(node.left, depth - 1)
        End If

    
    End Sub
    '  This is handle the request of printing top view
    
    Public Sub topView()
        Me.level = -1
        Me.printleftSide(Me.root, 0)
        Me.level = 0
        Me.printRightSide(Me.root, 0)
    
    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     3
        '     /     / \
        '    4     5   6
        '   /       \    
        '  7         8    
        '             \
        '              9
        '               \
        '                10
        '  Construct binary tree
        tree.root = New TreeNode(1)
        tree.root.left = New TreeNode(2)
        tree.root.right = New TreeNode(3)
        tree.root.right.right = New TreeNode(6)
        tree.root.right.left = New TreeNode(5)
        tree.root.left.left = New TreeNode(4)
        tree.root.left.left.left = New TreeNode(7)
        tree.root.right.left.right = New TreeNode(8)
        tree.root.right.left.right.right = New TreeNode(9)
        tree.root.right.left.right.right.right = New TreeNode(10)
        '  Display top view
        tree.topView()
    
    End Sub

End Class

Output

  1  2  4  7  3  6  10




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