SPKDTreeNode spKDTreeCreateFromArray(SPKDArray kdArr, int prevDim, SPConfig config, SP_CONFIG_MSG *msg, int* problem ){ SPKDTreeNode res = NULL; SPKDArray *children; if (spKDArrayGetNumOfFeatures(kdArr)==1){ res = spKDTreeCreateLeaf(spKDArrayGetPoint(kdArr, 0)); if(!res){ spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__); spKDArrayDestroy(kdArr, ALL_ROWS); *problem = true; return NULL; } spKDArrayDestroy(kdArr, ALL_ROWS); return res; } else{ //** KD Array is larger than 1 point **// res = spKDTreeCreateNode(); if(!res){ spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__); spKDArrayDestroy(kdArr, ALL_ROWS); *problem = true; return NULL; } res->dim = spKDArrayFindSplitDim(kdArr, prevDim, config, msg); children = spKDArraySplit(kdArr, res->dim); if(!children){ spKDTreeDestroy(res); spKDArrayDestroy(kdArr, ALL_ROWS); *problem = true; return NULL; } res->val = spKDArrayGetMedianVal(children[LEFT], res->dim); spKDArrayDestroy(kdArr, ALL_ROWS); res->left = spKDTreeCreateFromArray(children[LEFT], res->dim, config, msg, problem); if(!(res->left)){ spKDTreeDestroy(res); spKDArrayDestroy(kdArr, ALL_ROWS); spKDMultiArrayDestroy(children, true, ALL_ROWS, true, ALL_ROWS); *problem = true; return NULL; } res->right = spKDTreeCreateFromArray(children[RIGHT], res->dim, config, msg, problem); if(!(res->right)){ spKDTreeDestroy(res); spKDArrayDestroy(kdArr, ALL_ROWS); spKDMultiArrayDestroy(children, true, ALL_ROWS, true, ALL_ROWS); *problem = true; return NULL; } spKDMultiArrayDestroy(children, false, false, false, false); return res; } }
void spKDTreeDestroy(SPKDTreeNode node){ if(!node){ return; } if(node->dim == INVALID){ spPointDestroy(node->data); } if(node->left){ spKDTreeDestroy(node->left); } if(node->right){ spKDTreeDestroy(node->right); } free(node); }
void destroyCaseData(SPKDTreeNode tree,SPKDArray kdArr,SPPoint* pointsArray,int size, SPBPQueue queue,SPPoint queryPoint){ if (tree) spKDTreeDestroy(tree, false); if (kdArr) spKDArrayDestroy(kdArr); if (pointsArray) destroyPointsArray(pointsArray,size); if (queue) spBPQueueDestroy(queue); if (queryPoint) spPointDestroy(queryPoint); }
void endControlFlow(SPConfig config, SPImageData image, bool isCurrentImageFeaturesArrayAllocated, SPKDTreeNode kdTree, SPBPQueue bpq, int returnValue) { if (returnValue < 0) { printf(MAIN_RETURNED_ERROR); } printf("%s", EXITING); spConfigDestroy(config); freeImageData(image, !isCurrentImageFeaturesArrayAllocated, true); spKDTreeDestroy(kdTree, true); spBPQueueDestroy(bpq); spLoggerDestroy(); }