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.
相關文章
- E. Data Structures FanStruct
- CRICOS Data Structures and AlgorithmsHash TablesStructGo
- CF 2100-2400 data structures 亂做Struct
- Linux Data Structures(Linux資料結構)(轉)LinuxStruct資料結構
- Packt.PHP.7.Data.Structures.and.Algorithms.2017.5.epubPHPStructGo
- JBoss Data Grid 6釋出
- Physical Storage StructuresStruct
- Oracle Database Memory StructuresOracleDatabaseStruct
- B. Suffix StructuresStruct
- [CareerCup] 10.2 Data Structures for Large Social Network 大型社交網站的資料結構Struct網站資料結構
- 基於 Angular Material 的 Data Grid 設計實現Angular
- sentence structures經典句型Struct
- operating-system structuresStruct
- 核心編譯part5編譯
- 【DataGuard】使用Grid Control快速部署Oracle物理Data GuardOracle
- grid control Error in getting data for creating new listenerError
- Introduction to Oracle Memory Structures (69)OracleStruct
- linux grub loading stage2 解決辦法Linux
- django入門-測試-part5Django
- Android文字時鐘 — Part5Android
- Rman Backups When The Directory Structures Are DifferentStruct
- 編譯實踐學習 Part5編譯
- [譯] part 16: golang 結構體structuresGolang結構體Struct
- 【DataGuard】使用Grid Control對Oracle物理Data Guard進行Switchover切換Oracle
- 【DataGuard】使用Grid Control對Oracle物理Data Guard進行健康檢查Oracle
- 【DataGuard】使用Grid Control調整Oracle物理Data Guard資料保護模式Oracle模式
- 《資料結構的C++偽碼實現》(《DATA STRUCTURES A Pseudocode Approach with C++》)讀書筆記(一) (轉)資料結構C++StructAPP筆記
- 《資料結構的C++偽碼實現》(《DATA STRUCTURES A Pseudocode Approach with C++》)讀書筆記(三) (轉)資料結構C++StructAPP筆記
- 《資料結構的C++偽碼實現》(《DATA STRUCTURES A Pseudocode Approach with C++》)讀書筆記(四) (轉)資料結構C++StructAPP筆記
- 【DataGuard】使用Grid Control調整Oracle物理Data Guard備庫為Read OnlyOracle
- Day17 二叉樹Part5二叉樹
- Day31 貪心演算法part5演算法
- grid
- SCM600---Enterprise Structures in Sales and DistributionStruct
- ViCANdo新版本釋出(PART5)| JavaScript指令碼JavaScript指令碼
- 《PyTorch》Part5 PyTorch之遷移學習PyTorch遷移學習
- Oracle 19c Concepts(11):Physical Storage StructuresOracleStruct
- C語言中Pointer, Array,String and Structures的區別C語言Struct