title: c# QuadTree yielding properly
body: <p>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.</p>
  
  <p>Could you direct me how to better improve the performance of the following code?</p>
  
  <p>QuadTree</p>
  
  <pre><code>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);
      }
  </code></pre>
  
  <p>QuadTreeNode</p>
  
  <pre><code> 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);
              };
          }
      }
  </code></pre>