c# QuadTree yielding properly I have implemented a QuadTree of my own, and I'm afraid I didn't use yield properly when I query my tree, my fear is that I create O(HN) iterators.

Could you direct me how to better improve the performance of the following code?

QuadTree

public IEnumerable&lt;T&gt; Query(string leafId, Circle shape)
    {
        // Gets the relevant parent by a leafId Query (this is O(lg H))
        var startNode = GetParentIntersecting(leafId, shape);

        return startNode.Query(shape);
    }


QuadTreeNode

 public IEnumerable&lt;T&gt; Query(IShape shape)
    {
        var results = new HashSet&lt;T&gt;();

        foreach (var content in _contents)
        {
            if (shape.Contains(content.GetPosition()))
            {
                //Debug.Log(content.GetPosition());
                yield return content;
                //results.Add(content);
            }
        }

        if (IsLeaf)
            yield break;

        foreach (var node in Nodes)
        {
            if (node.IsEmpty &amp;&amp; node.IsLeaf)
                continue;

            foreach (var result in node.Query(shape))
            {
                results.Add(result);
            };
        }
    }
You use yield properly, it's OK to yield from inside nested loops. But all the code behind your first foreach is kind of useless, it's not doing anything useful.

First the yield break is useless, cause there are no more yields following, there are no more return values produced no matter if IsLeaf true or false. Iteration ends anyway.

Second you fill up results with something, but the content of results is lost, then the Method exits.