Exemplo n.º 1
0
    // This function assumes that San and You are not orbiting each other.
    public int FindDistance(string a, string b)
    {
        if (!allItemsHaveDepth)
        {
            GetOrbitsCount();
        }

        int        counter = 0;
        ObjectNode aNode   = GetObject(a, false).GetParent();
        ObjectNode bNode   = GetObject(b, false).GetParent();

        // step upwards on one side until both objects are at the same depth.
        while (aNode.Depth < bNode.Depth)
        {
            counter++;
            bNode = bNode.GetParent();
        }

        while (aNode.Depth > bNode.Depth)
        {
            counter++;
            aNode = aNode.GetParent();
        }

        // Continue Comparing parents until they share the same parent.
        while (aNode.Name != bNode.Name)
        {
            counter += 2;
            aNode    = aNode.GetParent();
            bNode    = bNode.GetParent();
        }

        return(counter);
    }
Exemplo n.º 2
0
    private void InitRoot()
    {
        ObjectNode tmp = objects[1];

        while (tmp.GetParent() != null)
        {
            tmp = tmp.GetParent();
        }

        treeRoot       = tmp;
        treeRoot.Depth = 0;
    }
Exemplo n.º 3
0
    // After this call, all orbits should have a depth.
    public int GetOrbitsCount()
    {
        int counter             = 0;
        List <ObjectNode> queue = new List <ObjectNode>();

        queue.Add(treeRoot);  // initial node

        while (queue.Count != 0)
        {
            // Add Child nodes to the queue
            ObjectNode tmp = queue[0];
            queue.RemoveAt(0);
            foreach (ObjectNode node in tmp.GetChildren())
            {
                queue.Add(node);
            }

            // Get depth from parent ~ pseudo recursively.
            if (tmp.Depth == -1)
            {
                tmp.Depth = tmp.GetParent().Depth + 1;
            }
            counter += tmp.Depth;
        }

        return(counter);
    }