Exemplo n.º 1
0
Arquivo: mtree.c Projeto: taysom/tau
int insert (tree_s *tree, u32 key, void *rec, unint size)
{
    void	*node;
    FN;
    node = grow_tree(tree, size);
    return insert_node(tree, node, key, rec, size);
}
Exemplo n.º 2
0
Arquivo: btree.c Projeto: taysom/tau
int insert (tree_s *tree, u64 key, void *rec, unint len)
{
	void	*root;
FN;
	root = grow_tree(tree, len);
	return insert_head(tree, root, key, rec, len);
}
Exemplo n.º 3
0
int tau_insert (tree_s *tree, u64 key, void *rec, unint size)
{
	void	*child;
FN;
	child = grow_tree(tree, size);
	return insert_node(tree, child, key, rec, size);
}
Exemplo n.º 4
0
//TODO: Maybe change this header to just have points and split to x and y here??
kdtree* kdtree_build(point2D *xpoints, point2D *ypoints, rect2D region, int n) {
  kdtree* tree = kdtree_init();
  tree->root = grow_tree(NULL, xpoints, ypoints, region, n);

  return tree;
}
Exemplo n.º 5
0
/* Given an array of points, builds the kd tree recursively */
treeNode *grow_tree(treeNode *parent, point2D *xpoints, point2D *ypoints, rect2D region, int n) {
  //Base Case: No points left
  if(n == 0) {
    return NULL;
  }

  //if there ARE points left, create a node
  treeNode* node = malloc(sizeof(treeNode));
  assert(node);

  //Base case: only one point left
  if(n == 1) {
    node->p = xpoints[0];
    node->region = region;
    node->type = point;
    node->parent = parent;

    node->left = NULL;
    node->right = NULL;
  }

  //Recursive part
  else {
    // both horiz and vert nodes need this info
    int mid = (n-1)/2; //INDEX of median value
    int lsize = mid + 1; //SIZE of left point set

    node->region = region;
    node->parent = parent;

    //four arrays for splitting into left and right recursive calls
    point2D *xleft = malloc(n * sizeof(point2D));
    point2D *xright = malloc(n * sizeof(point2D));
    point2D *yleft = malloc(n * sizeof(point2D));
    point2D *yright = malloc(n * sizeof(point2D));

    // horiz and vert nodes have different info, need two if statements
    if(parent == NULL || parent->type == horiz) { //make VERT node
      node->p = xpoints[mid];
      node->type = vert;

      //split x point set into left and right sets
      splitpoints(xpoints, xleft, xright, n, mid);

      //build the two y sets based on the x split point
      distribute_y_points(ypoints, yleft, yright, n, mid, xpoints[mid]);

      rect2D l_reg = buildregion(region.top, region.bot, region.left, xpoints[mid].x);
      rect2D r_reg = buildregion(region.top, region.bot, xpoints[mid].x, region.right);

      node->left = grow_tree(node, xleft, yleft, l_reg, lsize);
      node->right = grow_tree(node, xright, yright, r_reg, n-lsize);
    }
    else if(parent->type == vert) { //make HORIZ node
      node->p = ypoints[mid];
      node->type = horiz;

      //split y point set into left and right sets
      splitpoints(ypoints, yleft, yright, n, mid);

      //build the two x sets based on the y split point
      distribute_x_points(xpoints, xleft, xright, n, mid, ypoints[mid]);

      rect2D l_reg = buildregion(ypoints[mid].y, region.bot, region.left, region.right);
      rect2D r_reg = buildregion(region.top, ypoints[mid].y, region.left, region.right);

      node->left = grow_tree(node, xleft, yleft, l_reg, lsize);
      node->right = grow_tree(node, xright, yright, r_reg, n-lsize);
    }

    // free the four arrays
    free(xleft);
    xleft = NULL;
    free(xright);
    xright = NULL;
    free(xright);
    xright = NULL;
    free(yright);
    yright = NULL;
  }//End else (end recursive section)

  return node;
}