Posted on by Kalkicode
Code Queue

Level order traversal using queue in

Vb program for Level order traversal using queue. Here problem description and other solutions.

' Include namespace system
Imports System 
' program for
'  Level order tree traversal using queue

'  Create Q node
Public Class QNode
    Public  data As TreeNode
    Public  [next] As QNode
     Public Sub New(ByVal node As TreeNode) = node = Nothing
    End Sub
End Class

'  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 = data
        Me.left = Nothing
        Me.right = Nothing
    End Sub
End Class

Public Class MyQueue
    Public  head As QNode
    Public  tail As QNode
    Public  count As Integer
    Public Sub New()
        Me.head = Nothing
        Me.tail = Nothing
        Me.count = 0
    End Sub
    Public Function  size() As Integer
        Return  Me.count
    End Function
    Public Function  isEmpty() As Boolean
        Return  Me.count = 0
    End Function
    '  Add new node of queue
    Public Sub enqueue(ByVal value As TreeNode)
        '  Create a new node
        Dim node As QNode = New QNode(value)
        if (Me.head  Is  Nothing) Then
            '  Add first element into queue
            Me.head = node
            '  Add node at the end using tail
            Me.tail.[next] = node
        End IF
        Me.count += 1
        Me.tail = node
    End Sub
    '  Delete a element into queue
     Public Sub dequeue()
        if (Me.head  Is  Nothing) Then
            '  Empty Queue
        End If
        '  Visit next node
        Me.head = Me.head.[next]
        Me.count -= 1
        if (Me.head  Is  Nothing) Then
            '  When deleting a last node of linked list
            Me.tail = Nothing
        End If
    End Sub
    '  Get front node
    Public Function  peek() As TreeNode
        if (Me.head  Is  Nothing) Then
            Return  Nothing
        End If
    End Function
End Class

public Class BinaryTree
    Public  root As TreeNode
    Public Sub New()
        '  Set initial tree root
        Me.root = Nothing
    End Sub
    Public Sub levelOrder()
        if (Me.root IsNot Nothing) Then
            Dim q As MyQueue = New MyQueue()
            '  Add first node
            Dim node As TreeNode = Me.root
            while (q.isEmpty() = False AndAlso node IsNot Nothing)
                if (node.left IsNot Nothing) Then
                    '  Add left child node
                End If
                if (node.right IsNot Nothing) Then
                    '  Add right child node
                End If
                '  Display level node
                Console.Write("  " +
                '  Remove current node
                '  Get new head
                node = q.peek()
            End While
            Console.WriteLine("Empty Tree")
        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
        '  -----------------------
        '         1
        '       /   \
        '      2     3
        '     /     / \
        '    4     5   6
        '   /     / \
        '  7     8   9
        '  Adding tree nodes
        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.left = New TreeNode(8)
        tree.root.right.left.right = New TreeNode(9)
        '  Display level order  element
    End Sub
End Class


  1  2  3  4  5  6  7  8  9


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