Our Tree Structure : Tree Yours « Data Structure « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » Data Structure » Tree YoursScreenshots 
Our Tree Structure
Our Tree Structure

Imports System

Public Class MainClass
  Public Shared Sub Main()
      Dim tree As Tree = New Tree()
      Dim insertValue As Integer
      Dim As Integer

      Dim randomNumber As Random = New Random()
      For i = 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 
           
       
Related examples in the same category
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.