コード例 #1
0
bool remove_from_bst(BST *tree, int item) {
  Node **found_node = find_node(tree, item);
  // We don't do anything of the value is not in the tree.
  if (found_node == NULL) {
    return false;
  }

  Node *left_node = (*found_node)->left;
  Node *right_node = (*found_node)->right;
  
  if (left_node != NULL) {
    // We replace the root with the largest node in the left subtree.
    Node **pointer_to_largest = get_largest_node(&((*found_node)->left));
    Node *largest = *pointer_to_largest;
    assert(largest->right == NULL);

    // We replace the largest element with whatever was in its left subtree.
    *pointer_to_largest = largest->left;

    // Set the children of the replacement node.
    if (largest != left_node) {
      largest->left = left_node;
    }
      largest->right = right_node;

    // free the memory for the old root.
    free(*found_node);

    // Set the parent of the root to point to the new root.
    *found_node = largest;
  } else if (right_node != NULL) {
    // We replace the root with the smallest node in the right subtree.
    Node **pointer_to_smallest = get_smallest_node(&((*found_node)->right));
    Node *smallest = *pointer_to_smallest;
    assert(smallest->left == NULL);

    // We replace the smallest element with whatever was in its left subtree.
    *pointer_to_smallest = smallest->right;

    // Set the children of the replacement node.
    if (smallest != right_node) {
      smallest->right = right_node;
    }
    smallest->left = left_node;

    // free the memory for the old root.
    free(*found_node);

    // Set the parent of the root to point to the new root.
    *found_node = smallest;
  } else { // Both left and right are NULL.
    free(*found_node);
    *found_node = NULL;
  }

    tree->size--;
    return true;
}
コード例 #2
0
void *schedule_function( void *ptr )
{
	char *filename, *request_type, *con_type, *readBuf = NULL;
	char *str1;
	FILE*  fp1;
	size_t read;	
	char *data;
	int sock;
	int fsize;
	Node * temp;
	
	data=malloc(sizeof(char)*200);
	memset(data,0,sizeof(char)*200);
	sleep(t);
	while(1)
	{
pthread_mutex_lock(&lock1);
	    if(abcd->head!=NULL)
	    {
		if(SJF==1)
		  {
		    temp = get_smallest_node();
		  }
		else
		{
		  temp = get_first();	
		}
		add_to_sec(temp);
		pthread_mutex_unlock(&lock1);	      
	    }
	    else
	      {	      
		pthread_mutex_unlock(&lock1);
	      }
	}
}
コード例 #3
0
int get_smallest_in_bst(BST *tree) {
  assert(tree != NULL);
  assert(tree->size != 0);

  return ((*get_smallest_node(&tree->root))->value);
}