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

VB.Net
1. 2D
2. Application
3. Class
4. Data Structure
5. Database ADO.net
6. Development
7. Event
8. File Directory
9. Generics
10. GUI
11. Language Basics
12. Network Remote
13. Thread
14. Windows System
15. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net Tutorial
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
w__w__w__.j_a___v_a_2s.__c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.