Exemplo n.º 1
0
struct node *avlInsert(struct node *root,int data)
{
	int balance;
	if(root==NULL)
		return(newNode(data));
	else{
		if(data <= root->data)
			root->left=avlInsert(root->left,data);
		else
			root->right=avlInsert(root->right,data);
	}

	balance=height(root->left)-height(root->right);
	if(balance>1){
		if(height(root->left->left) >= height(root->left->right)){
			return rightRotate(root);
		}else{

			root->left =leftRotate(root->left);
			return rightRotate(root);
		}
	
	}else if(balance<-1){
		if(height(root->right->right) >= height(root->right->left)){
			return leftRotate(root);
		}else{
			root->right=rightRotate(root->right);
			return leftRotate(root);
		}
	
	}
	root->height=1+max(root->left,root->right);
	return root;
	
}
Exemplo n.º 2
0
void
tw_hash_insert(void *h, tw_event * event, long pe)
{
#ifdef USE_AVL_TREE
  tw_clock start;

  g_tw_pe[0]->avl_tree_size++;

  start = tw_clock_read();
  avlInsert(&event->dest_lp->kp->avl_tree, event);
  g_tw_pe[0]->stats.s_avl += tw_clock_read() - start;
#else
	tw_hash        *hash_t;

	hash_t = (tw_hash *) h;

	insert(hash_t->incoming[pe], event, hash_t->hash_sizes[pe]);

	(hash_t->num_stored[pe])++;
	if (hash_t->num_stored[pe] > floor(hash_t->hash_sizes[pe] * MAX_FRACTION))
	{
		rehash(hash_t, pe);
	}
#endif
}
Exemplo n.º 3
0
int main()
{
    int i, input[50], unbalanced = FALSE;
    element value;
    treePointer root = NULL;

    i=0;
    while(1) {
        scanf("%d", &(input[i]));
        if(input[i]==-1)
            break;
        value.key = input[i];
        avlInsert(&root, value, &unbalanced);
        i++;
    }
    sum = i;
    // print in order
    printf("In-order:");
    count = 0;
    inorder(root);
    printf("\n");
    // print post order
    printf("Post-order:");
    count = 0;
    postorder(root);
    printf("\n");
    return 0;
}
Exemplo n.º 4
0
/* this may replace root, which is why we pass
 * in a AvlTree * */
void
avlInsert(AvlTree *t, tw_event *key)
{
    /* insertion procedure */
    if (*t == AVL_EMPTY) {
        /* new t */
        *t = avl_alloc();
        if (*t == NULL) {
            tw_error(TW_LOC, "Out of AVL tree nodes!");
        }
        
        (*t)->child[0] = AVL_EMPTY;
        (*t)->child[1] = AVL_EMPTY;
        
        (*t)->key = key;
        
        (*t)->height = 1;
        
        /* done */
        return;
    }
    
    if (key->recv_ts == (*t)->key->recv_ts) {
        // We have a timestamp tie, check the event ID
        if (key->event_id == (*t)->key->event_id) {
            // We have a event ID tie, check the send_pe
            if (key->send_pe == (*t)->key->send_pe) {
                // This shouldn't happen but we'll allow it
                tw_printf(TW_LOC, "The events are identical!!!\n");
            }
            avlInsert(&(*t)->child[key->send_pe > (*t)->key->send_pe], key);
            avlRebalance(t);
        }
        else {
            // Event IDs are different
            avlInsert(&(*t)->child[key->event_id > (*t)->key->event_id], key);
            avlRebalance(t);
        }
        return;
    }
    else {
        // Timestamps are different
        avlInsert(&(*t)->child[key->recv_ts > (*t)->key->recv_ts], key);
        avlRebalance(t);
    }
}
Exemplo n.º 5
0
void avlInsert(treePointer *parent, element x, int *unbalanced)
{
    if (!*parent) {
        *unbalanced = TRUE;
        *parent = malloc(sizeof(Node));
        (*parent)->leftChild = (*parent)->rightChild = NULL;
        (*parent)->bf = 0;
        (*parent)->data = x;
    } else if (x.key < (*parent)->data.key) {
        avlInsert(&(*parent)->leftChild, x, unbalanced);
        if (*unbalanced)
            switch ((*parent)->bf) {
            case -1:
                (*parent)->bf = 0;
                *unbalanced = FALSE;
                break;
            case 0:
                (*parent)->bf = 1;
                break;
            case 1:
                leftRotation(parent, unbalanced);
            }
    } else if (x.key > (*parent)->data.key) {
        avlInsert(&((*parent)->rightChild), x, unbalanced);
        if (*unbalanced)
            switch ((*parent)->bf) {
            case 1:
                (*parent)->bf = 0;
                *unbalanced = FALSE;
                break;
            case 0:
                (*parent)->bf = -1;
                break;
            case -1:
                rightRotation(parent, unbalanced);
            }
    } else {
        *unbalanced = FALSE;
        printf("The key is already in the tree");
    }
}
Exemplo n.º 6
0
int main()
{
  treePointer root;
  element *emt;
  int unbalanced;
  void avlInsert(treePointer *parent,element x,int *unbalanced);

  MALLOC(emt,sizeof(element));
  emt->key=1;
  avlInsert(&root,*emt,&unbalanced);
  return 0;
}
Exemplo n.º 7
0
void avlInsert(treePointer *parent,element x,int *unbalanced)
{
  void leftRotation(treePointer *parent,int *unbalanced);
  void rightRotation(treePointer *parent,int *unbalanced);

  if(!*parent)  {  // insert element into null tree
    *unbalanced=TRUE;
    MALLOC(*parent,sizeof(treePointer));
    (*parent)->leftChild=(*parent)->rightChild=NULL;
    (*parent)->bf=0;
    (*parent)->data=x;
  }
  else if(x.key<(*parent)->data.key)  {
    avlInsert(&(*parent)->leftChild,x,unbalanced);
    if(*unbalanced)
    // left branch has grown higher
      switch((*parent)->bf)  {
        case -1: (*parent)->bf=0;
                 *unbalanced=FALSE; break;
        case 0: (*parent)->bf=1; break;
        case 1: leftRotation(parent,unbalanced);  
      }
  }
  else if(x.key>(*parent)->data.key)  {
    avlInsert(&(*parent)->rightChild,x,unbalanced);
    if(*unbalanced)
    // right branch has grown higher
      switch((*parent)->bf)  {
        case 1: (*parent)->bf=0;
                 *unbalanced=FALSE; break;
        case 0: (*parent)->bf=-1; break;
        case -1: rightRotation(parent,unbalanced);
      }
  }
  else  {
    *unbalanced=FALSE;
    printf("The key is already in the tree\n");
  }
}
Exemplo n.º 8
0
/*----------------------------------------------------------------------*
 * Add/Remove								*
 *----------------------------------------------------------------------*/
int
setAdd(SET set,void *data)
{
    /* An ordered set uses the avl tree routines. */
    if (SET_Ord(set))
	return avlInsert(set->telts,data,data);

    /* A non-ordered set uses the disjoint flag to check if it needs to 
     * exclude double elements */
    if (SET_IsSET(set) && setContains(set,data))
	return -1;

    /* Else, or if BAG, add the element to the queue */
    return - !qAdd(set->qelts,data);
}
Exemplo n.º 9
0
int main()
{
	int limit,i;
	int values[MAX];
	struct node *root=NULL;
	printf("Enter the number of  tree nodes: ");
	scanf("%d",&limit);
	printf("Enter %d values\n",limit);
	for(i=0;i<limit;i++){
		scanf("%d",&values[i]);
		root=avlInsert(root,values[i]);
	}
	printf("AVL tree:\n");
	printTree(root);
	printf("\n\nHeight of the tree is  %d \n",root->height);
	printf("\n\n");
	return;
}
Exemplo n.º 10
0
int main(int argc, const char * argv[])
{
    treeNode* avlTree = NULL;
    bool unbalanced = false;
    char code = 0;
    int key = 0;
    while (toupper(code) != 'X') {
        // printf("Enter 'I' to insert node, 'D' to delete node, 'C' to clear the tree, 'S' to show tree, 'X' to exit: ");
        scanf("%s", &code);
        switch (toupper(code)) {
            case 'I':
                // printf("Enter the value of node: ");
                scanf("%d", &key);
                avlInsert(&avlTree, key, &unbalanced);
                break;
                
            case 'D':
                // printf("Enter the value of node: ");
                scanf("%d", &key);
                avlErase(&avlTree, key, &unbalanced);
                break;
                
            case 'C':
                freeTree(avlTree);
                avlTree = NULL;
                unbalanced = false;
                break;
                
            // case 'S':
                // horizonDisplay(avlTree, avlTree, 1);
                // puts("");
                
            default:
                break;
        }
        printT(avlTree);
    }
    // printf("cleaning up...");
    freeTree(avlTree);
    // puts("\nGood bye~");
    return 0;
}