Node *treeMakeMinus(Node **node) { Node *tmpNode = NULL; tmpNode = treeNodeCreate(); tmpNode->_left = treeNodeCreate(); tmpNode->_right = treeCopy(node); tmpNode->_varOp = '-'; free(*node); *node = NULL; return tmpNode; }
Node *treeCopy(Node **node) { Node *tmpNode = NULL; if (*node == NULL) return NULL; tmpNode = treeNodeCreate(); tmpNode->_varOp = (*node)->_varOp; tmpNode->_num = (*node)->_num; tmpNode->_left = treeCopy(&(*node)->_left); tmpNode->_right = treeCopy(&(*node)->_right); return tmpNode; }
treeObj *msCreateTree(shapefileObj *shapefile, int maxdepth) { int i; treeObj *tree; rectObj bounds; if(!shapefile) return NULL; /* -------------------------------------------------------------------- */ /* Allocate the tree object */ /* -------------------------------------------------------------------- */ tree = (treeObj *) msSmallMalloc(sizeof(treeObj)); tree->numshapes = shapefile->numshapes; tree->maxdepth = maxdepth; /* -------------------------------------------------------------------- */ /* If no max depth was defined, try to select a reasonable one */ /* that implies approximately 8 shapes per node. */ /* -------------------------------------------------------------------- */ if( tree->maxdepth == 0 ) { int numnodes = 1; while(numnodes*4 < shapefile->numshapes) { tree->maxdepth += 1; numnodes = numnodes * 2; } } /* -------------------------------------------------------------------- */ /* Allocate the root node. */ /* -------------------------------------------------------------------- */ tree->root = treeNodeCreate(shapefile->bounds); for(i=0; i<shapefile->numshapes; i++) { if(msSHPReadBounds(shapefile->hSHP, i, &bounds) == MS_SUCCESS) treeAddShapeId(tree, i, bounds); } return tree; }
void treeBuild(Node **node, Stack *st) { Token token; if (stackEmpty(st)) return; token = stackTop(st); stackPop(st); (*node) = treeNodeCreate(); (*node)->_varOp = token._varOp; (*node)->_num = token._num; if (isOp((*node)->_varOp)) { treeBuild(&(*node)->_right, st); treeBuild(&(*node)->_left, st); } }
static int treeNodeAddShapeId( treeNodeObj *node, int id, rectObj rect, int maxdepth) { int i; /* -------------------------------------------------------------------- */ /* If there are subnodes, then consider whether this object */ /* will fit in them. */ /* -------------------------------------------------------------------- */ if( maxdepth > 1 && node->numsubnodes > 0 ) { for(i=0; i<node->numsubnodes; i++ ) { if( msRectContained(&rect, &node->subnode[i]->rect)) { return treeNodeAddShapeId( node->subnode[i], id, rect, maxdepth-1); } } } /* -------------------------------------------------------------------- */ /* Otherwise, consider creating four subnodes if could fit into */ /* them, and adding to the appropriate subnode. */ /* -------------------------------------------------------------------- */ #if MAX_SUBNODES == 4 else if( maxdepth > 1 && node->numsubnodes == 0 ) { rectObj half1, half2, quad1, quad2, quad3, quad4; treeSplitBounds(&node->rect, &half1, &half2); treeSplitBounds(&half1, &quad1, &quad2); treeSplitBounds(&half2, &quad3, &quad4); if(msRectContained(&rect, &quad1) || msRectContained(&rect, &quad2) || msRectContained(&rect, &quad3) || msRectContained(&rect, &quad4)) { node->numsubnodes = 4; node->subnode[0] = treeNodeCreate(quad1); node->subnode[1] = treeNodeCreate(quad2); node->subnode[2] = treeNodeCreate(quad3); node->subnode[3] = treeNodeCreate(quad4); /* recurse back on this node now that it has subnodes */ return(treeNodeAddShapeId(node, id, rect, maxdepth)); } } #endif /* -------------------------------------------------------------------- */ /* Otherwise, consider creating two subnodes if could fit into */ /* them, and adding to the appropriate subnode. */ /* -------------------------------------------------------------------- */ #if MAX_SUBNODE == 2 else if( maxdepth > 1 && node->numsubnodes == 0 ) { rectObj half1, half2; treeSplitBounds(&node->rect, &half1, &half2); if( msRectContained(&rect, &half1)) { node->numsubnodes = 2; node->subnode[0] = treeNodeCreate(half1); node->subnode[1] = treeNodeCreate(half2); return(treeNodeAddShapeId(node->subnode[0], id, rect, maxdepth-1)); } else if(msRectContained(&rect, &half2)) { node->numsubnodes = 2; node->subnode[0] = treeNodeCreate(&half1); node->subnode[1] = treeNodeCreate(&half2); return(treeNodeAddShapeId(node->subnode[1], id, rect, maxdepth-1)); } } #endif /* MAX_SUBNODE == 2 */ /* -------------------------------------------------------------------- */ /* If none of that worked, just add it to this nodes list. */ /* -------------------------------------------------------------------- */ node->numshapes++; node->ids = SfRealloc( node->ids, sizeof(ms_int32) * node->numshapes ); node->ids[node->numshapes-1] = id; return MS_TRUE; }