VB.Net/Data Structure/Tree Yours

Материал из VB Эксперт
Перейти к: навигация, поиск

Our Tree Structure

Imports System
Public Class MainClass
  Public Shared Sub Main()
      Dim tree As Tree = New Tree()
      Dim insertValue As Integer
      Dim i As Integer
      Dim randomNumber As Random = New Random()
      For i = 1 To 10
         insertValue = randomNumber.Next(100)
         Console.Write(insertValue & " ")
         tree.InsertNode(insertValue)
      Next
      Console.WriteLine("Preorder Traversal")
      tree.PreorderTraversal()
      Console.WriteLine("Inorder Traversal")
      tree.InorderTraversal()
      Console.WriteLine("Postorder Traversal")
      tree.PostorderTraversal()
  End Sub
End Class
Public Class TreeNode
   Private mLeftNode As TreeNode
   Private mData As Integer
   Private mRightNode As TreeNode
   Public Sub New(ByVal nodeData As Integer)
      mData = nodeData
      mRightNode = Nothing 
      LeftNode = Nothing 
   End Sub 
   Public Property LeftNode() As TreeNode
      Get
         Return mLeftNode
      End Get
      Set(ByVal value As TreeNode)
         mLeftNode = value
      End Set
   End Property 
   Public Property Data() As Integer
      Get
         Return mData
      End Get
      Set(ByVal value As Integer)
         mData = value
      End Set
   End Property 
   Public Property RightNode() As TreeNode
      Get
         Return mRightNode
      End Get
      Set(ByVal value As TreeNode)
         mRightNode = value
      End Set
   End Property 
   Public Sub Insert(ByVal insertValue As Integer)
      If insertValue < mData Then
         If mLeftNode Is Nothing Then
            LeftNode = New TreeNode(insertValue)
         Else
            LeftNode.Insert(insertValue)
         End If
      ElseIf insertValue > mData Then
         If RightNode Is Nothing Then
            RightNode = New TreeNode(insertValue)
         Else
            RightNode.Insert(insertValue)
         End If
      End If
   End Sub 
End Class 

Public Class Tree
   Private root As TreeNode
   Public Sub New()
      root = Nothing
   End Sub " New
   Public Sub InsertNode(ByVal insertValue As Integer)
      SyncLock (Me)
         If root Is Nothing Then
            root = New TreeNode(insertValue)
         Else 
            root.Insert(insertValue)
         End If
      End SyncLock
   End Sub
   Public Sub PreorderTraversal()
      SyncLock (Me)
         PreorderHelper(root)
      End SyncLock
   End Sub 
   Private Sub PreorderHelper(ByVal node As TreeNode)
      If node Is Nothing Then
         Return
      End If
      Console.Write(node.Data & " ")
      PreorderHelper(node.LeftNode)
      PreorderHelper(node.RightNode)
   End Sub 
   Public Sub InorderTraversal()
      SyncLock (Me)
         InorderHelper(root)
      End SyncLock
   End Sub 
   Private Sub InorderHelper(ByVal node As TreeNode)
      If node Is Nothing Then
         Return
      End If
      InorderHelper(node.LeftNode)
      Console.Write(node.Data & " ")
      InorderHelper(node.RightNode)
   End Sub 
   Public Sub PostorderTraversal()
      SyncLock (Me)
         PostorderHelper(root)
      End SyncLock
   End Sub 
   Private Sub PostorderHelper(ByVal node As TreeNode)
      If node Is Nothing Then
         Return
      End If
      PostorderHelper(node.LeftNode)
      PostorderHelper(node.RightNode)
      Console.Write(node.Data & " ")
   End Sub 
End Class