Пример #1
0
/* Inserts |item| into |table|, replacing any duplicate item.
   Returns |NULL| if |item| was inserted without replacing a duplicate,
   or if a memory allocation error occurred.
   Otherwise, returns the item that was replaced. */
struct AVLNode *
AVL_Replace(struct AVLNode **table, struct AVLNode *item, AVLNODECOMP cmpnode)
{
	struct AVLNode *p = AVL_Insert(table, item, cmpnode);

	if (p) {
		item->Link[0] = p->Link[0];
		item->Link[1] = p->Link[1];
		//item->Parent = p->Parent;
		//item->Balance = p->Balance;
		item->Private = p->Private;
		//if (p->Parent->Link[0] == p)
		//	p->Parent->Link[0] = item;
		//else
		//	p->Parent->Link[1] = item;
		struct AVLNode *a = getParent(p);
		if (a->Link[0] == p)
			a->Link[0] = item;
		else
			a->Link[1] = item;
		if (p->Link[0])
		//	p->Link[0]->Parent = item;
			setParent(p->Link[0], item);
		if (p->Link[1])
			//p->Link[1]->Parent = item;
			setParent(p->Link[1], item);
	}

	return p;
}
Пример #2
0
int hyperGraphAddNode(HyperGraphType h, HyperNodeType *x)
{
	/*
		adds x to h
	*/
	
	return AVL_Insert(h.vertices, x);
} /* hyper graph add node */
Пример #3
0
int main(int argc, char **argv)
{
    treenode *root = NULL;
    int i;
    int pp[MAX];
    int *p;
    int height_max = (int)(log(MAX) * 2.07 - 0.33);
    
    srand(time(NULL));
	for (round = 0; round < ROUND; round++) {
	    for (i = 0; i < MAX; i++) {
	    	pp[i] = i;
	    }
	    for (i = 0; i < MAX; i++) {
	    	int j, k, temp;
	    	j = rand() % MAX;
	    	k = rand() % MAX;
	    	temp = pp[j];
	    	pp[j] = pp[k];
	    	pp[k] = temp;
	    }
	
	    for (i = 0; i < MAX; i++) {
	        AVL_Insert(&root, pp[i]);
	    }
	    p = pp;
	    AVL_InTW(root, compare, &p);
	    for (i = 0; i < MAX; i++) {
	    	if (i != pp[i]) printf("faint!!\n");
	    }
	}
    AVL_PostTW(root, measureheight, NULL);
    printf("total height: real: %d, max: %d\n", root->height, height_max);

    AVL_PostTW(root, freenode, NULL);
    return 0;
}
Пример #4
0
bool AVL_Insert(treenode ** proot, int newinfo)
{
    bool tallersubtree;
    bool taller;
    treenode *root;

    root = *proot;
    if (root == NULL) {
        root = (treenode *) malloc(sizeof(treenode));
        root->info = newinfo;
        root->count = 1;
        root->Lchild = NULL;
        root->Rchild = NULL;
        root->bf = EH;
        taller = true;
    }

    else if (newinfo < root->info) {
        tallersubtree = AVL_Insert(&(root->Lchild), newinfo);
        if (tallersubtree)
            switch (root->bf) {
            case LH:
                root = AVL_LeftBalance(root);
                taller = false;
                break;
            case EH:
                root->bf = LH;
                taller = true;
                break;
            case RH:
                root->bf = EH;
                taller = false;
                break;
            }

        else
            taller = false;
    }

    else if (newinfo > root->info) {
        tallersubtree = AVL_Insert(&(root->Rchild), newinfo);
        if (tallersubtree)
            switch (root->bf) {
            case LH:
                root->bf = EH;
                taller = false;
                break;
            case EH:
                root->bf = RH;
                taller = true;
                break;
            case RH:
                root = AVL_RightBalance(root);
                taller = false;
            }

        else
            taller = false;
    }

    else {                      // newinfo == root->info
        root->count++;
        taller = false;
    }
    *proot = root;
    return taller;
}                               // end insert
Пример #5
0
int PebblerHyperNodeAddEdge(PebblerHyperNodeType *n, PebblerHyperEdgeType *e)
{
    return AVL_Insert(n, e);
} /* add edge */
Пример #6
0
int main(void)
{
	AVL_TREE *tree;
	int menuChoice, x, *dataPtr;

	tree = AVL_Create(compareInt);

	menuChoice = menu();
	while(menuChoice != -1)
	{
		switch(menuChoice)
		{
			case 1 :
				printf("Enter number: ");
				scanf("%d", &x);
				dataPtr = AVL_Retrieve(tree, &x);
				if(dataPtr == NULL)
				{
					dataPtr = malloc(sizeof (int));
					(*dataPtr) = x;
					AVL_Insert(tree, dataPtr);
				}
				else
					printf("%d already exists\n", x);
				break;
			case 2 :
				printf("Enter number: ");
				scanf("%d", &x);
				dataPtr = AVL_Retrieve(tree, &x);
				if(dataPtr == NULL)
					printf("%d does not exitst\n", x);
				else
				{
					dataPtr = malloc(sizeof (int));
					(*dataPtr) = x;
					AVL_Delete(tree, dataPtr);
					free(dataPtr);
				}
				break;
			case 3 :
				printf("There are %d nodes in the tree\n", AVL_Count(tree));
				break;
			case 4 :
				printf("Enter number: ");
				scanf("%d", &x);
				dataPtr = AVL_Retrieve(tree, &x);
				if(dataPtr == NULL)
					printf("%d was NOT found in the tree\n", x);
				else
					printf("%d was found in the tree\n", x);
				break;
			case 5 :
				printf("\n{ ");
				AVL_traverse(tree, printInt);
				printf(" }\n");
				break;
			default :
				printf("error\n");
				break;
		}
		menuChoice = menu();
	}

	tree = AVL_Destroy(tree);

	printf("Done.\n");
	return 0;
} /* main */