Пример #1
0
bool BSTree::remove (int content, BSTNode*& node){
    if(node == NULL){
        return false;
    }
    else if(node->getContents() == content){
        if(node->getLeftChild() == NULL){   
            BSTNode* target = node;
            node = node->getRightChild();
            delete target;
            size --;
            return true;
        }
        else{
            removeMax(node->getContents(), node->getLeftChild());
            size --;
            return true;
        }
    }
    else if(node->getContents() > content){
        return remove(content, node->getLeftChild());
    }
    else{
        return remove(content, node->getRightChild());
    }
}
Пример #2
0
void* minionDoWork(void* ptr){
  Knapsack* ks = ((ThreadArgs*)ptr)->_ks;
  Heap* h = ((ThreadArgs*)ptr)->_h;
  int* nodesProcessed = ((ThreadArgs*)ptr)->_nodesProcessed;
  pthread_mutex_t counterMutex = ((ThreadArgs*)ptr)->_counterMutex; 
  int i, semVal;
  while(1) {
    pthread_mutex_lock(&ks->_mtx);
    //while (pthread_mutex_trylock(&ks->_mtx)) usleep(1000);
    while (isEmpty(h)) {
      sem_post(&ks->_waitingThreads);
      sem_getvalue(&ks->_waitingThreads,&semVal);
      printf("SemVal = %d\n", semVal);
      if (semVal == MINIONS){
        pthread_cond_broadcast(&ks->_cond);
        pthread_mutex_unlock(&ks->_mtx);
        pthread_exit(NULL);
      }
      pthread_cond_wait(&ks->_cond,&ks->_mtx);
      sem_wait(&ks->_waitingThreads);
    }
    Node* n = removeMax(h);
    pthread_mutex_unlock(&ks->_mtx);
    tryProcessNode(ks, h, n); 
  }
}
Пример #3
0
void BSTree::removeMax(int& remThis, BSTNode*& removeNode){
    if(!removeNode->getRightChild()){
        remThis = removeNode->getContents();
        BSTNode *old_node = removeNode;
        removeNode = removeNode->getLeftChild();
        delete old_node;
    }else{
        removeMax(remThis, removeNode->getRightChild());
    }
}
Пример #4
0
void BSTree::removeMax (int& removed, BSTNode*& rootPtr){
    if(rootPtr->getRightChild() == NULL){
        BSTNode* maxNode = rootPtr;
        removed = rootPtr->getData();
        rootPtr = rootPtr->getLeftChild();
        delete maxNode;
    }
    else{
        removeMax(removed, rootPtr->getLeftChild());
    }
}//helper function for private remove
Пример #5
0
void BSTree::removeMax(int& content, BSTNode*& node){
    if(node->getRightChild() == NULL){
        BSTNode* target = node;
        content = node->getContents();
        node = node->getLeftChild();
        delete target;
    }
    else{
        removeMax(content, node->getRightChild());
    }
}
Пример #6
0
void BSTree::removeMax (int& contents, BSTNode*& parent) {
    if (parent->getRightChild() == NULL) {
        BSTNode* temp = parent;
        contents = parent->getContents();
        parent = parent->getLeftChild();
        delete temp;
        temp = NULL;
        size--;
    } else {
        removeMax(parent->getContents(), parent->getRightChild());
    }
}
Пример #7
0
int main() {

    int i;
    int vals[10];
    struct heapStruct *h;
    h = initHeap();

    // Test out individual inserts.
    insertMine(h, 7);
    insertMine(h, 3);
    insertMine(h, 5);
    insertMine(h, 12);
    insertMine(h, 2);
    insertMine(h, 8);
    insertMine(h, 14);
    insertMine(h, 9);
    insertMine(h, 1);
/* 
 
    insert(h, 7);
    insert(h, 3);
    insert(h, 5);
    insert(h, 12);
    insert(h, 2);
    insert(h, 8);
    insert(h, 4);
    insert(h, 9);
    insert(h, 1);
*/
    printHeap(h);

    for (i=0; i<9; i++) {
        printf("Delete %d\n",removeMax(h)); //-----------------------------------------------------------------change here
        printHeap(h);
    }
    freeHeap(h);
    
    // Test out array initialization.
    vals[0] = 12, vals[1] = 3, vals[2] = 18, vals[3] = 14, vals[4] = 5;
    vals[5] = 9, vals[6] = 1, vals[7] = 7; vals[8] = 2, vals[9] = 13;
    
    sort(vals, 10);
    
    for (i=0; i<10; i++)
        printf("%d ", vals[i]);
    printf("\n");
    
 
    return 0;
}
Пример #8
0
void BSTree::removeMax(int& removed, BSTNode*& root)
{
    if (!root->getRightChild())
    {
        BSTNode* oldRoot = root;
        removed = root->getContents();
        root = root->getLeftChild();
        delete oldRoot;
        oldRoot = NULL;
        --mSize;
    }
    else
        removeMax(removed, root->getRightChild());
}
Пример #9
0
void* minionDoWork(void* ptr){
  Knapsack* ks = ((ThreadArgs*)ptr)->_ks;
  Heap* h = ((ThreadArgs*)ptr)->_h;
  //int* nodesProcessed = &((ThreadArgs*)ptr)->_nodesProcessed;
  //int nodesProcessed = 0;
  while (!isEmpty(h)) {
    Node* n = removeMax(h);
    if (n->_depth >= (ks->_nbItems-1)) {
      if (n->_value > ks->_lowerBound) {
        printf("tighten LB to %d\n", n->_value);
	ks->_lowerBound = n->_value;
	ks->_bestX = n->_x;
      }
    } else {
      processNode(ks, h, n);
      //(*nodesProcessed)++;
    }  	 
  }
}
Пример #10
0
int main()
{
  char c;
  cliente entrada;
  while (scanf (" %c", &c) != EOF)
    {
      switch (c)
	{
	case 'A':
	  scanf (" %d", &entrada.dinheiro);
	  scanf (" %[^\n]", entrada.nome);
	  if (insere(entrada))
	    printf("Escritorio cheio\n");
	  break;
	case 'J':
	  if (clientes == 0)
	    {
	      printf("Escritorio vazio\n");
	      break;
	    }
	  entrada = pegaMin();
	  removeMin();	  
	  printf ("Joao vai atender %s (%d)\n", entrada.nome, entrada.dinheiro);
	  break;
	case 'M':
	  if (clientes == 0)
	    {
	      printf("Escritorio vazio\n");
	      break;
	    }
	  entrada = pegaMax();
	  removeMax();	  
	  printf ("Maria vai atender %s (%d)\n", entrada.nome, entrada.dinheiro);
	  break;
	case '#':
	  printf ("Ha %d clientes\n", clientes);
	  break;
	default:;
	}
    }
  return 0;
}
Пример #11
0
bool BSTree::remove (int value, BSTNode*& root) 
{
    if (root == NULL)
        return false;
    else if (root->getContents() > value)
        return remove(value, root->getLeftChild());
    else if (root->getContents() < value)
        return remove(value, root->getRightChild());
    else {
        if(root->getLeftChild() == NULL) 
        {
            BSTNode* oldRoot = root;
            root = root->getRightChild();
            delete oldRoot;
    } else
        removeMax(root->getContents(), root->getLeftChild());
    size--;
    return true;
    }
}
Пример #12
0
bool BSTree::remove(int data, BSTNode*& root)
{
    if (!root)
        return false;
    if (data < root->getContents())
        return remove(data, root->getLeftChild());
    if (data > root->getContents())
        return remove(data, root->getRightChild());
    if (!root->getLeftChild())
    {
        BSTNode* oldRoot = root;
        root = root->getRightChild();
        delete oldRoot;
        oldRoot = NULL;
        --mSize;
    }
    else
        removeMax(root->getContents(), root->getLeftChild());
    return true;
}
Пример #13
0
bool BSTree::remove (int contents, BSTNode*& parent) {
    if (parent == NULL) {
        return false;
    } else if (contents > parent->getContents()) {
        return remove(contents, parent->getRightChild());
    } else if (contents < parent->getContents()) {
        return remove(contents, parent->getLeftChild());
    } else {
        if (parent->getLeftChild() == NULL) {
            BSTNode* temp = parent;
            parent = parent->getRightChild();
            delete temp;
            temp = NULL;
            size--;
        } else {
            removeMax(parent->getContents(), parent->getLeftChild());
        }

        return true;
    }
}
Пример #14
0
bool BSTree::remove(int remThis, BSTNode*& removeNode){
    if(!removeNode){
        return false;
    }
    if(remThis < removeNode->getContents()){
        return remove(remThis, removeNode->getLeftChild());
    }
    if(remThis > removeNode->getContents()){
        return remove(remThis, removeNode->getRightChild());
    }
    if(remThis == removeNode->getContents()){
        if(!removeNode->getLeftChild()){
            BSTNode *old_node = removeNode;
            removeNode = removeNode->getRightChild();
            delete old_node;
        }else{
            removeMax(removeNode->getContents(), removeNode->getLeftChild());
        }
    }
    --size;
    return true;
}
Пример #15
0
bool BSTree::remove (int toRemove, BSTNode*& rootPtr){
    if (rootPtr == NULL){
        return false;
    }
    else if (toRemove < rootPtr->getData()){
        return remove (toRemove, rootPtr->getLeftChild());
    }
    else if (toRemove > rootPtr->getData()){
        return remove (toRemove, rootPtr->getRightChild());
    }
    else{
        if(rootPtr->getLeftChild() == NULL){
            BSTNode* oldPtr = rootPtr;
            rootPtr = rootPtr->getRightChild();
            delete oldPtr;
        }
        else{
            removeMax(rootPtr->getData(), rootPtr->getLeftChild());
        }
        size--;
        return true;
    }
}//traverse the tree and remove the node containing the target integer if present, return true; return false if target integer is not in tree
Пример #16
0
int main (int argc, char* argv[]) {
  /* Read from file input */
  FILE *fp;
  Item* temp;
  Knapsack* ks = (Knapsack*)malloc(sizeof(Knapsack));
  char* fName = argv[1];
  fp = fopen(fName,"r");
  fscanf(fp,"%d", &ks->_nbItems);

  // We randomly chose nbItems/4 as an initial heap size.
  // It's something to tweak as we go on
  Heap* h = initHeap(ks->_nbItems/4);
  ks->_items = (Item**)malloc(sizeof(Item*)*ks->_nbItems);
  ks->_lowerBound = 0;

  // Each row of input has an unused counter variable at the beginning.
  // We'll use fscanf to dump it into j. 
  int i, j;
  /* For each of the items, read in their values and create the node */
  for (i = 0; i < ks->_nbItems; i++) {
    ks->_items[i] = (Item*)malloc(sizeof(Item));
    fscanf(fp,"%d %d %d", &j, &ks->_items[i]->_value, &ks->_items[i]->_weight);
  }
  ks->_bestX = (char*)calloc(ks->_nbItems, sizeof(char));
  fscanf(fp,"%d",&ks->_capacity);

  fclose(fp);
  /* Sort 'em */
  qsort(ks->_items, ks->_nbItems, sizeof(Item*), compare); 

  /* Begin port of bb() */
  
  double rootUB = upperBound(ks, 0, ks->_capacity);
  // I don't understand why depth is -1 here but I'll go with it
  Node* n = initNode(0, -1, rootUB, ks->_capacity, NULL);
  add(h, n);

  /* Begin port of run() */
  int nodesProcessed = 0;
  while (!isEmpty(h)) {
    n = removeMax(h);
    if (n->_depth >= (ks->_nbItems-1)) {
      if (n->_value > ks->_lowerBound) {
        printf("tighten LB to %d\n", n->_value);
        ks->_lowerBound = n->_value;
        for (i=0; i < n->_depth+1; i++) {
          ks->_bestX[i] = n->_x[i];
        }
        for (i=n->_depth+1; i<ks->_nbItems; i++) {
          ks->_bestX[i] = 0;
        }
        destroyNode(n);
      }
    } else {
      processNode(ks, h, n);
      nodesProcessed++;
    }
  }
  printf("Found solution: %d\n", ks->_lowerBound);
  printf("Best x=");
  for (i=0; i<ks->_nbItems-1; i++) {
    printf("%d,",ks->_bestX[i]);
  }
  printf("%d\n",ks->_bestX[ks->_nbItems-1]);
  printf("Nodes processed: %d\n", nodesProcessed);
  // Fix those pesky memory leaks.
  destroyKnapsack(ks);
  destroyHeap(h);
  return 0;
}