示例#1
0
int
main()
{
	struct searchTree * t = initTree();

   
	struct searchTree *root = t = treeInsert(t , 3);
	treeInsert(t , 8);
	struct searchTree * x = treeInsert(t , 4);
	struct searchTree * y = treeInsert(t , 14);
	treeInsert(t , 9);
	treeInsert(t , 2);
	treeInsert(t , 0);

	treeWalk(t);
	treeDelete(x);
	treeWalk(root);
	treeDelete(y);
	treeWalk(root);

	printf("\nmax = %d min = %d \n", treeMax(t) -> k, treeMin(t) -> k);
	
	if(treeSearch(t, 3) != NULL)
		printf("found\n");
	
	printf("%d\n",t  -> k); 
	t = treeSuccessor(t);
	printf("%d\n",t  -> k); 
	t = treePredecessor(t);
	printf("%d\n",t  -> k); 
}
示例#2
0
void Foam::binaryTree<CompType, ThermoType>::balance()
{
    scalarField mean(chemistry_.nEqns(),0.0);

    //1) walk through the entire tree by starting with the tree's most left
    // chemPoint
    chP* x = treeMin();
    List<chP*> chemPoints(size_);
    label chPi=0;
    //2) compute the mean composition
    while (x != nullptr)
    {
        const scalarField& phij = x->phi();
        mean += phij;
        chemPoints[chPi++] = x;
        x = treeSuccessor(x);
    }
    mean /= size_;

    //3) compute the variance for each space direction
    List<scalar> variance(chemistry_.nEqns(),0.0);
    forAll(chemPoints, j)
    {
        const scalarField& phij = chemPoints[j]->phi();
        forAll(variance, vi)
        {
            variance[vi] += sqr(phij[vi]-mean[vi]);
        }
    }
bool traverse(BRNode * x, int k, int t) {
    if (x == NULL) {
        return false;
    }
    BRNode * y = x;
    while ((y = treePredecessor(y)) != NULL) {
        // [-1,2147483647], 1, 2147483647 should cause overflow
        if (abs(x->value - y->value) < 0 || abs(x->value - y->value) > t) {
            break;
        }
        if (abs(x->index - y->index) <= k) {
            return true;
        }
    }
    y = x;
    while ((y = treeSuccessor(y)) != NULL) {
        if (abs(x->value - y->value) < 0 || abs(x->value - y->value) > t) {
            break;
        }
        if (abs(x->index - y->index) <= k) {
            return true;
        }
    }
    return false;
}
示例#4
0
void tree::remove(node* z)
{
    node* y=(z->leftNode()==m_nil||z->rightNode()==m_nil)?z:treeSuccessor(z);
    node* x=m_nil!=y->leftNode()?y->leftNode():y->rightNode();
    x->setParentNode(y->parentNode());
    if (y->parentNode()==m_nil)
    {
        m_root=x;
    }
    else if (y->parentNode()->leftNode()==y)
    {
        y->parentNode()->setLeftNode(x);
    }
    else
    {
        y->parentNode()->setRightNode(x);
    }
    
    if (y!=z)
    {
        z->setValue(y->value());
        z->setKey(y->key());
    }
    
    if (y->nodeColor()==black)
    {
        removeFixUp(x);
    }
    delete y;
}
示例#5
0
 void _redblacktree::remove(tree *z){ // remove z
     tree *x, *y;
     if (z->left == NULL && z->right == NULL) { // if z is a leaf
         y = z; // ??
     } else {
         y = treeSuccessor(z); // find a node after Z
     }
     if (y->left != NULL) { // check if left tree is null
         x = y->left;
     } else {
         x = y->right;
     }
     if (x != NULL) {
         x->parent = y->parent;
     }
     
     if(y->parent == NULL){
         root = x;
     } else {
         if( y == y->parent->left){
             y->parent->left = x;
         } else {
             y->parent->right = x;
         }
     }
     if (y != z) {
         z->ptr = y->ptr;
         z->size = y->size;
         if (y->ptr == 0) {
             delete z;
         }
     }
     if (y->color == black && x != NULL) {
         DeleteFixup(x);
     }
 }