|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
Wanneer de edges van de nodes van een graphstructuur niet zozeer verwijzen naar een nextnode en/of previousnode van een bepaalde lineare opeenvolging ( van een bepaalde lijst ), maar eerder verwijzen naar "childnodes" of "parentnodes" dan spreekt men over een "tree". Visual Basic 2010 Broncode Class Person Private m_Name As String Public Property Name() As String Get Name = m_Name End Get Set( ByVal value As String) m_Name = value End Set End Property Private m_Father As Person Public Property Father() As Person Get Father = m_Father End Get Set( ByVal value As Person) m_Father = value End Set End Property Private m_Mother As Person Public Property Mother() As Person Get Mother = m_Mother End Get Set( ByVal value As Person) m_Mother = value End Set End PropertyEnd ClassClass Example Public Shared Sub Main() Dim paul As Person = New Person With {.Name = "Paul"} Dim elisabeth As Person = New Person With {.Name = "Elisabeth"} Dim john As Person = New Person With {.Name = "John", _ .Mother = elisabeth} Dim ema As Person = New Person With {.Name = "Ema", _ .Father = paul} Dim george As Person = New Person With {.Name = "George", _ .Father = john, _ .Mother = ema} PrintFamily(george) Console.ReadLine() End Sub Public Shared Sub PrintFamily( ByVal person As Person, _ Optional ByVal depth As Integer = 0) Console.WriteLine(person.Name) If person.Father IsNot Nothing Then Console.Write( New String( " "c, depth + 2) & "- father : ") PrintFamily(person.Father, depth + 2) End If If person.Mother IsNot Nothing Then Console.Write( New String( " "c, depth + 2) & "- mother : ") PrintFamily(person.Mother, depth + 2) End If End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output George
- father : John
- mother : Elisabeth
- mother : Ema
- father : PaulIn de context van trees spreekt men ook wel over een "root" ( hier met deze client is dat dan george ), wat dus de "wortel" is van de "boom". En men spreekt ook wel over "leafs", dus de "bladeren" of verder vertakte nodes van de boom.
Vele variaties bestaan op tree-structuren. Variaties verschillend dan in aantal trees ( bij meerdere trees spreekt men over een "forest" ), aantal edges of in de mogelijkheid enkel op- of neerwaarts ( of beide ) door de boom te navigeren ( men spreekt over "tree-traversal" ).
In bovenstaand voorbeeld hebben we een "binary tree", waar elke node maximaal twee edges heeft. 22.10.1. OefeningOpgave :
Zorg ervoor dat we objecten kunnen creëren om bestanden en om folders voor te stellen. Alle hiervoor vermelde entries hebben een bepaalde naam. Directories kunnen bestanden en andere directories bevatten.
Creëer in uw clientcode een method om de volledige directory/entry-tree van een bepaalde entry weer te geven.
Oplossing : Visual Basic 2010 Broncode MustInherit Class Entry Protected Sub New( ByVal name As String) m_Name = name End Sub Private ReadOnly m_Name As String Public ReadOnly Property Name() As String Get Name = m_Name End Get End PropertyEnd ClassClass Directory : Inherits Entry Public Sub New( ByVal name As String) MyBase.New(name) End Sub Private m_Entries As New System.Collections. ArrayList Public Sub Add( ByVal entry As Entry) m_Entries.Add(entry) End Sub Public ReadOnly Property Entries() As System.Collections. ArrayList Get Entries = m_Entries End Get End PropertyEnd ClassClass File : Inherits Entry Public Sub New( ByVal name As String) MyBase.New(name) End SubEnd ClassClass Exercise4Solution Public Shared Sub Main() Dim rootDirectory As Directory = New Directory( "C:\") Dim directory1 As Directory = New Directory( "directory1") Dim directory2 As Directory = New Directory( "directory2") Dim directory3 As Directory = New Directory( "directory3") Dim file1 As File = New File( "file1.vb") Dim file2 As File = New File( "file2.vb") Dim file3 As File = New File( "file3.vb") Dim file4 As File = New File( "file4.vb") rootDirectory.Add(directory1) rootDirectory.Add(directory2) rootDirectory.Add(file1) directory1.Add(file2) directory1.Add(file3) directory2.Add(directory3) directory3.Add(file4) PrintEntry(rootDirectory) Console.ReadLine() End Sub Public Shared Sub PrintEntry( ByVal entry As Entry, _ Optional ByVal depth As Integer = 0) Console.WriteLine( New String( " "c, depth) & "- " & entry.Name) If TypeOf entry Is Directory Then Dim directory As Directory = DirectCast(entry, Directory) Dim directoryEntries As System.Collections. ArrayList = _ directory.Entries For Each directoryEntry As Entry In directoryEntries PrintEntry(directoryEntry, depth + 2) Next End If End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output - C:\
- directory1
- file2.vb
- file3.vb
- directory2
- directory3
- file4.vb
- file1.vb
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|