int main() { struct treap *t; char c; t = treap_new(); //_treap_insert(t,'A',187); //_treap_insert(t,'B',215); //_treap_insert(t,'C',238); //_treap_insert(t,'D',140); //_treap_insert(t,'E',239); //_treap_insert(t,'F',180); //_treap_insert(t,'G',101); //_treap_insert(t,'H',180); //_treap_insert(t,'I',104); //_treap_insert(t,'J',239); for(c='A'; c<'K'; c++) { treap_insert(t,c); } treap_print(t); printf("\n......................................................\n"); treap_delete(t,'G'); treap_delete(t,'F'); treap_print(t); treap_free(t); return 0; }
treapset *treap_delete(treapset *root, TREAPTYPE value) { treapset *result; /*base case*/ if (root == NULL) return NULL; if (root->data == value) { if (root->middle != NULL) { /*there is a middle child which will replace the deleted node*/ root->middle->left = root->left; root->middle->right = root->right; result = root->middle; free(root->str); free(root); return result; } else if (root->right == NULL) { if (root->left == NULL) { /*no children*/ free(root->str); free(root); return NULL; } /*left child only*/ result = root->left; free(root->str); free(root); return result; } else if (root->left == NULL) { /*right child only*/ result = root->right; free(root->str); free(root); return result; } else { /*both children*/ /*bubbling down with lower priority child until leaf*/ if (root->left->priority <= root->right->priority) { root = treap_rrotate(root); root->right = treap_delete(root->right, value); } else { root = treap_lrotate(root); root->left = treap_delete(root->left, value); } } } else if (value < root->data) { root->left = treap_delete(root->left, value); } else { root->right = treap_delete(root->right, value); } return root; }
void *consume_packets(rwindow *rwin) { // First packet number that we receive is one with SEQ #1 int next_seq = 1; packet_t *pkt; #ifdef DEBUG char file_name[300]; // Open the file for writing sprintf(file_name, "%s.out", "test"); FILE *pf = fopen(file_name, "w"); ASSERT(pf); #endif double sleep_time; BOOL last_pkt_found = FALSE; do { pthread_mutex_lock(rwin->mutex); treap_node *tn = treap_find(&rwin->t_rwin, next_seq); while (tn != NULL) { pkt = (packet_t *) tn->data; treap_delete(&rwin->t_rwin, next_seq); #if DEBUG int treap_sz = treap_size(&rwin->t_rwin); VERBOSE("==== Read packet %d with datalen %d and flags %x, treap_sz: %d ====\n", next_seq, pkt->datalen, pkt->flags, treap_sz); int ret = fwrite(pkt->data, pkt->datalen, 1, pf); VERBOSE("fwrite returned with ret = %d\n", ret); #else if (!(pkt->flags & FLAG_FIN)) { INFO("\n==== BEGIN PACKET #%d DATA ===" "\n%s" "\n==== END PACKET #%d DATA ===\n", pkt->seq, pkt->data, pkt->seq); } #endif if (pkt->flags & FLAG_FIN) { last_pkt_found = TRUE; break; } next_seq++; tn = treap_find(&rwin->t_rwin, next_seq); } pthread_mutex_unlock(rwin->mutex); if (last_pkt_found) { break; } sleep_time = -1.0 * cargs->mean * log(drand48()); #ifdef DEBUG VERBOSE("sleep_time: %lf\n", sleep_time); #endif usleep(sleep_time * 1000); } while (1); #ifdef DEBUG fclose(pf); #endif return NULL; }