answer04.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "pa04.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /* Create a single instance of a sparse array node with a specific
  5. * index and value. This is a constructor function that allocates
  6. * memory, copies the integer values, and sets the subtree pointers to
  7. * NULL.
  8. */
  9. SparseNode *SparseNode_create(int index, int value)
  10. {
  11. SparseNode * one_node = NULL;
  12. return one_node;
  13. }
  14. /* Add a particular value into a sparse array on a particular index.
  15. * The sparse array uses the index as a key in a binary search tree.
  16. * It returns the new sparse array root
  17. * as its return value. If the index does not exist, create it.
  18. * If the index exists, REPLACE the value to the new one. Use the index to
  19. * determine whether to go left or right in the tree (smaller index
  20. * values than the current one go left, larger ones go right).
  21. */
  22. SparseNode * SparseArray_add ( SparseNode * array, int index, int value )
  23. {
  24. return array ;
  25. }
  26. /* Build a sparse array from given indices and values with specific length.
  27. * This function takes an array of indices, an array of values, and
  28. * the length as inputs.
  29. * It returns a sparse array.
  30. * You need to insert tree nodes in order:
  31. * the first sparse array node contains indices[0] and values[0])
  32. */
  33. SparseNode *SparseArray_build(int * indicies, int * values, int length)
  34. {
  35. SparseNode * array = NULL;
  36. return array;
  37. }
  38. /* Destroy an entire sparse array.
  39. * traversing the binary tree in postorder. Use the
  40. * SparseNode_destroy () function to destroy each node by itself.
  41. */
  42. void SparseArray_destroy ( SparseNode * array )
  43. {
  44. }
  45. /* Retrieve the smallest index in the sparse array.
  46. */
  47. int SparseArray_getMin ( SparseNode * array )
  48. {
  49. return 0;
  50. }
  51. /* Retrieve the largest index in the sparse array.
  52. */
  53. int SparseArray_getMax ( SparseNode * array )
  54. {
  55. return 0;
  56. }
  57. /* Retrieve the node associated with a specific index in a sparse
  58. * array. It returns the value
  59. * associated with the index. If the index does not exist in the
  60. * array, it returns NULL. If the given index is smaller than the current
  61. * node, search left ; if it is larger, search right.
  62. */
  63. SparseNode * SparseArray_getNode(SparseNode * array, int index )
  64. {
  65. return NULL;
  66. }
  67. /* Remove a value associated with a particular index from the sparse
  68. * array. It returns the new
  69. * sparse array ( binary tree root ). HINT : You will need to isolate
  70. * several different cases here :
  71. * - If the array is empty ( NULL ), return NULL
  72. * - Go left or right if the current node index is different.
  73. * - If both subtrees are empty, you can just remove the node.
  74. * - If one subtree is empty, you can just remove the current and
  75. * replace it with the non - empty child.
  76. * - If both children exist, you must find the immediate successor of
  77. * the current node ( leftmost of right branch ), swap its values with
  78. * the current node ( BOTH index and value ), and then delete the
  79. * index in the right subtree.
  80. */
  81. SparseNode * SparseArray_remove ( SparseNode * array, int index )
  82. {
  83. return array ;
  84. }
  85. /* The function makes a copy of the input sparse array
  86. * and it returns a new copy.
  87. */
  88. SparseNode * SparseArray_copy(SparseNode * array)
  89. {
  90. return NULL;
  91. }
  92. /* Merge array_1 and array_2, and return the result array.
  93. * This function WILL NOT CHANGE the contents in array_1 and array_2.
  94. * When merging two sparse array:
  95. * 1. The contents in array_1 and array_2 should not be changed. You should make
  96. * a copy of array_1, and do merging in this copy.
  97. * 2. array_2 will be merged to array_1. This means you need to read nodes in
  98. * array_2 and insert them into array_1.
  99. * 3. You need to use POST-ORDER to traverse the array_2 tree.
  100. * 4. Values of two nodes need to be added only when the indices are the same.
  101. * 5. A node with value of 0 should be removed.
  102. * 6. if array_2 has nodes with index different than any nodes in array_1, you
  103. * should insert those nodes into array_1.
  104. *
  105. */
  106. SparseNode * SparseArray_merge(SparseNode * array_1, SparseNode * array_2)
  107. {
  108. return NULL;
  109. }