Stage2 Part5:Grid Data Structures
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.
相關文章
- CRICOS Data Structures and AlgorithmsHash TablesStructGo
- E. Data Structures FanStruct
- COMP4134 Algorithms and Data StructuresGoStruct
- CF 2100-2400 data structures 亂做Struct
- Packt.PHP.7.Data.Structures.and.Algorithms.2017.5.epubPHPStructGo
- 基於 Angular Material 的 Data Grid 設計實現Angular
- operating-system structuresStruct
- grid
- [譯] part 16: golang 結構體structuresGolang結構體Struct
- RAC and Grid
- Grid Points
- Oracle 19c Concepts(11):Physical Storage StructuresOracleStruct
- Oracle 19c Concepts(12):Logical Storage StructuresOracleStruct
- 編譯實踐學習 Part5編譯
- CKKS Part5: CKKS的重縮放
- 響應式UI部件集DevExtreme v20.2新版亮點:增強Data Grid、Tree List功能UIdevREM
- export GRID_HOME=/u01/app/19c/grid $GRID_HOME/bin/crExportAPP
- grid佈局
- Shichikuji and Power Grid
- C語言中Pointer, Array,String and Structures的區別C語言Struct
- 《PyTorch》Part5 PyTorch之遷移學習PyTorch遷移學習
- Day17 二叉樹Part5二叉樹
- Tekla Structures 2022,鋼結構詳圖設計Struct
- use-magic-grid:magic-grid 庫的官方 React 埠React
- Day31 貪心演算法part5演算法
- G. GCD on a gridGC
- 【WPF】Grid的用法
- CSS Grid 佈局CSS
- EASE-Grid投影
- Beyond Fixed Grid: Learning Geometric Image Representation with a Deformable Grid——論文閱讀ORM
- ViCANdo新版本釋出(PART5)| JavaScript指令碼JavaScript指令碼
- 【GRID】Grid Infrastructure 啟動的五大問題 (Doc ID 1526147.1)ASTStruct
- Big Data and Data Warehousing
- 12.2.0.1.0 Grid RU安裝
- 12.2.0.1 Grid RUR 安裝
- 12.2 Grid RUR 安裝
- SAP: 建立ALV GRID容器
- Kendo UI:Grid 表格元件UI元件