Exemple #1
0
/* Retrieve the node associated with a specific index in a sparse
 * array.  It returns the value
 * associated with the index. If the index does not exist in the
 * array, it returns NULL. If the given index is smaller than the current
 * node, search left ; if it is larger, search right.
 */
SparseNode * SparseArray_getNode(SparseNode * array, int index )
{
  if (array == NULL) { return NULL; }
  if (index == (array->index)) { return array; }
  if (index < (array->index)) { return SparseArray_getNode(array->left, index);}
  return SparseArray_getNode(array->right, index);
}
Exemple #2
0
SparseNode * SparseArray_merge(SparseNode * array_1, SparseNode * array_2)
{
  SparseNode * mergeInsert (SparseNode * array, int index, int value, SparseNode * array_1);
  SparseNode * DeleteZero (SparseNode * array);

  int n; 
  int min = SparseArray_getMin(array_2);
  int max = SparseArray_getMax(array_2);

  if (SparseArray_getMin(array_1) < min)
    min = SparseArray_getMin(array_1);

  if (SparseArray_getMax(array_1) > max)
    max = SparseArray_getMax(array_1);

  SparseNode * copy; 
  copy = SparseArray_copy(array_1); 

  for (n=min; n<=max; n++) 
  {  
    if(SparseArray_getNode(array_2,n) != NULL)
      copy = mergeInsert(copy, n, SparseArray_getNode(array_2,n)->value, array_1); 
  }
  copy = DeleteZero(copy);
  return copy;
}
Exemple #3
0
SparseNode * SparseArray_getNode(SparseNode * array, int index)
{
  if (array == NULL) return NULL;
  if (array->index == index) return array;
  if (array->index > index)
    return SparseArray_getNode(array->left, index);
  else
    return SparseArray_getNode(array->right, index);
}
Exemple #4
0
SparseNode * DeleteZero (SparseNode * array)
{
  int n;
  int min = SparseArray_getMin(array);
  int max = SparseArray_getMax(array);

  for (n=min; n<=max; n++)    {
      if (SparseArray_getNode(array, n) && (SparseArray_getNode(array, n) -> value == 0))
	array = SparseArray_remove(array, n);
    }
  return array;
}
Exemple #5
0
/* Retrieve the node associated with a specific index in a sparse
 * array tree.  
 *
 * Arguments:
 * *array         the root node of a sparse array tree
 * index          the index of the node you want to search
 * 
 * returns:
 * SparseNode*    the node with the index that you searched from the tree.
 *                If no node found, NULL should be returned. 
 *                
 * Hint: If the given index is smaller than the current
 * node, search left ; if it is larger, search right.
 */
SparseNode * SparseArray_getNode(SparseNode * array, int index )
{
  if (array == NULL)
    return NULL;

  if (index == (array -> index))
    return array;
  else if (index < (array -> index))
    return SparseArray_getNode(array -> left, index);
  else if (index > (array -> index))
    return SparseArray_getNode(array -> right, index);

  return NULL;
}
Exemple #6
0
SparseNode * SparseArray_mergehelp(SparseNode * mergeNode, SparseNode * array_2)
{
  if(array_2 == NULL)
    {
      return mergeNode;
    }

  SparseNode * getNode = SparseArray_getNode(mergeNode, array_2 -> index);
  SparseNode * tempNode = array_2;

  SparseArray_mergehelp(mergeNode, array_2 -> right);
  SparseArray_mergehelp(mergeNode, array_2 -> left);

      if(getNode == NULL)
	{
	  mergeNode = SparseArray_add(mergeNode, tempNode -> index, tempNode -> value);
	}
      else
	{
	  getNode -> value += tempNode -> value;

	  if(getNode -> value == 0)
	    {
	      mergeNode = SparseArray_remove(mergeNode, getNode -> index);
	    }
	}

  return mergeNode;
}
Exemple #7
0
/* Merge array_1 and array_2, and return the result array. 
 * This function WILL NOT CHANGE the contents in array_1 and array_2.
 *
 * Arguments:
 * *array_1         the root node of the first sparse array tree
 * *array_2         the root node of the second sparse array tree
 * 
 * returns:
 * SparseNode*    the root node of the new sparse array tree that you just
 *                merged from the two input sparse array tree
 *     
 * When merging two sparse array tree:
 * 1. The contents in array_1 and array_2 should not be changed. You should make
 *    a copy of array_1, and do merging in this copy.
 * 2. array_2 will be merged to array_1. This means you need to read nodes in 
 *    array_2 and insert them into array_1.
 * 3. You need to use POST-ORDER to traverse the array_2 tree. 
 * 4. Values of two nodes need to be added only when the indices are the same.
 * 5. A node with value of 0 should be removed.
 * 6. if array_2 has nodes with index different than any nodes in array_1, you
 *    should insert those nodes into array_1.
 * 
 * Hint: you may write new functions
 */
SparseNode * SparseArray_mergeHelper(SparseNode* p, SparseNode* array_2) {
  if (array_2 != NULL) {
    SparseNode* q = NULL;
    if ((q = SparseArray_getNode(p, array_2 -> index))) { //Search of current array_2 index in p successful
      int sum = q -> value + array_2 -> value;
      if (sum == 0)
        p = SparseArray_remove(p, q -> index); 
      else
        q -> value = sum;
    } 
    else //If search unsuccessful, insert node pointed by array_2 into p
      p = SparseArray_insert(p, array_2 -> index, array_2 -> value);
  
    //SparseNode* freeOne = p; //save address of p for freeing later
    p = SparseArray_mergeHelper(p, array_2 -> left);
    //SparseArray_destroy(freeOne);

    //SparseNode* freeTwo = p;
    p = SparseArray_mergeHelper(p, array_2 -> right);
    //SparseArray_destroy(freeTwo);
    return p;
  }
  else
    return p;
}
Exemple #8
0
/* Retrieve the node associated with a specific index in a sparse
 * array.  It returns the value
 * associated with the index. If the index does not exist in the
 * array, it returns NULL. If the given index is smaller than the current
 * node, search left ; if it is larger, search right.
 */
SparseNode * SparseArray_getNode(SparseNode * array, int index )
{
  if (array->index == index)
  {
    return array;
  }
  
  if (array->index > index && array->left != NULL)
  {
    return SparseArray_getNode(array->left, index);
  }
  
  if (array->index > index && array->right != NULL)
  {
    return SparseArray_getNode(array->right, index);
  }
  
  return NULL;
}
Exemple #9
0
SparseNode * SparseArray_getNode(SparseNode * array, int index)
{

  if(array == NULL)
    {
      return NULL;
    }

   if((array -> index) == index)
    {
      return array;
    }

  if(index < (array -> index))//scrolls left if index is smaller
    {
      return SparseArray_getNode(array -> left, index);
    }
    
  return SparseArray_getNode(array -> right, index);
 }
Exemple #10
0
// gcc -g -Wall -Wshadow -DMYTEST -o answer07 answer07.c && ./answer07
int main(int argc, char * * argv)
{
  int val[8] = {0,1,2,4,1,5,8,3};
  int ind[8] = {5,2,7,1,9,6,4,3};
  printf("\nAbout to run my custom test-cases\n");

  // Test sparsenode create
  printf("\nSparseNode Create\n");
  SparseNode * head = SparseNode_create(7, 5);
  dump(head);
  //Insert
  printf("\nInsert\n");
  head = SparseArray_insert(head,10,3);
  head = SparseArray_insert(head,4,3);
  head = SparseArray_insert(head,1,3);
  head = SparseArray_insert(head,10,5);
  head = SparseArray_insert(head,8,3);
  head = SparseArray_insert(head,12,3);
  head = SparseArray_insert(head,5,3);
  dump(head);
  //Destroy
  printf("\nDestroy\n");
  SparseArray_destroy(head);
  dump(head);
  //Build
  printf("\nBuild\n");
  SparseNode * node;
  node = SparseArray_build(ind,val,8);
  dump(node);
  //Max and Min
  printf("\nMax\n");
  int max = SparseArray_getMax(node);
  int min = SparseArray_getMin(node);
  printf("\nMax = %d Min = %d\n",max,min);
  //Get Node
  printf("\nGet Node\n");
  SparseNode * getNode = SparseArray_getNode(node,7);
  dump(getNode);
  //Remove
  node = SparseArray_remove(node,2);
  printf("\nAfter Remove\n");
  dump(node);
  //Copy
  printf("\nCopy\n");
  dump(node);
  SparseNode * copy = SparseArray_copy(node);
  printf("\nAfter Copy\n");
  dump(copy);
  //Merge
  printf("\nMerge\n");

  return 0;
}
Exemple #11
0
/* Remove a value associated with a particular index from the sparse
 * array. It returns the new
 * sparse array ( binary tree root ). HINT : You will need to isolate
 * several different cases here :
 * - If the array is empty ( NULL ), return NULL
 * - Go left or right if the current node index is different.

 * - If both subtrees are empty, you can just remove the node.

 * - If one subtree is empty, you can just remove the current and
 * replace it with the non - empty child.

 * - If both children exist, you must find the immediate successor of
 * the current node ( leftmost of right branch ), swap its values with
 * the current node ( BOTH index and value ), and then delete the
 * index in the right subtree.
*/
SparseNode * SparseArray_remove ( SparseNode * array, int index )
{
  SparseNode * temp = NULL;
  int temp2 = 0;
  
  if (array == NULL)
  {
    return NULL;
  }
  
  if (array->index > index)
  {
    array->left = SparseArray_remove(array->left, index);
  }
  
  if (array->index < index)
  {
    array->right = SparseArray_remove(array->right, index);
  }
  
  if (array->left == NULL && array->right == NULL)
  {
    free(array);
    return NULL;
  }
  
  if (array->left != NULL && array->right == NULL)
  {
    temp = array->left;
    free(array);
    return temp;
  }
  
  if (array->left == NULL && array->right != NULL)
  {
    temp = array->right;
    free(array);
    return temp;
  }
//this part may be a problem  
  if (array->left != NULL && array->right != NULL)
  {
    temp2 = SparseArray_getMin(array->right);
    temp = SparseArray_getNode(array->right, temp2);
    array->index = temp->index;
    array->value = temp->value;
    free(temp);
    return array;
  }
  return array;
}
Exemple #12
0
/* Retrieve the node associated with a specific index in a sparse
* array tree.
*
* Arguments:
* *array the root node of a sparse array tree
* index the index of the node you want to search
*
* returns:
* SparseNode* the node with the index that you searched from the tree.
* If no node found, NULL should be returned.
*
* Hint: If the given index is smaller than the current
* node, search left ; if it is larger, search right.
*/
SparseNode * SparseArray_getNode(SparseNode * array, int index )
{
    if (array == NULL) 
    { 
      return NULL; 
    } 

    else if (array -> index == index) 
    { 
      return array; 
    } 
    else if (index < array -> index) 
    { 
      array = array -> left; 
      return SparseArray_getNode(array, index); 
    } 
    else if (index > array -> index) 
    { 
      array = array -> right; 
      return SparseArray_getNode(array, index); 
    } 

  return NULL;
}
Exemple #13
0
SparseNode * SparseArray_merge_partition(SparseNode * array, SparseNode * temp)
{
  if(array == NULL){return temp;}
  SparseNode * OPTION_MERGE = SparseArray_getNode(temp, array->index);
  if(OPTION_MERGE == NULL){temp = SparseArray_insert(temp, array->index, array->value);}
  else
    {
      OPTION_MERGE->value = OPTION_MERGE->value + array->value;
      if(OPTION_MERGE->value == 0)
	temp = SparseArray_remove(temp, OPTION_MERGE->index);
    }

  temp = SparseArray_merge_partition(array->left, temp);
  temp = SparseArray_merge_partition(array->right, temp);
  return temp;
}
Exemple #14
0
/* Remove a value associated with a particular index from the sparse
 * array. It returns the new
 * sparse array tree ( binary tree root ). 
 *
 * Arguments:
 * *array         the root node of a sparse array tree
 * index          the index of the node you want to remove
 * 
 * returns:
 * SparseNode*    the root node of the sparse array tree that you just modified
 *          
 *    
 * HINT : First, you need to find that node. Then, you will need to isolate
 * several different cases here :
 * - If the array is empty ( NULL ), return NULL
 * - Go left or right if the current node index is different.

 * - If both subtrees are empty, you can just remove the node.

 * - If one subtree is empty, you can just remove the current and
 * replace it with the non - empty child.

 * - If both children exist, you must find the immediate successor of
 * the current node ( leftmost of right branch ), swap its values with
 * the current node ( BOTH index and value ), and then delete the
 * index in the right subtree.
*/
SparseNode * SparseArray_remove ( SparseNode * array, int index ) {
  if (array == NULL)
    return NULL;
  
  if (array -> index == index) {
    if (array -> left == NULL && array -> right == NULL) {
      free(array);
      return NULL;
    }
    else if (array -> left == NULL) {
      SparseNode* p = array -> right;
      free(array);
      return p;
    }
    else if (array -> right == NULL) {
      SparseNode* p = array -> left;
      free(array);
      return p;
    }
    else {
      int tempValue;
      int successorIndex = SparseArray_getMin(array -> right);
      SparseNode* successor = SparseArray_getNode(array, successorIndex);
      
      //tempIndex = array -> index;
      tempValue = array -> value;
      array -> index = successor -> index;
      array -> value = successor -> value;
      successor -> index = index;
      successor -> value = tempValue;
      
      array -> right = SparseArray_remove(array -> right, index);
      return array;
    }
  }
  
  if (array -> index > index) {
    array -> left = SparseArray_remove(array -> left, index);
    return array;
  }
  else {
    array -> right = SparseArray_remove(array -> right, index);
    return array;
  }
}
Exemple #15
0
/* Remove a value associated with a particular index from the sparse
 * array. It returns the new
 * sparse array tree ( binary tree root ). 
 *
 * Arguments:
 * *array         the root node of a sparse array tree
 * index          the index of the node you want to remove
 * 
 * returns:
 * SparseNode*    the root node of the sparse array tree that you just modified
 *          
 *    
 * HINT : First, you need to find that node. Then, you will need to isolate
 * several different cases here :
 * - If the array is empty ( NULL ), return NULL
 * - Go left or right if the current node index is different.

 * - If both subtrees are empty, you can just remove the node.

 * - If one subtree is empty, you can just remove the current and
 * replace it with the non - empty child.

 * - If both children exist, you must find the immediate successor of
 * the current node ( leftmost of right branch ), swap its values with
 * the current node ( BOTH index and value ), and then delete the
 * index in the right subtree.
*/
SparseNode * SparseArray_remove ( SparseNode * array, int index )
{
    array = SparseArray_getNode(array,index);
    
    // Node does not have any subtrees
    if(((array -> left) == NULL) && ((array -> right) == NULL))
    {
	free(array);
	return NULL;
    }
    
    // Node has only one subtree
    if((array -> left) == NULL)
    {
	SparseNode * t = array -> right;
	free(array);
	return t;
    }
    
    if((array -> right) == NULL)
    {
	SparseNode * t = array -> left;
	free(array);
	return t;
    }
    
    // Node has both subtrees
    SparseNode * n = array -> right; // n must not me be NULL
    while((n -> left) != NULL)
    {
	n = n -> left;
    }
    array -> index = n -> index;
    array -> value = n -> value;
    n -> index = index;
    
    // delete n
    
    array -> right = SparseArray_remove (array -> right,index );
    
    return array ;
}
Exemple #16
0
int main ( int argc , char ** argv )
{
  int i; //index

  //check arguments
  if (argc != 3)
    {
      printf("usage: ./pa04 <input file> <output file>\n");
      return EXIT_FAILURE;
    }
  
  //initializing input file
  FILE * fptr = NULL;
  fptr = fopen(argv[1], "r");
  if (fptr == NULL)
    {
      printf("File error!\n");
      return EXIT_FAILURE;
    }
  /**********************************
   //initialize array 1
   **********************************/
  int number_of_value_1 = 0;
  fscanf(fptr, "%d", &number_of_value_1);
  printf("\nlength: %d\n", number_of_value_1);

  int* indices_1 = malloc(sizeof(int)* number_of_value_1);
  if (indices_1 == NULL)
    {
      printf("Array_1 indices malloc error!\n");
      return EXIT_FAILURE;
    }

  int* values_1 = malloc(sizeof(int)* number_of_value_1);
  if (values_1 == NULL)
    {
      printf("Array_1 values malloc error!\n");
      return EXIT_FAILURE;
    }

  printf("indices: ");

  for (i = 0; i < number_of_value_1; i++)
    {
      fscanf(fptr, "%d", &indices_1[i]);
      printf("%d ", indices_1[i]);
    }

  printf("\nvalues: ");
  for (i = 0; i < number_of_value_1; i++)
    {
      fscanf(fptr, "%d", &values_1[i]);
      printf("%d ", values_1[i]);
    }
  printf("\n");
  
  SparseNode * array_1 = NULL ;
  array_1 = SparseArray_build(indices_1, values_1, number_of_value_1);


  /************************************
   //initialize array 2
   ************************************/
  int number_of_value_2 = 0;
  fscanf(fptr, "%d", &number_of_value_2);
  printf("\nlength: %d\n", number_of_value_2);

  int* indices_2 = malloc(sizeof(int)* number_of_value_2);
  if (indices_2 == NULL)
    {
      printf("Array_2 indices malloc error!\n");
      return EXIT_FAILURE;
    }

  int* values_2 = malloc(sizeof(int)* number_of_value_2);
  if (values_2 == NULL)
    {
      printf("Array_2 values malloc error!\n");
      return EXIT_FAILURE;
    }

  printf("indices: ");
  for (i = 0; i < number_of_value_2; i++)
    {
      fscanf(fptr, "%d", &indices_2[i]);
      printf("%d ", indices_2[i]);
    }

  printf("\nvalues: ");
  for (i = 0; i < number_of_value_2; i++)
    {
      fscanf(fptr, "%d", &values_2[i]);
      printf("%d ", values_2[i]);
    }
   printf("\n");

  SparseNode * array_2 = NULL ;
  array_2 = SparseArray_build(indices_2, values_2, number_of_value_2);
  
  //finilizing the initialization step
  fclose(fptr);
  free(indices_1);
  free(values_1);
  free(indices_2);
  free(values_2);

  /***********************************
   ***********************************
   * Printing results
   ***********************************
   **********************************/
  SparseNode * onenode = NULL;
  
  fptr = fopen(argv[2], "w");
  if (fptr == NULL){
    printf("output file error\n");
    return EXIT_FAILURE;
  }

  /********************************************************
   * array 1 and array 2
   ********************************************************/

  printf("*******************************\n");
  printf("Array 1\n");
  printf("*******************************\n");
  if (array_1 != NULL)
    {
      for (i = SparseArray_getMin(array_1); 
	   i <= SparseArray_getMax(array_1); i++) 
	{
	  onenode = SparseArray_getNode(array_1,i);
	  if (onenode != NULL)
	    {
	      printf("%5d: %6d\n" , i , onenode->value);
	    }
	}
    }

  printf("\n*******************************\n");
  printf("Array 2\n");
  printf("*******************************\n");
  if (array_2 != NULL){
    for (i = SparseArray_getMin(array_2); 
	 i <= SparseArray_getMax(array_2); i++) 
      {
	onenode = SparseArray_getNode(array_2,i);
	if (onenode != NULL)
	  {
	    printf("%5d: %6d\n" , i , onenode->value);
	  }
      }
  }
  printf("\n*******************************\n");

  
  /********************************************************
   * copy array
   ********************************************************/
  
  SparseNode * copy = SparseArray_copy(array_1);

  printf("Array copy\n");
  printf("*******************************\n");
  if (copy != NULL){
    for (i = SparseArray_getMin(copy);
	 i <= SparseArray_getMax(copy); i++) 
      {
	onenode = SparseArray_getNode(copy,i);
	if (onenode != NULL)
	  {
	    printf("%5d: %6d\n" , i , onenode->value);
	  }
      }
  }
  printf("\n*******************************\n");
  
  /********************************************************
   * doing addition
   ********************************************************/

  SparseNode * array_new = NULL;
  array_new = SparseArray_merge(array_1, array_2);
  printf("Array result\n");
  printf("*******************************\n");
  if (array_new != NULL){
    for (i = SparseArray_getMin(array_new); 
	 i <= SparseArray_getMax(array_new); i++) {
      onenode = SparseArray_getNode(array_new,i);
      if (onenode != NULL)
	{
	  printf("%5d: %6d\n" , i , onenode->value);
	  fprintf(fptr, "%5d: %6d\n" , i , onenode->value);
	}
    }
  }
  printf("\n*******************************\n");

  

  SparseArray_destroy ( array_1 );
  SparseArray_destroy ( array_2 );
  SparseArray_destroy ( array_new );
  SparseArray_destroy ( copy );
  fclose(fptr);
  return EXIT_SUCCESS ;
}