Sha256: 2d3dccd6779e16379a115336ab9518286e1a4283f0c528c4cf31e0a368dc5139

Contents?: true

Size: 1.59 KB

Versions: 345

Compression:

Stored size: 1.59 KB

Contents

using System;
using System.Collections.Generic;
using System.Linq;

public class TreeBuildingRecord
{
    public int ParentId { get; set; }
    public int RecordId { get; set; }
}

public class Tree
{
    public int Id { get; set; }
    public int ParentId { get; set; }

    public List<Tree> Children { get; set; }

    public bool IsLeaf => Children.Count == 0;
}

public static class TreeBuilder
{
    public static Tree BuildTree(IEnumerable<TreeBuildingRecord> records)
    {
        var ordered = new SortedList<int, TreeBuildingRecord>();

        foreach (var record in records)
        {
            ordered.Add(record.RecordId, record);
        }

        records = ordered.Values;

        var trees = new List<Tree>();
        var previousRecordId = -1;

        foreach (var record in records)
        {   
            var t = new Tree { Children = new List<Tree>(), Id = record.RecordId, ParentId = record.ParentId };
            trees.Add(t);

            if ((t.Id == 0 && t.ParentId != 0) ||
                (t.Id != 0 && t.ParentId >= t.Id) ||
                (t.Id != 0 && t.Id != previousRecordId + 1))
            {
                throw new ArgumentException();
            }

            ++previousRecordId;
        }
        
        if (trees.Count == 0)
        {
            throw new ArgumentException();
        }

        for (int i = 1; i < trees.Count; i++)
        {
            var t = trees.First(x => x.Id == i);
            var parent = trees.First(x => x.Id == t.ParentId);
            parent.Children.Add(t);
        }

        var r = trees.First(t => t.Id == 0);
        return r;
    }
}

Version data entries

345 entries across 345 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.179 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.178 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.177 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.176 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.175 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.174 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.173 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.172 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.171 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.170 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.169 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.167 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.166 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.165 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.164 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.163 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.162 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.161 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.160 tracks/csharp/exercises/tree-building/TreeBuilding.cs