Example #1
0
int get_successors(char *key, int k, char *result[]) {
    if (k<1) {
		printf("k should be positive not -1");
	    return -1;
    }

	int i, j, len=0;
	PAGENO PageNo = treesearch_page(ROOT, key);

	assert(PageNo > 0);

	struct PageHdr *PagePtr = FetchPage(PageNo);
	struct KeyRecord *KeyRcPtr = PagePtr->KeyListPtr;

	if (result == NULL) {
		result = (char **)malloc(sizeof(char) * 10 * k);

		for (i=0; i<k; i++)
    		result[i] = (char *)malloc(sizeof(char) * 10);
	}

	while (strcmp(key, KeyRcPtr->StoredKey)>0) {
		KeyRcPtr = KeyRcPtr->Next;
	}
	if (strcmp(key, KeyRcPtr->StoredKey)==0)
		KeyRcPtr = KeyRcPtr->Next; 
	
	for (i=0; i<k && PagePtr!=NULL; i++) {
		if (KeyRcPtr == NULL) {
			PagePtr = FetchPage(PagePtr->PgNumOfNxtLfPg);
			if (PagePtr == NULL)
				break;
			KeyRcPtr = PagePtr->KeyListPtr;
		}
		result[i] = (char *)KeyRcPtr->StoredKey;
		KeyRcPtr = KeyRcPtr->Next;
		len++;
	}
	/* sort the result array */
	for (i=0; i<len; i++) {
		for (j = i+1; j<len; j++) {
			if (CompareKeys(result[i], result[j])==2) {
				char *tmp = result[i];
				result[i] = result[j];
				result[j] = tmp;
			}
		}
	}

	printf("found %d successors:\n", len);

	for (i=0; len>0; len--, i++) {
		printf("%s\n",result[i]);
	}

	free(result);

    return 0;
}
POSTINGSPTR treesearch_page_buildLL(int PageNo, char *key, struct node *root) { // ROOT, word
    POSTINGSPTR  result;
    struct PageHdr *PagePtr = FetchPage(PageNo);
    if (PagePtr != NULL) {
        root = (struct node *) malloc(sizeof(struct node *));
        root->value = PagePtr->PgNum;
        root->next = head;
        head = root;
    }
    if (IsLeaf(PagePtr)) { /* found leaf */
        result = PageNo;
        head = root;
    } else if ((IsNonLeaf(PagePtr)) && (PagePtr->NumKeys == 0)) {
        /* keys, if any, will be stored in Page# 2
           THESE PIECE OF CODE SHOULD GO soon! **/
        result = treesearch_page_buildLL(FIRSTLEAFPG, key, root);
    } else if ((IsNonLeaf(PagePtr)) && (PagePtr->NumKeys > 0)) {
        PAGENO ChildPage = FindPageNumOfChild(PagePtr, PagePtr->KeyListPtr, key,
                PagePtr->NumKeys);
        result = treesearch_page_buildLL(ChildPage, key, root);
    } else {
        assert(0 && "this should never happen");
    }
    FreePage(PagePtr);
    return result;
}
Example #3
0
PrintTreeInOrder(PAGENO pgNum, int level) {
    struct PageHdr *p;
    struct KeyRecord *k;
    struct PageHdr   *FetchPage();   // add this line to avoid seg-fault
    int i;

     p = (struct PageHdr * ) FetchPage(pgNum);

    if(IsNonLeaf(p)) {
        for(k=p->KeyListPtr; k != NULL; k = k->Next) {
            PrintTreeInOrder(k->PgNum,level+1);
            for(i=0; i < level*15; i++) {
                printf(" ");
            }
            printf("%s \n",k->StoredKey);
        }
        PrintTreeInOrder(p->PtrToFinalRtgPg,level+1);
    } else {
        int count=0;
        for(k=p->KeyListPtr; k != NULL; k = k->Next) {
            count++;
            if(k == p->KeyListPtr) {
                for(i=0; i < level*15; i++) {
                    printf(" ");
                }
                printf("%s ",k->StoredKey);
            } else
                if(k->Next == NULL) {
                    printf("... %s ",k->StoredKey);
                }
        }
        printf("(%d) \n",count);
    }
}
/**
* find the posting pointer to which the key should reside, given the
* starting page number to look at.
*
* to search the whole tree, pass in ROOT as the page number.
*/
POSTINGSPTR treesearch(PAGENO PageNo, char *key) {
    /* recursive call to find page number */
    const PAGENO page = treesearch_page(PageNo, key);
    /* from page number we traverse the leaf page */
    struct PageHdr *PagePtr = FetchPage(page);
    POSTINGSPTR result = searchLeaf(PagePtr, key, 1);
    FreePage(PagePtr);
    return result;
}
/**
* recursive call to find the page in which the key should reside
* and return the page number (guaranteed to be a leaf page).
*/
PAGENO treesearch_page(PAGENO PageNo, char *key) { // ROOT, word
    PAGENO result;
    struct PageHdr *PagePtr = FetchPage(PageNo);
    /*if (PagePtr != NULL) {
        printf("\ntree search - pgnum:%ld", PagePtr->PgNum);
    }*/
    if (IsLeaf(PagePtr)) { /* found leaf */
        result = PageNo;
    } else if ((IsNonLeaf(PagePtr)) && (PagePtr->NumKeys == 0)) {
        /* keys, if any, will be stored in Page# 2
           THESE PIECE OF CODE SHOULD GO soon! **/
        result = treesearch_page(FIRSTLEAFPG, key);
    } else if ((IsNonLeaf(PagePtr)) && (PagePtr->NumKeys > 0)) {
        PAGENO ChildPage = FindPageNumOfChild(PagePtr, PagePtr->KeyListPtr, key,
                PagePtr->NumKeys);
        result = treesearch_page(ChildPage, key);
    } else {
        assert(0 && "this should never happen");
    }
    FreePage(PagePtr);
    return result;
}
Example #6
0
int get_successors(char *key, int k, char *result[]) {
   
   	struct KeyRecord *KeyListTraverser;
   	int i;

    PAGENO pg = treesearch_page(ROOT, key);

    // fetch the first page
    struct PageHdr *PagePtr = FetchPage(pg);
    btReadCount ++;
    // get the key list
	KeyListTraverser = PagePtr->KeyListPtr;

	//find the key in keylist, store the pointer

	while(KeyListTraverser != NULL) {

		if (strcmp(KeyListTraverser->StoredKey, key) == 0)
		{	
            KeyListTraverser = KeyListTraverser->Next;
			break;	
		}
        else if (strcmp(KeyListTraverser->StoredKey, key) > 0)
        {
            break;
        }

		KeyListTraverser = KeyListTraverser->Next;
	}

	// then iterate each page and keyList to print the keys

    int count = 0;

    

    while(count < k) {
    	while(KeyListTraverser != NULL && count < k) {
    		
    		strcpy(result[count], KeyListTraverser->StoredKey);
    		//result[count] = KeyListTraverser->StoredKey;
    		//printKey(KeyListTraverser);
    		count ++;

    		KeyListTraverser = KeyListTraverser->Next;
    	}
    	if (count < k && PagePtr->PgNumOfNxtLfPg != NULLPAGENO)
    	{
    		FreePage(PagePtr);
    		btReadCount++;
    		PagePtr = FetchPage(PagePtr->PgNumOfNxtLfPg);
            //printf("the number of keys in this node: %d\n", PagePtr->NumKeys);
    		KeyListTraverser = PagePtr->KeyListPtr;
    		
    	}

    }

    printf("found %d successors:\n", count);

    for (i = 0; i < count; ++i)
    {	
    	printf("%s\n", result[i]);
    }


    FreePage(PagePtr);

   	

    return 0;
}
int dfs(char *key, char* result[],PAGENO pgnum,int need, int k){
	if(need<=0) return 0;

	struct PageHdr *PagePtr=FetchPage(pgnum);
	char *Word; /* Key stored in B-Tree */
	struct KeyRecord* KeyListTraverser=PagePtr->KeyListPtr;
	/* put keyrecord into stack*/

	int cmp_result;
	int NumKeys=PagePtr->NumKeys;
	int count=0;


	/*stack is only used for nonleaf pages*/
	Stack* stack;

	if (IsLeaf(PagePtr)){
		while (KeyListTraverser != NULL) {
			        if(CompareKeys(KeyListTraverser->StoredKey,key)==0){
			        	exist=1;
			        	break;
			        }
			        /*write this string into result. start from an empty place and, if get more string than needed, replace the oldest one*/
			        strcpy(result[count%need+k-need],KeyListTraverser->StoredKey);
			        count++;

			        KeyListTraverser = KeyListTraverser->Next;
		}

		FreePage(PagePtr);

        if( exist==0) return k;

		if(count>need) return need;
		else return count;

	}else{
		/*Use stack to do backtrack and keep searching predecessor leaf pages*/
		stack=init_stack(NumKeys+1);

		while( NumKeys>0){
			Word=KeyListTraverser->StoredKey;
			(*(Word + KeyListTraverser->KeyLen)) = '\0';

			cmp_result=CompareKeys(key, Word);
			NumKeys--;

			if(cmp_result<2){

				/*put PAGENO into stack*/
				push(stack,KeyListTraverser->PgNum);

				break;
			}

			/* put PAGENO into stack*/
			push(stack,KeyListTraverser->PgNum);

			KeyListTraverser = KeyListTraverser->Next;

		}

        /* Because PgNum means the left side(remember for B+ tree Child node is on left side of one key record) 
            child page no. This case means there are more beyond these left childs, which is the rightmost 
            child of the parent */
		if(cmp_result==2 && NumKeys==0){

			push(stack,PagePtr->PtrToFinalRtgPg);

		}

		while(stack->top>0){
			PAGENO tovisit= *(pop(stack));

			count+= dfs(key,result,tovisit,need-count,k);


			if(count>=need) return need;
		}
		free_stack(stack);
		FreePage(PagePtr);

		return count;
	}

}