QuadTree recreation on c#

I am trying to code Quad Tree on C# but I am not getting any success due to lack in my coding experience!
Any help would be appreciated.
Thanks!
Script:

quadtree wip.gh (7.0 KB)

Hi, I was further fixing silly mistakes. and made few corrections. But there are still few errors which I am not able to understand.
Please find attached the revised script below:

quadtree wip.gh (8.4 KB)

Hi @Alca,

I’ve moved your discussion to the Grasshopper Developer category. In your case, it’s probably easier and quicker to get help there.

See attached (Entry level recursion on similar matters) .

NOTE: If all res == 2 we have Oct recursive divisions [ and if zMax == zMin we have Quad divisions and flat boxes]. But why bother with limits like these?

TIP: When doing recursion use as many public/static vars as possible in order to avoid the locomotive effect (Method with 666 variables etc etc).

Recursion_OctTree_V1.gh (122.2 KB)

Hi Perter!
Thank you for your reply. I’m looking for 2d Quad tree. I think the one you shared is Octree which is 3 dimensional.
Excuse me if I am wrong.

As I said set the zMin == zMax and see what happens.

So IF these 2 are equal and resX, resY == 2 you have the thing that you are after (But NEVER when coding trap yourself in any kind of restriction(s) - within the rational limits, that is).

BTW: This is “almost” 2 times faster (hope dies last):

Recursion_OctTree_V1B.gh (123.9 KB)

BTW: In fact you can evolve the recursion into a 100+ times faster one by monitoring the pts content of the previous Box (i.e. using a Tree of pts indices). That way each child Box searches pt containment with regard only these “parent” points. This makes a huge difference in elapsed time. So … take the challenge and mastermind a way to do it.

Here’s your target goal: 1 to 2 (in a slow I5) milliseconds for 150 points and 7-8 loops. Using a proper i9 K you would be bellow the magic 1 millisecond mark.