Codiqo

Learn C# · lesson 53 of 61 · about 11 minutes

Project: The Dragon Trap

Total up a tree of parts, where every part can be made from more parts.

What you'll learn

  • Model a tree with a class that holds a list of itself
  • Total a tree of any depth recursively
  • Print a tree with indentation

The dragon trap

At the lair, the hero will need a trap, and the blacksmith can make one. A trap is made of parts, and parts are made of smaller parts: the dragon trap needs a net, bait and a cage; the net needs rope and weights; and the bait needs a gem, because dragons can't resist one. Each part has the blacksmith's price for making it, but anything the hero already has in the bag costs nothing, and neither do the parts it's made from. ShowPart prints the tree with each part under the one it belongs to, and TotalCost adds it all up. If the gold covers it, the hero makes the trap and pays.

Data inside data

A folder holds some files and can hold other folders, which hold more folders, and so on. A Folder class with a List<Folder> of children describes that perfectly.

The size of a folder is its own files' size plus the size of each child folder. That's a recursive definition, so it's a recursive method:
- Base case: a folder with no children is just its own files
- Recursive case: add up the children's sizes too

Totalling a tree

var root = new Folder("root", 2);
var photos = new Folder("photos", 5);
photos.Children.Add(new Folder("holiday", 8));
root.Children.Add(photos);
root.Children.Add(new Folder("docs", 3));

Console.WriteLine(TotalSize(root));

static int TotalSize(Folder f)
{
    int total = f.Files;
    foreach (Folder child in f.Children) total += TotalSize(child);
    return total;
}

class Folder(string name, int files)
{
    public string Name { get; } = name;
    public int Files { get; } = files;
    public List<Folder> Children { get; } = [];
}

Output

18

Printing the tree

Pass a depth along and add 1 for each level. new string(' ', depth * 2) makes the indent, so each folder appears under its parent, like a file explorer.

Showing the shape

var root = new Folder("root");
var photos = new Folder("photos");
photos.Children.Add(new Folder("holiday"));
root.Children.Add(photos);
root.Children.Add(new Folder("docs"));

Show(root, 0);

static void Show(Folder f, int depth)
{
    Console.WriteLine(new string(' ', depth * 2) + f.Name);
    foreach (Folder child in f.Children) Show(child, depth + 1);
}

class Folder(string name)
{
    public string Name { get; } = name;
    public List<Folder> Children { get; } = [];
}

Output

root
  photos
    holiday
  docs

Two ways to stop

TotalCost stops in two ways. A part that's already in the bag costs 0, without looking at what it's made of. A part with no smaller parts, like the cage, is just its own price, because the loop over its Parts has nothing to do. Everything else is its own price plus the total of each smaller part. ShowPart stops the same way, so a part in the bag is shown as in the bag and its smaller parts aren't listed.

Tip

class Folder(string name, int files) is a primary constructor: the parameters can be used to set up the properties straight away.

Now try it yourself

This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.