Stage2 Part5:Grid Data Structures

奉圖發表於2020-10-25

Set 10

  • Where is the isValid method specified? Which classes provide an implementation of this method?
    answer: In the Grid interface. The BoundedGrid and UnboundedGrid classes both implement this method.
    在這裡插入圖片描述(from Grid.java)
    在這裡插入圖片描述(from BoundedGrid.java)
    在這裡插入圖片描述(from UnboundedGrid.java)

  • Which AbstractGrid methods call the isValid method? Why don’t the other methods need to call it?
    answer: getValidAdjacentLocations method. Other methods calls the getValidAdjacentLocations method to call the isValid method.
    在這裡插入圖片描述(from AbstractGrid.java)

  • Which methods of the Grid interface are called in the getNeighbors method? Which classes provide implementations of these methods?
    answer: getNeighbors calls the get method and getOccupiedAdjacentLocations method. BoundedGrid class and UnboundedGrid class implement the get method. AbstractGrid class implements the getOccupiedAdjacentLocations method.
    在這裡插入圖片描述(from AbstractGrid.java)

  • Why must the get method, which returns an object of type E, be used in the getEmptyAdjacentLocations method when this method returns locations, not objects of type E?
    answer: The get method returns a reference to the object stored in the grid at the given location or null if no object exists. So get method must be used to test if the neighbor location is null.
    在這裡插入圖片描述(from Grid.java)

  • What would be the effect of replacing the constant Location.HALF_RIGHT with Location.RIGHT in the two places where it occurs in the getValidAdjacentLocations method?
    answer: Only north, east, south and west four derections neighbor could be found.

Set 11

  • What ensures that a grid has at least one valid location?
    answer: The col and row must more bigger than 0, so the grid has at least one valid location.
    在這裡插入圖片描述(from BoundedGrid.java)

  • How is the number of columns in the grid determined by the getNumCols method? What assumption about the grid makes this possible?
    answer: occupantArray[0].length
    在這裡插入圖片描述(from BoundedGrid.java)

  • What are the requirements for a Location to be valid in a BoundedGrid?
    answer: 0 <= location.row < row and 0 <= location.col < col
    在這裡插入圖片描述(from BoundedGrid.java)

In the next four questions, let r = number of rows, c = number of columns, and n = number of occupied locations.

  • What type is returned by the getOccupiedLocations method? What is the time complexity (Big-Oh) for this method?
    answer: ArrayList. O(r * c), because all locations imust be tested.
    在這裡插入圖片描述(from BoundedGrid.java)

  • What type is returned by the get method? What parameter is needed? What is the time complexity (Big-Oh) for this method?
    answer: Return type is E. Paramater is Location. O(1).
    在這裡插入圖片描述(from BoundedGrid.java)

  • What conditions may cause an exception to be thrown by the put method? What is the time complexity (Big-Oh) for this method?
    answer: The object is null or the location is invalid. O(1).
    在這裡插入圖片描述(from BoundedGrid.java)

  • What type is returned by the remove method? What happens when an attempt is made to remove an item from an empty location? What is the time complexity (Big-Oh) for this method?
    answer: Return type is E. If an attempt is made to remove an item from an empty location, null is stored in the location and null is returned. O(1).
    在這裡插入圖片描述(from BoundedGrid.java)

  • Based on the answers to questions 4, 5, 6, and 7, would you consider this an efficient implementation? Justify your answer.
    answer: Yes. The time complexity is little.

Set 12

  • Which method must the Location class implement so that an instance of HashMap can be used for the map? What would be required of the Location class if a TreeMap were used instead? Does Location satisfy these requirements?
    answer: The Location class must implement the hashCode and the equals methods. The TreeMap requires keys of the map to be Comparable. The Location class satisfies all of these requirements.

  • Why are the checks for null included in the get, put, and remove methods? Why are no such checks included in the corresponding methods for the BoundedGrid?
    answer: Because the isValid method in the UnboundedGrid always return true and the null location in UnboundedGrid id invalid. So these methods must check for null. Null check included in the isValid method in BoundedGrid actually. If the location parameter is null in the isValid method, an attempt to access loc.getRow() will cause a NullPointerException to be thrown.
    在這裡插入圖片描述(from BoundedGrid.java)

  • What is the average time complexity (Big-Oh) for the three methods: get, put, and remove? What would it be if a TreeMap were used instead of a HashMap?
    answer: O(1). If a TreeMap were used instead of a HashMap, the average time complexity would be O(log n), where n is the number of occupied locations in the grid.

  • How would the behavior of this class differ, aside from time complexity, if a TreeMap were used instead of a HashMap?
    answer: Most of the time, the getOccupiedLocations method will return the occupants in a different order. Keys (locations) in a HashMap are placed in a hash table based on an index that is calculated by using the keys’ hashCode and the size of the table. The order in which a key is visited when the keySet is
    traversed depends on where it is located in the hash table. A TreeMap stores its keys in a balanced binary search tree and traverses this tree with an inorder traversal. The keys in the keySet (Locations) will be visited in ascending order (row major order) as defined by Location’s compareTo method.

  • Could a map implementation be used for a bounded grid? What advantage, if any, would the two-dimensional array implementation that is used by the BoundedGrid class have over a map implementation?
    answer: Yes, a map could be used to implement a bounded grid. If a HashMap were used to implement the bounded grid, the average time complexity for getOccupiedLocations would be O(n), where n is the number of items in the grid. If the bounded grid were almost full, the map implementation would use more memory, because the map stores the item and its location. A two-dimensional array only stores the items. The locations are produced by combining the row and column indices for each item.

相關文章