?
Solved

how do you create a tree with more than 2 nodes in VB.NET

Posted on 2011-09-19
6
Medium Priority
?
256 Views
Last Modified: 2012-05-12
I need to create a tree that has 5 nodes per parent that goes down 10 levels deep.  I have seen a ton of examples for binary trees but nothing for greater than 2.  
0
Comment
Question by:monica73174
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36564371
Just create a List within the Node that holds all the child Nodes:

    Public Class MyTreeNode
        Public SomeValue As String
        Public Children As New List(Of MyTreeNode)
    End Class

Now it can hold as many children as you like...
0
 

Author Comment

by:monica73174
ID: 36564451
I am having trouble figuring out how I move from each node at each level to set the children.

For example the first level has the root and that is the parent but then the next level has 5 nodes and I need to move to each node to set the children of those nodes.  how do I track the parent of each group of children?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36564500
Where is your data coming from?

>> "how do I track the parent of each group of children?"

You keep a reference to it...

Here is an example:  A --> B --> C

    Dim root As New MyTreeNode
    root.SomeValue = "A"

    Dim child1 As New MyTreeNode
    child1.SomeValue = "B"

    Dim child2 As New MyTreeNode
    child2.SomeValue = "C"

    root.Children.Add(child1)
    child1.Children.Add(child2)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:monica73174
ID: 36564517
Basically my tree will be the same five actions repeated over and over.

This is a small sample of what it should look like.

                          Initial state - root
action1 - action2 - action3 - action4 - action5  --

then these five nodes will be repeated under each of these nodes as children and so on up to 10 levels.  .
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 36564612
Here's a new MyTreeNode:
Public Class MyTreeNode

    Public Sub New(ByVal Value As String)
        SomeValue = Value
    End Sub

    Public SomeValue As String

    Public Children As New List(Of MyTreeNode)

    Public Shared Sub Print(ByVal root As MyTreeNode)
        Debug.Print(root.SomeValue)
        For Each child As MyTreeNode In root.Children
            MyTreeNode.Print(child, 1)
        Next
    End Sub

    Private Shared Sub Print(ByVal node As MyTreeNode, ByVal Level As Integer)
        Debug.Print(New String(Chr(9), Level) & node.SomeValue)
        For Each child As MyTreeNode In node.Children
            MyTreeNode.Print(child, Level + 1)
        Next
    End Sub

End Class

Open in new window


To be used in this example:
Public Class Form1

    Private Root As MyTreeNode

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Example:
        CreateTree(3, 5) ' 3 actions, 5 levels deep
    End Sub

    Public Sub CreateTree(ByVal NumActions As Integer, ByVal NumLevels As Integer)
        Root = New MyTreeNode("Root")
        CreateNodes(Root, NumActions, NumLevels, 1)
    End Sub

    Private Sub CreateNodes(ByVal parent As MyTreeNode, ByVal NumActions As Integer, ByVal NumLevels As Integer, ByVal CurLevel As Integer)
        For i As Integer = 1 To NumActions
            Dim mtn As New MyTreeNode("Action" & i)
            parent.Children.Add(mtn)
        Next
        If CurLevel < NumLevels Then
            For Each child As MyTreeNode In parent.Children
                CreateNodes(child, NumActions, NumLevels, CurLevel + 1)
            Next
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        MyTreeNode.Print(Me.Root)
    End Sub

End Class

Open in new window



Which produced this Output:  
Root
	Action1
		Action1
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action2
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action3
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
	Action2
		Action1
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action2
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action3
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
	Action3
		Action1
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action2
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
		Action3
			Action1
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action2
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3
			Action3
				Action1
					Action1
					Action2
					Action3
				Action2
					Action1
					Action2
					Action3
				Action3
					Action1
					Action2
					Action3

Open in new window

0
 

Author Comment

by:monica73174
ID: 36564661
Thank you it looks exactly like what I am trying to do.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question