unsigned long long treeSumRange(node **root, unsigned long long left_bound, unsigned long long right_bound) { unsigned long long result; node *left, *right; result = 0; left = right = 0; if (empty(*root) || left_bound > right_bound) return result; treeSplit(root, &left, left_bound); treeSplit(&left, &right, right_bound); result += getSum(left); if (empty(left) && getValue(right) < left_bound) { return result; } if (getValue(left) < left_bound) { result -= getValue(left); } if (getValue(right) <= right_bound) { result += getValue(right); } treeMerge(&left, &right); treeMerge(root, &left); return result; }
void treeSplit(t_Node* ptTree, double* pdDepth, double dSplit, t_Node **aptSplit,int* pnSplit) { double dOldDepth = *pdDepth; if(*pdDepth > dSplit || ptTree->nId != INTERNAL){ aptSplit[*pnSplit] = ptTree; (*pnSplit)++; return; } else{ (*pdDepth) = dOldDepth + ptTree->dLeft; treeSplit(ptTree->ptLeft, pdDepth, dSplit, aptSplit,pnSplit); (*pdDepth) = dOldDepth + ptTree->dRight; treeSplit(ptTree->ptRight, pdDepth, dSplit, aptSplit,pnSplit); } }
void writeList(FILE* ofp, t_Node *ptTree, double dMaxSplit) { double dSplitDepth = 0.0, dSplit = 0.0, dDepth = 0.0, dMaxDepth = 0.0; t_Node* aptSplit[ptTree->nN]; int i = 0, j = 0, nCount = 0, nSplit = 0, nLeaves = 0; int *anLeaves = NULL; maxDepth(ptTree, &dMaxDepth); while(dSplit < dMaxSplit){ dSplitDepth = dMaxDepth - dSplit; nSplit = 0; dDepth = 0.0; treeSplit(ptTree, &dDepth, dSplitDepth, aptSplit, &nSplit); fprintf(ofp,"%f %d ",dSplit, nSplit); for(i = 0; i < nSplit; i++){ nLeaves = 0; countLeaves(aptSplit[i],&nLeaves); if(anLeaves != NULL){ free(anLeaves); anLeaves = NULL; } anLeaves = (int *) malloc(sizeof(int)*nLeaves); nCount = 0; getLeavesR(aptSplit[i], anLeaves, &nCount); for(j = 0; j < nLeaves - 1; j++){ fprintf(ofp, "%d,",anLeaves[j]); } fprintf(ofp,"%d",anLeaves[nLeaves - 1]); if(i < nSplit - 1){ fprintf(ofp, " "); } } fprintf(ofp, "\n"); dSplit += 0.01; } }