Reverse anti clockwise spiral traversal of a binary tree in vb.net

Vb program for Reverse anti clockwise spiral traversal of a binary tree. Here more solutions.

``````' Include namespace system
Imports System
'   Vb.net program for
'   Reversal anti clockwise spiral
'   Traversal of a binary tree

'  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

'  Define Binary Tree
public Class BinaryTree
Public  root As TreeNode
Public Sub New()
Me.root = Nothing
End Sub
'  Find height of given binary tree
Public Function  treeHeight(ByVal node As TreeNode) As Integer
if (node IsNot Nothing) Then
Dim l As Integer = Me.treeHeight(node.left) + 1
Dim r As Integer = Me.treeHeight(node.right) + 1
if (l > r) Then
Return  l
Else
Return  r
End IF
Else
Return  0
End IF
End Function
'  Display given level from left to right
Public Sub printLeftToRight(ByVal node As TreeNode,
ByVal level As Integer)
if (node  Is  Nothing) Then
Return
End If
if (level = 1) Then
'  print level node
Console.Write("  " + node.data.ToString())
Else
'  First visit left child
Me.printLeftToRight(node.left, level - 1)
Me.printLeftToRight(node.right, level - 1)
End IF
End Sub
'  Display given level from right to left
Public Sub printRightToLeft(ByVal node As TreeNode,
ByVal level As Integer)
if (node  Is  Nothing) Then
Return
End If
if (level = 1) Then
'  print level node
Console.Write("  " + node.data.ToString())
Else
'  First visit right child
Me.printRightToLeft(node.right, level - 1)
Me.printRightToLeft(node.left, level - 1)
End IF
End Sub
'  Handles the request of printing reverse
'  anti clockwise spiral of binary tree
Public Sub reversalAntiClockwise()
if (Me.root  Is  Nothing) Then
Console.WriteLine( vbLf &" Empty Tree ")
Else
'  Start level
Dim top As Integer = 1
'  Last level
Dim down As Integer = Me.treeHeight(Me.root)
'  This is level indicator (top to down selector)
Dim selection As Boolean = True
'  When the top level less than or equal to down level
while (top <= down)
if (selection = False) Then
'  right to left
Me.printRightToLeft(Me.root, top)
'  Increase level
top += 1
'  Next is bottom level
selection = True
Else
'  left to right
Me.printLeftToRight(Me.root, down)
'  Decrease the level
down -= 1
'  Next is top level
selection = False
End IF
End While
End IF
End Sub
Public Shared Sub Main(ByVal args As String())
Dim tree As BinaryTree = New BinaryTree()
'         6
'        / \
'       /   \
'      4     7
'     / \     \
'    2   3     12
'       / \    /
'      10  8  5
'      /    \
'     9     -1
'    -----------------
'    Binary tree

tree.root = New TreeNode(6)
tree.root.left = New TreeNode(4)
tree.root.left.right = New TreeNode(3)
tree.root.left.right.left = New TreeNode(10)
tree.root.left.right.left.left = New TreeNode(9)
tree.root.left.right.right = New TreeNode(8)
tree.root.left.right.right.right = New TreeNode(-1)
tree.root.left.left = New TreeNode(2)
tree.root.right = New TreeNode(7)
tree.root.right.right = New TreeNode(12)
tree.root.right.right.left = New TreeNode(5)
'  Test
tree.reversalAntiClockwise()
End Sub
End Class

``````

Output

``  9  -1  6  10  8  5  7  4  2  3  12``

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.