Esempio n. 1
0
void exploitMemLeaks() {	

	// All data allocated and tracked, all will be freed
	int * low = calloc(1, sizeof(int));
	* low = 1;
	
	int * mid = calloc(1, sizeof(int));
	* mid = 2;
	
	int * hi = calloc(1, sizeof(int));
	* hi = 3;
	
	printf("Created the following data to add to a tree:\n");
	printf("Low: %d, Mid: %d, Hi: %d\n", * low, * mid, * hi);
	
	// Test inside a tree
	Tree * test = createBinTree(&compNum, &destructor);
	
	addToTree(test, mid);
	addToTree(test, low);
	addToTree(test, hi);
	
	printInOrder(test, &print);
	// Up to here, as long as destroyBinTree is called, no leaks are possible
	// Now lets look at some of the issues with the library
	
	// Left will contain Low (1), we know this. This function allocates memory
	// so it's return value must be freed
	Tree * left = getLeftSubtree(test);
	free(left);
	// All is well, no memory leaks here

	// Now let's get the right subtree, Hi (3), but before we free it, let's get
	// get one of it's subtrees too. We know it won't contain anything so
	// presumably we'll get some sort of error return value
	Tree * right = getRightSubtree(test);	

	Tree * empty = getRightSubtree(right);
	if (empty == NULL)
		printf("The call to an empty subtree returned NULL.\n");
		// Oh good, we did get some error handling, great now there's no problem

	// OOPS, no, there's a huge issue. The getSubtree functions are presumably 
	// doing the following: allocating memory to a temporary new tree pointer, 
	// checking what's in the subtree and returning NULL if nothing is in it.
	// So the temporary new tree pointer to allocated memory is completely lost
	// in all instances.
	
	// We can't even detect the error without a memory checker because the
	// following call of free(NULL) has no effect
	free(empty);
	
	free(right);
	
	destroyBinTree(test);	
}
Esempio n. 2
0
void drawConnectionPreOrder (BinNode * tree) {
	int cury,curx;
	getyx(stdscr,cury,curx);

	if (getRightSubtree(tree)) {
		vline('+',2);
	}
	if (getLeftSubtree(tree)) {
		vline('+', getNumRoots(getRightSubtree(tree))+2);
	}

	move(cury+1,0);
}
Esempio n. 3
0
int numLinesUp (BinNode * tree) {
	BinNode * rightSub = getRightSubtree(tree);
	if (rightSub == NULL) {
		return 0;
	}
	BinNode * leftSub = getLeftSubtree(rightSub);
	int numRoots = getNumRoots(leftSub);

	return numRoots + 2;
}
Esempio n. 4
0
int maxHeight(Tree * tree){
    
    if (tree == NULL)
        return 0;
    int left = maxHeight(getLeftSubtree(tree));
    int right = maxHeight(getRightSubtree(tree));
    if (left < right)
        return right + 1;
    else
        return left + 1;
}
Esempio n. 5
0
void drawConnectionInOrder (BinNode * tree) {
	int cury,curx;
	getyx(stdscr,cury,curx);

	if (getRightSubtree(tree)) {
		move(cury - (numLinesUp(tree)-1), curx);	
		vline('+', numLinesUp(tree));
		move(cury,curx);
	}
	if (getLeftSubtree(tree)) {
		vline('+', numLinesDown(tree));
	}

	move(cury+1,0);
}
Esempio n. 6
0
// Traverse pre order
void traversePreOrder (BinNode * tree, int shift) {
	if (tree == NULL) {
		return;
	}
	printData(tree, shift);
	
	drawConnectionPreOrder(tree);

	traversePreOrder(getRightSubtree(tree), shift + wordStrlen(tree) + 1);

	traversePreOrder(getLeftSubtree(tree), shift + wordStrlen(tree) + 1);

	refresh();

	return;

}
Esempio n. 7
0
void printTree(Tree * tree, int level){
    
    Restaurant * root = getRootData(tree);
    
    Tree * right = getRightSubtree(tree); // set set the right side as a subtree
    Tree * left = getLeftSubtree(tree); // set set the left side as a subtree
    
    if (right != NULL) // if there is a node on the right
        printTree(right, level+1); // evaluate that node
    
    for (int i=0; i<(level*5); i++){
        printf(" ");
    }
    
    printf(" %s(%d)\n", root->name, root->rating);
    
    if (left != NULL) // if there is a node on the right
        printTree(left, level+1);
}
    size_t DynamicSuffixArray::FL(size_t i) {
        uchar c;

        size_t smaller, node = 1; //Start at root

        //Descend down the tree to locate the character
        //Correct the cumulative sum while descending right
        while(node < C_DIM) {
            smaller = C[getLeftSubtree(node)];

            if(smaller >= i) {
                node = getLeftSubtree(node);
            } else {
                node = getRightSubtree(node);
                i -= smaller;
            }
        }

        c = FROM_LEAF(node); //Get char to which the leaf node belongs

        return this->L->select(c, i);
    }
Esempio n. 9
0
int getNumRoots (BinNode * tree) {
	if (tree == NULL) {
		return 0;
	}
	return 1 + getNumRoots(getLeftSubtree(tree)) + getNumRoots(getRightSubtree(tree));
}