Insertion in binary search tree without recursion in vb.net

Vb program for Insertion in binary search tree without recursion. Here more information.

' Include namespace system
Imports System 
'  Vb.net program for
'  iterative insert in binary search tree

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 element
    Public Sub addNode(ByVal data As Integer)
        '  Create a new node
        Dim node As TreeNode = New TreeNode(data)
        if (Me.root  Is  Nothing) Then
            '  When adds a first node in bst
            Me.root = node
        Else
            Dim find As TreeNode = Me.root
            '  Add new node to proper position
            while (find IsNot Nothing)
                if (find.data >= data) Then
                    if (find.left  Is  Nothing) Then
                        '  When left child empty
                        '  So add new node here
                        find.left = node
                        Return
                    Else
                        '  Otherwise
                        '  Visit left sub-tree
                        find = find.left
                    End IF
                Else
                    if (find.right  Is  Nothing) Then
                        '  When right child empty
                        '  So add new node here
                        find.right = node
                        Return
                    Else
                        '  Visit right sub-tree
                        find = find.right
                    End IF
                End IF
            End While
        End IF
    End Sub
    '  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.addNode(10)
        tree.addNode(4)
        tree.addNode(3)
        tree.addNode(5)
        tree.addNode(15)
        tree.addNode(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