Вышло как-то так
using System.Collections.Generic;
namespace ConsoleApp2
{
public class SvTreeNode
{
public readonly string Name;
protected SvTreeNode(string nameOfNode)
{
Name = nameOfNode;
}
}
public class Track : SvTreeNode
{
public readonly int DurationInSecond;
public Track(string name, int durationInSecond) : base(name)
{
DurationInSecond = durationInSecond;
}
}
public class Folder : SvTreeNode
{
public readonly List<SvTreeNode> ListOfFileInFolder = new List<SvTreeNode>();
public Folder(string name) : base(name){}
}
}
////
using System;
using System.Threading;
namespace ConsoleApp2
{
public class SvTree
{
private readonly Folder _root;
private int _count;
public SvTree(string nameOfRoot)
{
_root = new Folder(nameOfRoot);
_count++;
}
private SvTreeNode Search(string name)
{
return SearchTo(name, _root);
}
private SvTreeNode SearchTo(string name, Folder folder)
{
if (name == _root.Name) return _root;
foreach (var file in folder.ListOfFileInFolder)
{
if (file.Name == name) return file;
if (file is Folder tempFile)
SearchTo(name, tempFile);
}
return null;
}
private static void InsertTo(Folder folder, SvTreeNode node)
{
folder.ListOfFileInFolder.Add(node);
}
public void InsertFolder(string nameOfNewFolder, string nameOfFolder)
{
var insertValue = new Folder(nameOfNewFolder);
_count++;
if (Search(nameOfFolder) is Folder currentFolder)
InsertTo(currentFolder, insertValue);
}
public void InsertMusicFile(string nameOfMusicFile, int durationInSec, string nameOfFolder)
{
var insertValue = new Track(nameOfMusicFile, durationInSec);
_count++;
if (Search(nameOfFolder) is Folder currentFolder)
InsertTo(currentFolder, insertValue);
}
}
}
///
using System;
namespace ConsoleApp2
{
static class Program
{
private static void Main()
{
const string nameOfRootFolder = "Музыка";
var music = new SvTree(nameOfRootFolder);
//Альбом Convolk "AntiHero"
music.InsertFolder("ANTIHERO", nameOfRootFolder);
music.InsertMusicFile("convolk - swear to god",129, "ANTIHERO");
music.InsertMusicFile("convolk - black sheap",164, "ANTIHERO");
music.InsertMusicFile("convolk - medicine",156, "ANTIHERO");
//Альбом Незабудки "1999"
music.InsertFolder("1999", nameOfRootFolder);
music.InsertMusicFile("НЕЗАБУДКИ - Бегство",177, "1999");
music.InsertMusicFile("convolk - Балкон",183, "1999");
music.InsertMusicFile("convolk - Лето",160, "1999");
Console.WriteLine();
}
}
}
left,right- а с сильно ветвящимся - количество дочерних узлов заранее неизвестно :) – Grundy Jun 13 '21 at 10:02