Skip to main content

Detect leaf nodes of threaded BST in

Print leaf node in Threaded BST

Vb program for Detect leaf nodes in threaded BST. Here mentioned other language solution.

' Include namespace system
Imports System 
' program for
'  Print leaf node in Threaded BST

Public Class TreeNode
    Public  data As Integer
    Public  left As TreeNode
    Public  right As TreeNode
    Public Sub New(ByVal data As Integer, 
                   ByVal left As TreeNode, 
                   ByVal right As TreeNode) = data
        Me.left = left
        Me.right = right
    End Sub
End Class
public Class BinarySearchTree
    Public  root As TreeNode
    Public Sub New()
        Me.root = Nothing
    End Sub
    '  Added node in Threaded BST
     Public Function  addNode(ByVal node As TreeNode, 
                              ByVal x As TreeNode, 
                              ByVal y As TreeNode, 
                              ByVal data As Integer) As TreeNode
        if (node IsNot Nothing) Then
            if ( >= data) Then
                if (node.left  Is  x) Then
                    node.left = New TreeNode(data, x, node)
                    node.left = Me.addNode(node.left, x, node, data)
                End IF
            ElseIf ( < data) Then
                if (node.right  Is  y) Then
                    node.right = New TreeNode(data, node, y)
                    node.right = Me.addNode(node.right, node, y, data)
                End IF
            End If
            Return  node
            Return  New TreeNode(data, x, y)
        End IF
    End Function
    '  Adding a new node in binary search tree
    Public Sub add(ByVal data As Integer)
        Me.root = Me.addNode(Me.root, Nothing, Nothing, data)
    End Sub
    '  Print all leaf node in Threaded BST
    Public Sub leafNode(ByVal node As TreeNode, 
                        ByVal x As TreeNode, 
                        ByVal y As TreeNode)
        if (node IsNot Nothing) Then
            if (node.left  Is  x AndAlso node.right  Is  y) Then
                '  This is a leaf node Then print
                Console.Write("  {0}",
            End If
            if (node.left IsNot x) Then
                '  Visit left child
                Me.leafNode(node.left, x, node)
            End If
            if (node.right IsNot y) Then
                '  Visit right child
                Me.leafNode(node.right, node, y)
            End If
        End If
    End Sub
    Public Shared Sub Main(ByVal args As String())
        Dim tree As BinarySearchTree = New BinarySearchTree()
        '  Add nodes in binary search tree
        '            5
        '           / \
        '          /   \
        '         /     \
        '        3       9
        '       / \     / \
        '      1   4   8   11
        '     / \     /      \
        '   -3   2   7        12
        '  Test
        tree.leafNode(tree.root, Nothing, Nothing)
    End Sub
End Class


  -3  2  4  7  12


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