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; }
SparseNode * SparseArray_add ( SparseNode * array, int index, int value ) { if (value == 0) { return NULL; } if (array == NULL) { return SparseNode_create(index, value); } if (index == (array->index)) { array->value = value; return array; } if (index < (array->index)) { array->left = SparseArray_add(array->left,index,value); } else { array->right = SparseArray_add(array->right,index,value); } return array; }
SparseNode *SparseArray_build(int * indicies, int * values, int length) { SparseNode * array = NULL; int i = 0; for (i = 0; i < length; i++) { array = SparseArray_add(array, indicies[i], values[i]); } return array; }
//Builds array with given length SparseNode * SparseArray_build(int * indicies, int * values, int length) { SparseNode * array = NULL; int ct; for(ct = 0; ct < length; ct++) { array = SparseArray_add(array, indicies[ct], values[ct]); } return array; }
SparseNode * SparseArray_add ( SparseNode * array, int index, int value ) { if (array == NULL) { array = SparseNode_create(index, value); return array; } if (array->index > index) { SparseArray_add (array->left, index, value); } else if (array->index < index) { SparseArray_add (array->right, index, value); } else if (array->index == index) { array->value = value; } return array; }
SparseNode * SparseArray_add (SparseNode * array, int index, int value) { if(value == 0) { return array; } if(array == NULL) { return SparseNode_create(index, value);//creates index if NULL found } if(index == (array -> index))//returns value into existing index { array -> value = value; return array; } if(index < (array -> index))//scrolls left creating indices as it goes { if(array -> left == NULL) { array -> left = SparseNode_create(index, value); } else { array -> left = SparseArray_add(array -> left, index, value); } } else//scrolls right creating indices as it goes { if(array -> right == NULL) { array -> right = SparseNode_create(index, value); } else { array -> right = SparseArray_add(array -> right, index, value); } } return array; }
SparseNode *SparseArray_build(int * indicies, int * values, int length) { if (length == 0) { return NULL; } SparseNode * array = NULL; int j = 0; array = SparseNode_create(indicies[0], values[0]); while ((array == NULL) && (j<length)) { array = SparseNode_create(indicies[j], values[j]); j++; } int i = 0; for (i = j; i < length; i++) { SparseArray_add(array, indicies[i], values[i]); } return array; }
SparseNode * SparseArray_copy(SparseNode * array) { if(array == NULL) { return NULL; } SparseNode * copy = NULL; copy = SparseArray_add(copy, array -> index, array -> value); copy -> right = SparseArray_copy(array -> right); copy -> left = SparseArray_copy(array -> left); return copy; }