示例#1
0
文件: tmap.c 项目: wfei/hw
NODE* findCeil(NODE *r, NODE *parent, double max) {
    NODE *result;
    if (r == NULL) {
	return parent;
    }
    if (max >= r->nameVal.value) {
	result = findCeil(r->right, r, max);
	return result;
    } else {
	return parent;
    }
}
示例#2
0
文件: tmap.c 项目: wfei/hw
/**
* returns an array populated with all entries in the given range.
*   number of such entries is reported through the integer pointer
*   parameter m (so the caller knows the length of the array).
*
* Runtime requirement is "output dependent" in that it depends on
*   the map size AND the number of elements in the range (m).
*
* Runtime:  O(m + log n)
*
*   only minimal partial credit for a linear time solution.
*/
NAME_VAL * tmap_extract_range(TMAP *t, double min, double max, int *m){
    NODE *floor, *ceil;
    floor = findFloor((*t)->root, NULL, min);
    ceil = findCeil((*t)->root, NULL, max);
    *m = (*t)->size - floor->numOfLeft - ceil->numOfRight;
    NAME_VAL *arr = (NAME_VAL *)malloc(*m * sizeof(NAME_VAL));
    NODE *floorLeftTree = floor->left, *ceilRightTree = ceil->right;
    floor->left = NULL;
    ceil->right = NULL;
    int index = 0;
    bst_inorder_NAMEVAL((*t)->root, arr, &index);
    floor->left=floorLeftTree;
    ceil->right = ceilRightTree;
    return arr;
}
bool next_permutaion(char *str,int n){
	bool flag=false;
	//find last position where a character is smaller than its immidiate next character
	int pos=-1,i;
	for(i=0;i<n-1;i++){
		if(str[i]<str[i+1]){
			flag=true;
			pos=i;
		}
	}
	if(pos==-1)
		return flag;
	int next=findCeil(str,pos+1,n-1,str[pos]);
	swap(str,next,pos);
	qsort(str+pos+1,n-pos-1,sizeof(char),cmp);
	return flag;
}
示例#4
0
void sortedPermutations(char str[]){
    int size = strlen(str);
    qsort(str, size, sizeof(str[0]), compare);
    int isFinished = 0;
    while(!isFinished){
        printf("%s\n", str);
        int i;
        for(i = size - 2; i >= 0; --i){
            if(str[i] < str[i+1])
                break;
        }
        if(i == -1) isFinished = 1;
        else {
            int ceilIndex = findCeil(str, str[i], i+1, size - 1);
            swap(&str[i], &str[ceilIndex]);
            qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare);
        }
    }
}
示例#5
0
// Print all permutations of str in sorted order
void sortedPermutations(char str[])
{
    // Get size of string
    int size = strlen(str);
 
    // Sort the string in increasing order
    qsort(str, size, sizeof( str[0] ), compare);
 
    // Print permutations one by one
    bool isFinished = false;
    while (!isFinished)
    {
        // print this permutation
        static int x = 1;
        printf("%d  %s \n", x++, str);
 
        // Find the rightmost character which is smaller than its next
        // character. Let us call it 'first char'
        int i;
        for (i = size - 2; i >= 0; --i)
            if (str[i] < str[i+1])
                break;
 
        // If there is no such chracter, all are sorted in decreasing order,
        // means we just printed the last permutation and we are done.
        if (i == -1)
            isFinished = true;
        else
        {
            // Find the ceil of 'first char' in right of first character.
            // Ceil of a character is the smallest character greater than it
            int ceilIndex = findCeil(str, str[i], i + 1, size - 1);
 
            // Swap first and second characters
            swap(&str[i], &str[ceilIndex]);
 
            // Sort the string on right of 'first char'
            qsort(str + i + 1, size - i - 1, sizeof(str[0]), compare);
        }
    }
}
示例#6
0
文件: tmap.c 项目: wfei/hw
/**
* returns the number of entries in the map with values
*   between min and max inclusive -- i.e., the number of
*   entries where min <= val <= max
*
* Runtime:  O(log n)
*
*   only minimal partial credit for a linear time solution.
*/
int tmap_range_size(TMAP *t, double min, double max){
    NODE *floor, *ceil;
    floor = findFloor((*t)->root, NULL, min);
    ceil = findCeil((*t)->root, NULL, max);
    return (*t)->size - floor->numOfLeft - ceil->numOfRight;
}