Insertion in binary search tree using recursion in vb.net

Vb program for Insertion in binary search tree using recursion. Here problem description and other solutions.

' Include namespace system
Imports System 
'  Vb.net program for
'  Insertion in binary search tree by using recursion

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 Sub New()
        Me.root = Nothing
    End Sub
    '  Insert a node element
    Public Function  addNode(ByVal node As TreeNode, 
                             ByVal data As Integer) As TreeNode
        if (node IsNot Nothing) Then
            if (node.data >= data) Then
                '  When new element is smaller or
                '  equal to current node
                node.left = Me.addNode(node.left, data)
            Else
                '  When new element is higher to current node
                node.right = Me.addNode(node.right, data)
            End IF
            '  important to manage root node
            Return  node
        Else
            Return  New TreeNode(data)
        End IF
    End Function
    '  Display preorder
    Public Sub preorder(ByVal node As TreeNode)
        if (node IsNot Nothing) Then
            '  Display node value
            Console.Write("  " + node.data.ToString())
            '  Visit to left subtree
            Me.preorder(node.left)
            '  Visit to right subtree
            Me.preorder(node.right)
        End If
    End Sub
    Public Sub inorder(ByVal node As TreeNode)
        if (node IsNot Nothing) Then
            '  Visit to left subtree
            Me.inorder(node.left)
            '  Display node value
            Console.Write("  " + node.data.ToString())
            '  Visit to right subtree
            Me.inorder(node.right)
        End If
    End Sub
    Public Sub postorder(ByVal node As TreeNode)
        if (node IsNot Nothing) Then
            '  Visit to left subtree
            Me.postorder(node.left)
            '  Visit to right subtree
            Me.postorder(node.right)
            '  Display node value
            Console.Write("  " + node.data.ToString())
        End If
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim tree As BinarySearchTree = New BinarySearchTree()
        '         10
        '        / \
        '       /   \
        '      4     15
        '     / \   /
        '    3   5 12
        '    -------------
        '    Build binary search tree
        tree.root = tree.addNode(tree.root, 10)
        tree.addNode(tree.root, 4)
        tree.addNode(tree.root, 3)
        tree.addNode(tree.root, 5)
        tree.addNode(tree.root, 15)
        tree.addNode(tree.root, 12)
        '  Display tree nodes
        Console.WriteLine("Preorder ")
        tree.preorder(tree.root)
        Console.WriteLine( vbLf &"Inorder ")
        tree.inorder(tree.root)
        Console.WriteLine( vbLf &"Postorder ")
        tree.postorder(tree.root)
    End Sub
End Class

Output

Preorder
  10  4  3  5  15  12
Inorder
  3  4  5  10  12  15
Postorder
  3  5  4  12  15  10


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