Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Agreed -- I would love to see an example of a binary tree.

As a matter of fact, I was struggling with Stack Overflow problems when constructing huge kd-trees recursively. I read this article a week ago, but failed to translate the pure recursive formulation of constructing a tree into a tail-recursive one -- since the recursion goes two ways instead of one

Here's an example from Wikipedia:

  function kdtree (list of points pointList, int depth)
    {
    // Select axis based on depth so that axis cycles through all valid values
    var int axis := depth mod k;

    // Sort point list and choose median as pivot element
    select median by axis from pointList;
        
    // Create node and construct subtrees
    var tree_node node;
    node.location := median;
    node.leftChild := kdtree(points in pointList before median, depth+1);
    node.rightChild := kdtree(points in pointList after median, depth+1);
    return node;
}


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: