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.159 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.158 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.157 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.156 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.155 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.154 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.153 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.152 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.151 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.150 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.149 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.148 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.147 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.146 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.145 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.144 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.143 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.142 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.141 tracks/csharp/exercises/tree-building/TreeBuilding.cs
trackler-2.2.1.140 tracks/csharp/exercises/tree-building/TreeBuilding.cs