Find the maximum tree depth

I have a tree data structure with N first level child nodes that also have children.

For instance:

  •  
  • Root 
    •     
    • Node1    
      •         
      • Node11        
        •           
        • Node111          
          •              
          • Node1111          
                  
                
      • Node12    
    • Node2
      • Node21
        • Node211

I would like to know which of the logs has the greatest depth. As in the previous example, this will be

Node1 - Node11 - Node111 - Node1111

which has a depth of four levels.

Any suggestion?

Thank!

+5
source share
5 answers

You must check all nodes. A few months ago, I implemented this algorithm as follows:

class Node
{
    public String Name { get; private set; }
    public IList<Node> Childs { get; private set; }

    public Node(String name)
    {
        Name = name;
        Childs = new List<Node>();
    }

    public List<Node> Depth
    {
        get
        {
            List<Node> path = new List<Node>();
            foreach (Node node in Childs)
            {
                List<Node> tmp = node.Depth;
                if (tmp.Count > path.Count)
                    path = tmp;
            }
            path.Insert(0, this);
            return path;
        }
    }

    public override string ToString()
    {
        return Name;
    }
}

Test example:

Node node1111 = new Node("Node1111");
Node node111 = new Node("Node111");
Node node11 = new Node("Node11");
Node node12 = new Node("Node12");
Node node1 = new Node("Node1");
Node root = new Node("Root");
Node node2 = new Node("Node2");
Node node21 = new Node("Node21");
Node node211 = new Node("Node211");
root.Childs.Add(node1);
root.Childs.Add(node2);
node1.Childs.Add(node11);
node1.Childs.Add(node12);
node11.Childs.Add(node111);
node111.Childs.Add(node1111);
node2.Childs.Add(node21);
node21.Childs.Add(node211);

List<Node> path = root.Depth;
foreach (Node n in path)
    Console.Write(String.Format("{0} - ", n.ToString()));

Console.WriteLine();

Node node2111 = new Node("Node2111");
node2111.Childs.Add(new Node("Node21111"));
node211.Childs.Add(node2111);

path = root.Depth;
foreach (Node n in path)
    Console.Write(String.Format("{0} - ", n.ToString()));

Console.WriteLine();

Console output:

Root - Node1 - Node11 - Node111 - Node1111 -
Root - Node2 - Node21 - Node211 - Node2111 - Node21111 -
+4
source

, , . , , .

0
The deepest branch from a node is:
    the longest of the respective deepest branches from each child node
    prepended with the current node.
0

- , node. node .

node. . .

0

- , .

, UNIX find, awk tr:

find / -depth | tr -dc '/\n' \
    | awk '{if (length($0) > max) { max=length($0)}}; END {print max}'

... findis an iterator, tris a data manipulation that translates one character set to another (in this case it is used for -d (delete) additions (-c) (/), so it converts any UNIX full path to only delimiters / from there.From there I find the longest line of input ... and my result.

Of course, this approach will not help you with your homework. But the concept should be clear. :)

0
source

All Articles