|
6 | 6 |
|
7 | 7 | /** |
8 | 8 | * This is the implementation of Hashed Array Tree (HAT) data structure that is the same as Dynamic |
9 | | - * Array (ArrayList in Java) in terms of time complexity, however, it wastes SQRT(N) extra space |
| 9 | + * Array (ArrayList in Java) in terms of time complexity, however, it wastes O(SQRT(N)) extra space |
10 | 10 | * when increasing capacity. |
| 11 | + * |
| 12 | + * Although name implies hashing, HAT does not use any hash. |
| 13 | + * |
| 14 | + * Initially, we start with base array that stores only references to leaf arrays whose size is |
| 15 | + * the same as base array. Every time current leaf array is full we create new leaf array and start |
| 16 | + * to insert data into newly created leaf array. |
| 17 | + * |
| 18 | + * When increasing capacity, we double size of base array and leaf create leaf arrays with doubles |
| 19 | + * size and copy entries from previous HAT. In this way, only quarter of newly created HAT is full |
| 20 | + * and other leaf arrays will be null. |
11 | 21 | */ |
12 | 22 | public class HashedArrayTree { |
13 | 23 |
|
14 | | - private Object[][] data; |
| 24 | + private Object[][] data; // base array, used to store references to leaf arrays |
15 | 25 |
|
16 | 26 | private int size; |
17 | 27 | private int iPointer = -1; |
18 | | - private int jPointer = 17; // negative value means leaf array has not been created; |
| 28 | + private int jPointer = 17; |
19 | 29 |
|
20 | 30 | public HashedArrayTree() { |
21 | | - this.data = new Object[4][]; // should be power of 2, all leaf arrays are not initialized |
| 31 | + this.data = new Object[16][]; // should be power of 2, all leaf arrays are not initialized |
22 | 32 | } |
23 | 33 |
|
24 | 34 | public void add(Integer elem) { |
@@ -62,6 +72,9 @@ private void ensureCapacity() { |
62 | 72 |
|
63 | 73 | } |
64 | 74 |
|
| 75 | + /** |
| 76 | + * helper method to see capacity and number of entries in HAT |
| 77 | + */ |
65 | 78 | public void printStats() { |
66 | 79 | int busy = 0; |
67 | 80 | int cap = 0; |
@@ -93,6 +106,10 @@ public int size() { |
93 | 106 | return size; |
94 | 107 | } |
95 | 108 |
|
| 109 | + /** |
| 110 | + * returns available leaf to insert data. |
| 111 | + * if current leaf is already full,creates new leaf and returns it |
| 112 | + */ |
96 | 113 | private Object[] getLeaf() { |
97 | 114 |
|
98 | 115 | if (jPointer >= data.length) { |
|
0 commit comments