Skip to content

Commit f646fd5

Browse files
committed
Hashed Array Tree
1 parent 46a2366 commit f646fd5

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎src/ds/HashedArrayTree.java‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,29 @@
66

77
/**
88
* 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
1010
* 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.
1121
*/
1222
public class HashedArrayTree {
1323

14-
private Object[][] data;
24+
private Object[][] data; // base array, used to store references to leaf arrays
1525

1626
private int size;
1727
private int iPointer = -1;
18-
private int jPointer = 17; // negative value means leaf array has not been created;
28+
private int jPointer = 17;
1929

2030
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
2232
}
2333

2434
public void add(Integer elem) {
@@ -62,6 +72,9 @@ private void ensureCapacity() {
6272

6373
}
6474

75+
/**
76+
* helper method to see capacity and number of entries in HAT
77+
*/
6578
public void printStats() {
6679
int busy = 0;
6780
int cap = 0;
@@ -93,6 +106,10 @@ public int size() {
93106
return size;
94107
}
95108

109+
/**
110+
* returns available leaf to insert data.
111+
* if current leaf is already full,creates new leaf and returns it
112+
*/
96113
private Object[] getLeaf() {
97114

98115
if (jPointer >= data.length) {

‎src/ds/SkipList.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ds;
2+
3+
/**
4+
* Created by sherxon on 6/4/17.
5+
*/
6+
public class SkipList {
7+
8+
9+
private class SkipNode {
10+
11+
SkipNode left, right, up, down;
12+
13+
}
14+
}

0 commit comments

Comments
 (0)