/* 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 ) { if (array == NULL) { return NULL; } if (index < (array->index)) { array->left = SparseArray_remove(array->left, index); return array; } if (index > (array->index)) { array->right = SparseArray_remove (array->right, index); } if (((array->left) == NULL) && ((array->right) == NULL)) { free(array); return NULL; } if ((array->left) == NULL) { SparseNode * rightside = array->right; free(array); return rightside; } if ((array->right) == NULL) { SparseNode * leftside = array->left; free(array); return leftside; } SparseNode * temp = array->right; while ((temp->left) != NULL) { temp = temp->left; } array->value = temp->value; array->index = temp->index; temp->index = index; array->right = SparseArray_remove(array->right,index); return array; }
/* 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 (index < (array -> index)) { array -> left = SparseArray_remove(array->left, index); return array; } if (index > (array -> index)) { array -> right = SparseArray_remove(array->right, index); return array; } // both NULL, no children if ((array -> left == NULL) && (array -> right == NULL)) { free(array); return NULL; } // left NULL if ((array -> left)==NULL) { SparseNode * temp = array -> right; free(array); return temp; } // right NULL if ((array -> right)==NULL) { SparseNode * temp = array -> left; free(array); return temp; } // find immediate successor SparseNode * temp = array -> right; while((temp->left)!=NULL) { temp = temp -> left; } int val = array -> value; array -> index = temp -> index; array -> value = temp -> value; temp -> index = index; temp -> value = val; array -> right = SparseArray_remove(array -> right, index); return array; }
/* 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(index < (array -> index)) { array -> right = SparseArray_remove(array -> right, index); return array; } if(index > (array -> index)) { array -> left = SparseArray_remove(array -> left, index); return array; } if(((array -> left) == NULL) && ((array -> right) == NULL)) { free(array); return NULL; } if((array -> left) == NULL) { SparseNode * RightNode = array -> right; free(array); return RightNode; } if((array -> right) == NULL) { SparseNode * LeftNode = array -> left; free(array); return LeftNode; } SparseNode * children = array -> right; while((children -> left) != NULL) { children = children -> left; } array -> value = children -> value; array -> index = children -> index; children -> index = index; array -> right = SparseArray_remove(array -> right, index); return array ; }
/* 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; }
SparseNode * SparseArray_remove ( SparseNode * array, int index ) { if(array == NULL) { return NULL; } if(index < array -> index) { array -> left = SparseArray_remove(array -> left, index); return array; } if(index > array -> index) { array -> right = SparseArray_remove(array -> right, index); return array; } if(((array -> left) == NULL) && ((array -> right) == NULL)) { free(array); return NULL; } if((array -> left) == NULL) { SparseNode * rc = array -> right; free(array); return rc; } if((array -> right) == NULL) { SparseNode * lc = array -> left; free(array); return lc; } SparseNode * scsr = array -> right; while((scsr -> left) != NULL) { scsr = scsr -> left; } array -> index = scsr -> index; scsr -> index = index; array -> value = scsr -> value; array -> right = SparseArray_remove(array -> right, index); return array ; }
/* 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 (index < array -> index) { array -> left = SparseArray_remove(array -> left, index); return array; } if (index > (array -> index)) { array -> right = SparseArray_remove(array -> right, index); return array; } if (((array -> left) == NULL) && ((array -> right) == NULL)) { free (array); return NULL; } if ((array -> left) == NULL) { SparseNode * Right_Node = array -> right; free (array); return Right_Node; } if ((array -> right) == NULL) { SparseNode * Left_Node = array -> left; free (array); return Left_Node; } SparseNode *Traverse = array -> right; while (Traverse -> left != NULL) { Traverse = Traverse -> left; } array -> index = Traverse -> index; array -> value = Traverse -> value; Traverse -> index = index; array -> right = SparseArray_remove(array -> right, index); return array ; }
/* 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; }
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; }
/* 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; } }
SparseNode * SparseArray_remove(SparseNode * array, int index) { if (array == NULL) return NULL; if ((array -> index) < index) { array -> right = SparseArray_remove(array -> right, index); return array; } if ((array -> index) > index) { array -> left = SparseArray_remove(array -> left, index); return array; } if (((array -> left) == NULL) && ((array -> right) == NULL)) { free(array); return NULL; } if ((array -> left) == NULL) { SparseNode * sn = array -> right; free(array); return sn; } if ((array -> right) == NULL) { SparseNode * sn = array -> left; free(array); return sn; } SparseNode * p = array -> right; while ((p -> left) != NULL) { p = p -> left; } int t = p -> index; p -> index = array -> index; array-> index = t; array -> right = SparseArray_remove(array-> right, index); return array; }
// 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; }
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; }
SparseNode * Merge_add ( SparseNode * array, int index, int value ) { if (array == NULL) { return SparseNode_create(index, value); } if (index < array->index) { array->left = Merge_add(array->left, index, value); } if (index > array->index) { array-> right = Merge_add(array->right, index, value); } if (index == array->index) { array->value += value; if (array->value == 0) { array = SparseArray_remove(array, index); } } return array ; }
SparseNode * SparseArray_remove(SparseNode * array, int index) { if(array == NULL){return NULL;} if(array->index == index) { if(array->right == NULL && array-> left == NULL) { free(array); return NULL; } if(array->left == NULL) { SparseNode * node_NEW = array->right; free(array); return node_NEW; } if(array->right == NULL) { SparseNode * node_NEW = array->left; free(array); return node_NEW; } SparseNode * node_NEW = SparseArray_remove_partition(array->right); array->index = node_NEW->index; array->value = node_NEW->value; array->right = SparseArray_remove(array->right, array->index); return array; } if(index > array->index) { array->right = SparseArray_remove(array->right, index); return array; } array->left = SparseArray_remove(array->left, index); return array; }
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; }
int tests_SparseArray_remove(int test_number) { int n_tests = 8; // If test_number is out of range, then... if(test_number < 0 || test_number >= n_tests) { //return how many distinct test-cases we have. return n_tests; } // Assume failure unless otherwise noted int success = FALSE; printf("Testing: SparseNode_remove(a%d)\n", test_number); SparseNode * sol_node = NULL; SparseNode * stu_node = NULL; //build the arrays for removal sol_node = make_array(test_number); stu_node = make_array(test_number); printf("Built array a%d:\n", test_number); print_array(sol_node, 0); printf("Remove Node with index %d:\n", test_number+test_number); stu_node = SparseArray_remove(stu_node, test_number+test_number); sol_node = SparseArray_remove_sol(sol_node, test_number+test_number); printf("Student result after remove:\n"); print_array(stu_node, 0); printf("Solution result after remove:\n"); print_array(sol_node, 0); printf("Checking if result tree is the same...\n"); if(cmp_array(stu_node, sol_node)) { success = TRUE; } // Cleanup if(sol_node) { SparseArray_destroy_sol(sol_node); } if(stu_node) { SparseArray_destroy_sol(stu_node); } return success; }
/* 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 ; }
SparseNode * SparseArray_mergeinsert(SparseNode * array1, int index, int value) { //int newvalue = 0; if(array1 == NULL) { return SparseNode_create(index, value); } if (array1 -> index > index) { array1 -> left = SparseArray_mergeinsert(array1 -> left, index, value); return array1; } if (array1 -> index < index) { array1 -> right = SparseArray_mergeinsert(array1 -> right, index, value); return array1; } if (array1 -> index == index) { array1->value += value; if(array1 -> value == 0) { array1 = SparseArray_remove(array1, array1 -> index); } return array1; } return 0; }
SparseNode * Merge_Insert (SparseNode * Tree_Array, int index, int value) { if (value == 0) { return Tree_Array; } if (Tree_Array == NULL) { return SparseNode_create(index, value); } if (Tree_Array -> index == index) { Tree_Array -> value = value + Tree_Array -> value; if(Tree_Array -> value == 0) { Tree_Array = SparseArray_remove(Tree_Array, Tree_Array -> index); } return Tree_Array; } if (Tree_Array -> index > index) { Tree_Array -> left = Merge_Insert (Tree_Array -> left, index, value); } else { Tree_Array -> right = Merge_Insert (Tree_Array -> right, index, value); } return Tree_Array; }