README 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. Assignment PA04: Sparse Array
  2. Consider a data structure called a sparse array: most elements of a
  3. sparse array are 0. This means that most elements do not need to be
  4. stored in the array. Only the elements whose values are non-zero are
  5. stored. In a C program, a normal array of size N occupies a contiguous
  6. piece of memory allocated for indexes from 0 to N − 1. It would be
  7. inefficient to store a sparse array in a normal C array because most
  8. space is wasted. Instead, you can use a data structure that stores
  9. only non-zero elements.
  10. In this question, you will implement a sparse array using a binary
  11. search tree and add two sparse array. Each tree node has two integers
  12. storing the index and the value. The binary search tree is built based
  13. on the indexes:
  14. -- If a node’s index is smaller than the parent’s index, this node is
  15. on the left side.
  16. -- If a node’s index is larger than the parent’s index, this node is
  17. on the right side.
  18. -- The indexes in the whole tree are distinct: no two nodes have the
  19. same index. It is possible to have two nodes of the same value (but
  20. different indexes).
  21. When merging two sparse array, array_1 and array_2:
  22. 1. The contents in array_1 and array_2 should not be changed. You
  23. should create a new array that stores the result of merging.
  24. 2. array_2 will be merged to array_1. This means you need to read
  25. nodes in array_2 and insert them into array_1.
  26. 3. You need to use post-order to traverse the array_2 tree.
  27. 4. Values of two nodes need to be added only when the indices are the
  28. same.
  29. 5. A node with value of 0 should be removed.
  30. 6. if array_2 have nodes with index different than any nodes in
  31. array_1, you should insert those nodes into array_1.
  32. Example:
  33. Consider the following indices and corresponding values:
  34. array_1:
  35. int indices_1 [ NUM_VALUES_1 ] = {1 , 2 , 3 , 7 , 8 , 9 };
  36. int values_1 [ NUM_VALUES_1 ] = {3 , 2 , -1 , -9 , -5 , 3 };
  37. array_2:
  38. int indices_2 [ NUM_VALUES_2 ] = {0 , 1 , 2 , 3 , 4 , 7 , 9};
  39. int values_2 [ NUM_VALUES_2 ] = {5 , -3 , -8 , 1 , 7 , 9 , 1};
  40. results:
  41. int indices_3 [ NUM_VALUES_3 ] = { 0 , 2 , 4 , 8 , 9 };
  42. int values_3 [ NUM_VALUES_3 ] = { 5 , -6 , 7 ,-5 , 4 };
  43. (elements whose values are zero are deleted)
  44. Another hint: You may need to write your own functions.
  45. About outputs:
  46. The output of pa04 only contains the final output, which means make test only works when all the function has been done.
  47. However, you can check your trees by "make testxx". (xx means some number, such as 01, 02) Array1 and Array2 will be printed out in "debug_files/", but this only works when you finished all functions except SparseArray_merge and SparseArray_copy.