int main() { ull i,n,j,x; scanf("%llu",&n); while(n) { n=n+1; ull a[n],b[n],c[n],d[n]; ull total = 0; scanf("%llu",&a[1]); for(i=2;i<n;i++) { scanf("%llu",&x); insert(a,x,i); } scanf("%llu",&b[1]); for(i=2;i<n;i++) { scanf("%llu",&x); insert(b,x,i); } for(i=1;i<n;i++) { c[i] = delete_min(a,n-i); d[i] = delete_min(b,n-i); } for(i=1;i<n;i++) total += c[i] * d[n-i]; printf("%llu\n",total); scanf("%llu",&n); } return 0; }
void dijkstra(graph* g, int source, item** ph) { int infinity = 100000000; int vertices = g->node_count; heap* h = make_heap(); for (int i = 0; i < vertices; i++) { item* itm = (item*)malloc(sizeof(item)); if (i == source) { itm->key = 0; } else { itm->key = infinity; } int* item_val = (int*)malloc(sizeof(int)); *item_val = i; itm->value = item_val; ph[i] = itm; insert_item(itm, h); } item* val = find_min(h); delete_min(h); while (val != NULL) { if (val->key == infinity) { break; } int u_index = *((int*)(val->value)); g_node* n = g->nodes[u_index]; for (int v_index = 0; v_index < n->edge_count; v_index++) { edge* e = n->edges[v_index]; int dist_between = e->distance; item* v = ph[e->target->id]; int alt = val->key + dist_between; if (alt < v->key) { int delta = v->key - alt; decrease_key(delta, v, h); } } val = find_min(h); delete_min(h); } }
static dbtype_t _bonsai_delete(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t *valout) { dbval_t *np = dbptr(ctx, node); dbtype_t min, left, right; int cmp; if (!node.all) { if (valout) valout->type = Error; return DBNULL; } left = np->left; right = np->right; cmp = dbcmp(ctx, key, np->key); if (cmp < 0) { return balance(ctx, node, _bonsai_delete(ctx, left, key, valout), right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, left, _bonsai_delete(ctx, right, key, valout), subtree_right, 1); } if (valout) *valout = np->value; rcuwinner(node, 0xe9); if (!left.all) return right; if (!right.all) return left; right = delete_min(ctx, right, &min); return balance(ctx, min, left, right, subtree_right, 0); }
static void pet_test(void) { #define NODES 1000000 node_t *pool, *root; struct timeval tv, tv2; time_t sec; suseconds_t msec; int i, j; pool = pool_new(NODES); j = NODES / 2; gettimeofday(&tv, NULL); root = alloc_node(pool); root = make_node(root, 0); for (i = 1; i < NODES; i++, j = (j + 17) % NODES) root = insert(root, alloc_node(pool), j); while (root) root = delete_min(root); gettimeofday(&tv2, NULL); sec = tv2.tv_sec - tv.tv_sec; msec = tv2.tv_usec - tv.tv_usec; msec += sec * 1000000; printf("%d nodes add/remove: %lu msec\n", NODES, msec); pool_release(pool); }
int main() { ull i,j,k = 0; for(i=2;i<1000000;i++) if(p[i]==0) { prime[k++] = i; for(j=2*i;j<1000000;j=j+i) p[j] = 1; } int test_cases,t; scanf("%d",&test_cases); for(t=1;t<=test_cases;t++) { ull n,x,i,j=0,l=0; scanf("%llu",&n); n++; ull num[n],num_sorted[n],a[n]; scanf("%llu",&num[1]); for(i=2;i<n;i++) { scanf("%llu",&x); insert(num,x,i); } for(i=1;i<n;i++) num_sorted[i] = delete_min(num,n-i); i=1; printf("Case #%d: %llu\n",t,l); for(i=0;i<l;i++) printf("%llu\n",a[i]); } return 0; }
int main() { int i,n,j,x,k,m,y,z; scanf("%d%d",&n,&m); n=n+1; arr a[n],b[n]; int c[n]; scanf("%d",&a[1].data); a[1].index=1; c[1] = a[1].data; for(i=2;i<n;i++) { scanf("%d",&x); insert(a,x,i); c[i] = x; } for(i=1;i<n;i++) b[i] = delete_min(a,n-i); for(i=1;i<n;i++) printf("i==%d d==%d\n",b[i].index,b[i].data); //while(m--) //{ // scanf("%d%d%d",&x,&y,&z); //} return 0; }
int main() { //srand(time(0)); srand(50); heap *h = make_heap(); node **nodes = malloc(TESTSIZE * sizeof(node *)); for(int i = 0; i < TESTSIZE; i++) { nodes[i] = insert(h, rand() % 100); } while (h->size > 0) { make_dot(nodes, "after_inserts.dot"); getchar(); if (rand() % 2) { printf("decrease_key\n"); decrease_key(h, nodes[rand() % TESTSIZE], rand() % 100); } else { printf("delete_min\n"); delete_min(h); } if (h->size > 0) printf("heap size = %d and root key = %d\n", h->size, h->root->key); } free(nodes); return 1; }
void insert_review(long long val) { if (input < 3) { insert_max(val); return; } long third = input/3; long size = min_heap.size() - 1; long long temp; if (size < third) { // can add logic if (val > max_heap[1]) { insert_min(val); } else { temp = delete_max(); if (temp != -1) insert_min(temp); insert_max(val); } } else { // can't add logic if (val > min_heap[1]) { temp = delete_min(); insert_min(val); if (temp != -1) insert_max(temp); } else { insert_max(val); } } }
job pq_remove(pq q, job j) { uint64_t id; unsigned int pri; if (j->heap_index >= q->used) return NULL; if (q->heap[j->heap_index] != j) return NULL; id = j->id; j->id = 0; pri = j->pri; j->pri = 0; bubble_up(q, j->heap_index); j->id = id; j->pri = pri; /* can't happen */ if (q->heap[0] != j) return NULL; delete_min(q); return j; }
int main() { int i,n,j,x,count=0,k,l; scanf("%d",&n); n=n+1; int a[4][n],b[4][n]; scanf("%d",&a[0][1]); scanf("%d",&a[1][1]); scanf("%d",&a[2][1]); scanf("%d",&a[3][1]); for(i=2;i<n;i++) { scanf("%d",&x); insert(a[0],x,i); scanf("%d",&x); insert(a[1],x,i); scanf("%d",&x); insert(a[2],x,i); scanf("%d",&x); insert(a[3],x,i); } for(i=1;i<n;i++) { b[0][i] = delete_min(a[0],n-i); b[1][i] = delete_min(a[1],n-i); b[2][i] = delete_min(a[2],n-i); b[3][i] = delete_min(a[3],n-i); } for(i=1;i<n;i++) { for(j=1;j<n;j++) { for(k=1;k<n;k++) { for(l=1;l<n;l++) { if(b[3][l] > -1*(b[0][i]+b[1][j]+b[2][k])) break; if(b[0][i]+b[1][j]+b[2][k]+b[3][l] == 0) count++; } } } } printf("%d\n",count); return 0; }
//Deletion in Median Heap int del_med(Heap H,int n) { //Returns root of max heap if N2 = N1 //Returns root of min heap if N2 = N1 + 1 if(H.size_max == n - H.size_min-1) return delete_max(H,n); else return delete_min(H,n); }
/* * Deletes the leftmost node in the subtree. (Note that "deletes" means "removes from the tree." No memory * delete operation is actually performed.) * * Parameters: * - ptn = Pointer to root of subtree to have its leftmost node deleted. * * Returns: * Pointer to root of subtree after having the leftmost node deleted. * * N.B.: * This function is recursive; however, the nature of the tree guarantees that the stack space consumed * by its stack frames will be O(log n). */ static PRBTREENODE delete_min(PRBTREENODE ptn) { if (!(ptn->ptnLeft)) return rbtNodeRight(ptn); if (!rbtIsRed(ptn->ptnLeft) && !rbtIsRed(ptn->ptnLeft->ptnLeft)) ptn = move_red_left(ptn); ptn->ptnLeft = delete_min(ptn->ptnLeft); return fix_up(ptn); }
void sort(int a[], int n) { int i; for (i = 0; i < n; i++) { insert(a[i]); } for (i = 0; i < n; i++) { a[i] = delete_min(); } }
job pq_take(pq q) { job j; if (q->used == 0) return NULL; j = q->heap[0]; delete_min(q); return j; }
/* 削除した最小値は *min に与える,削除後の木を返す.*/ node *delete_min(int *min, bs_tree t) { node *temp; if(t->leftchild == NULL) { /* 左の子がないので最小値がある */ *min = t->key; temp = t->rightchild; free(t); /* メモリの解放 */ t = temp; } else t->leftchild = delete_min(min, t->leftchild); return t; }
/* dont use the return value */ struct node* delete_min(struct node *root, int free_memory) { struct node *replace = NULL; if(root && root->left == NULL) { replace = root->right; if(free_memory) free(root); return replace; } root->left = delete_min(root->left, free_memory); root->count = 1 + get_size(root->left) + get_size(root->right); return root; }
static dbtype_t delete_min(pgctx_t *ctx, dbtype_t node, dbtype_t *out) { dbval_t *np = dbptr(ctx, node); dbtype_t left = np->left, right = np->right; if (!left.all) { *out = node; return right; } return balance(ctx, node, delete_min(ctx, left, out), right, subtree_left, 0); }
element_type delete_min(SEARCH_TREE T,tree_ptr father) { element_type i; if(T==NULL) return NULL; if(T->left==NULL){ i=T->element; father->left=T->right; free(T); return i; }else return(delete_min(T->left,T)); }
void timer_thread(unsigned long now) { htimer_t *tm; while ((tm = root)) { if (now < tm->node.key) break; root = __container_of(delete_min(&tm->node)); if (tm->expire) tm->expire(tm); } }
static dbtype_t _bonsai_multi_delete(pgctx_t *ctx, dbtype_t node, dbtype_t key, dbtype_t *value) { dbval_t *np = dbptr(ctx, node); dbtype_t min, left, right; int cmp; int i; if (!node.all) { value->type = Error; return DBNULL; } left = np->left; right = np->right; cmp = dbcmp(ctx, key, np->key); if (cmp < 0) { return balance(ctx, node, _bonsai_delete(ctx, left, key, value), right, subtree_left, 1); } if (cmp > 0) { return balance(ctx, node, left, _bonsai_delete(ctx, right, key, value), subtree_right, 1); } if (np->nvalue == 1) { if (dbcmp(ctx, *value, np->values[0])!=0) { value->type = Error; return node; } if (!left.all) return right; if (!right.all) return left; right = delete_min(ctx, right, &min); rcuwinner(node, 0xe9); return balance(ctx, min, left, right, subtree_right, 0); } node = bonsai_copy(ctx, left, right, node); np = dbptr(ctx, node); for(i=0; i<np->nvalue; i++) { if (dbcmp(ctx, *value, np->values[i])==0) { memcpy(np->values+i, np->values+i+1, (np->nvalue-i-1)*sizeof(dbtype_t)); break; } } if (i == np->nvalue) { value->type = Error; } else { np->nvalue--; } return node; }
void k_smallest( int* input_array, int num_input_values, int* output_array, int k ) { if (k == 0) return; // copy input array into a temporary array -- should not contaminate input_array int temp[num_input_values], heap_size = num_input_values; for (int i = 0; i < num_input_values; i++){ temp[i] = input_array[i]; } heapify(temp, &heap_size); for (int i = 0; i < k; i++){ output_array[i] = delete_min(temp, &heap_size); } }
static rbtree_node* delete_min(rbtree_node *h, VALUE *deleted_value) { if ( !h->left ) { if(deleted_value) *deleted_value = h->value; free(h); return NULL; } if ( !isred(h->left) && !isred(h->left->left) ) h = move_red_left(h); h->left = delete_min(h->left, deleted_value); return fixup(h); }
int main() { int i,n,j,x,k; long long int count=0; scanf("%d%d",&n,&k); n=n+1; arr a[n],b[n],piles[k]; scanf("%d",&a[1].data); a[1].index=1; for(i=2;i<n;i++) { scanf("%d",&x); insert(a,x,i); } b[1] = delete_min(a,n-1); printf("i==%d d==%d\n",b[1].index,b[1].data); for(i=2;i<n;i++) { b[i] = delete_min(a,n-i); printf("i==%d d==%d\n",b[i].index,b[i].data); } printf("%lld\n",count); return 0; }
/* * Deletes the node in the subtree having an arbitrary key. (Note that "deletes" means "removes from the tree." * No memory delete operation is actually performed.) An O(log n) operation. * * Parameters: * - ptree = Pointer to the tree head structure, containing the compare function. * - ptnCurrent = Pointer to the root of the current subtree we're deleting from. * - key = Key value we're deleting from the tree. It is assumed that this key value exists in the subtree. * * Returns: * Pointer to the root of the subtree after the node has been deleted. * * N.B.: * This function is recursive; however, the nature of the tree guarantees that the stack space consumed * by its stack frames (and those of delete_min, where we call it) will be O(log n). */ static PRBTREENODE delete_from_under(PRBTREE ptree, PRBTREENODE ptnCurrent, TREEKEY key) { register TREEKEY keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent)); register int cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent); if (cmp < 0) { /* hunt down the left subtree */ if (!rbtIsRed(ptnCurrent->ptnLeft) && !rbtIsRed(ptnCurrent->ptnLeft->ptnLeft)) ptnCurrent = move_red_left(ptnCurrent); ptnCurrent->ptnLeft = delete_from_under(ptree, ptnCurrent->ptnLeft, key); } else { if (rbtIsRed(ptnCurrent->ptnLeft)) { ptnCurrent = rotate_right(ptnCurrent); keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent)); cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent); } if ((cmp == 0) && !rbtNodeRight(ptnCurrent)) return ptnCurrent->ptnLeft; /* degenerate case */ if ( !rbtIsRed(rbtNodeRight(ptnCurrent)) && (!rbtNodeRight(ptnCurrent) || !rbtIsRed(rbtNodeRight(ptnCurrent)->ptnLeft))) { ptnCurrent = move_red_right(ptnCurrent); keyCurrent = (*(ptree->pfnGetTreeKey))((*(ptree->pfnGetFromNodePtr))(ptnCurrent)); cmp = (*(ptree->pfnTreeCompare))(key, keyCurrent); } if (cmp == 0) { /* * Here we find the minimum node in the right subtree, unlink it, and link it into place in place of * ptnCurrent (i.e. node pointed to by ptnCurrent should no longer be referenced). We inherit the * child pointers and color of ptnCurrent (minus the reference from the right-hand tree where applicable). */ register PRBTREENODE ptnMin = find_min(rbtNodeRight(ptnCurrent)); rbtSetNodeRight(ptnMin, delete_min(rbtNodeRight(ptnCurrent))); ptnMin->ptnLeft = ptnCurrent->ptnLeft; rbtSetNodeColor(ptnMin, rbtNodeColor(ptnCurrent)); ptnCurrent = ptnMin; } else /* hunt down the right subtree */ rbtSetNodeRight(ptnCurrent, delete_from_under(ptree, rbtNodeRight(ptnCurrent), key)); } return fix_up(ptnCurrent); }
/* * Sorts the edges in the MST using (conveniently) heap sort. Returns the largest */ double sort_MST(int numpoints, node graph[]) { // initialize heap & location array node heap[numpoints]; int location[numpoints]; // fill the heap, remember that dist(i) = min(i,j) for all j for(int i = 0; i < numpoints; i++){ location[i] = 0; insert(graph[i], heap, location); // insert element } // sort by removing from heap double sorted_arr[numpoints]; for(int i = 0; i < numpoints; i++) { sorted_arr[i] = delete_min(heap, location).dist; } return sorted_arr[numpoints-1]; }
/* Returns the head of the bianry tree - Hibbard deletion */ struct node *del_node(struct node *head, int value) { struct node *curr = NULL; struct node *replace = NULL; if(head == NULL) return NULL; curr = head; if(value < curr->value && curr->left) { curr->left = del_node(curr->left, value); } else if(value > curr->value && curr->right) { curr->right = del_node(curr->right, value); } else { /* found the exact node*/ if(curr->left == NULL) { free(curr); return curr->right; } if(curr->right == NULL) { free(curr); return curr->left; } /* 1: get_min from curr's right, this is the repalcement node so save ptr * 2: del the that from the location not from mremory * 3: update it's children and its currents location * * Eg: To delete 9 -> repalce with node 11 * and update node 11 left and right */ replace = get_min(curr->right); delete_min(curr->right, FALSE); replace->right = curr->right; replace->left = curr->left; free(curr); } head->count = get_size(head->left) + get_size(head->right) + 1; return replace; }
void main() { int op; do { printf("enter option\n1: insert\n2:display\n3:delete min\n 4: exit\n"); scanf("%d",&op); switch (op) { case 1: insert_end(); break; case 2: display(); break; case 3: delete_min(); break; case 4: break; } } while (op!=4); }
void delete_min(int ind) { int max=ind; if(2*ind<=maxheap[0]&&maxheap[max]>maxheap[2*ind]) { max=2*ind; } else if(2*ind+1<=maxheap[0]&&maxheap[max]>maxheap[2*ind+1]) { max=2*ind+1; } int tmp; if(max!=ind) { tmp=maxheap[ind]; maxheap[ind]=maxheap[max]; maxheap[max]=tmp; delete_min(max); } return; }
static void delete_min_test(void) { node_t *pool, *root; pool = pool_new(10); root = make_node(alloc_node(pool), 1); root = insert(root, alloc_node(pool), 5); root = insert(root, alloc_node(pool), 8); root = insert(root, alloc_node(pool), 10); root = insert(root, alloc_node(pool), 2); root = insert(root, alloc_node(pool), 4); root = insert(root, alloc_node(pool), 6); root = insert(root, alloc_node(pool), 3); while (root) { printf("delete key: %lu\n", root->key); root = delete_min(root); } pool_release(pool); }
//process a new token read from the stream void Naive_Estimator_Update(Naive_Estimator_type * est, int token) { est->count++; Freq_Update(est->freq, token); //end of Misra-Gries part of algorithm //increment count of token, sets processing to 1 c_a* counter = naive_increment_count(est->hashtable, token); if(est->count == 1) { naive_handle_first(est, counter); return; } Sample_type* min; while(((Sample_type*) peek_min(est->prim_heap))->prim <= est->count) { min=delete_min(est->prim_heap); if(min->prim < est->count) { fprintf(stderr, "a sampler's prim decreased. fatal error\n"); fprintf(stderr, "min->c_s0_key: %d, min->prim %d, est->count %d\n", min->c_s0->key, min->prim, est->count); exit(1); } naive_decrement_prim_samplers(est->hashtable, min->c_s0); naive_increment_prim_samplers(counter); //have min take a new sample min->c_s0 = counter; min->val_c_s0 = counter->count; min->t0 *= prng_float(est->prng); naive_reset_wait_times(min, est); //reinsert min into primary heap insert_heap(est->prim_heap, min); } naive_done_processing(est->hashtable, counter); }