@@ -47,15 +47,82 @@ public void put(K key, V value) throws Exception {
4747 this .bucketArray [baseIndex ] = bucket ;
4848 this .size ++;
4949 } else {
50- int findAt = bucket .find (pairToBeAdded );
51- if (findAt == -1 ) {
50+ int foundIndex = bucket .find (pairToBeAdded );
51+ if (foundIndex == -1 ) {
5252 bucket .addLast (pairToBeAdded );
5353 this .size ++;
5454 } else {
55- HTPair pair = bucket .getAt (findAt );
55+ HTPair pair = bucket .getAt (foundIndex );
5656 pair .value = value ;
5757 }
5858 }
59+
60+ // to make it constant time operation
61+
62+ double lambda = (this .size * 1.0 ) / this .bucketArray .length ;
63+ if (lambda > 0.75 ) {
64+ this .rehash ();
65+ }
66+ }
67+
68+ private void rehash () throws Exception {
69+ GenericLinkedList <HTPair >[] oldBucketArray = this .bucketArray ;
70+ this .bucketArray = (GenericLinkedList <HTPair >[]) new GenericLinkedList [this .bucketArray .length * 2 ];
71+ this .size = 0 ;
72+
73+ for (GenericLinkedList <HTPair > pair : oldBucketArray ) {
74+ while (pair != null && !pair .isEmpty ()) {
75+ HTPair currentPair = pair .removeFirst ();
76+ this .put (currentPair .key , currentPair .value );
77+ }
78+ }
79+
80+ }
81+
82+ // O(1) Time
83+ private int hashFunction (K key ) {
84+ int hashCode = key .hashCode ();
85+ hashCode = Math .abs (hashCode );
86+ int hashIndex = hashCode % this .bucketArray .length ;
87+ return hashIndex ;
88+ }
89+
90+ public V get (K key ) throws Exception {
91+ int hashIndex = hashFunction (key );
92+ GenericLinkedList <HTPair > bucket = this .bucketArray [hashIndex ];
93+ HTPair pairToFind = new HTPair (key , null );
94+
95+ if (bucket == null ) {
96+ return null ;
97+ } else {
98+ int foundIndex = bucket .find (pairToFind );
99+ if (foundIndex == -1 ) {
100+ return null ;
101+ } else {
102+ HTPair pair = bucket .getAt (foundIndex );
103+ return pair .value ;
104+ }
105+ }
106+ }
107+
108+ public V remove (K key ) throws Exception {
109+ int hashIndex = hashFunction (key );
110+ GenericLinkedList <HTPair > bucket = this .bucketArray [hashIndex ];
111+ HTPair pairToRemove = new HTPair (key , null );
112+
113+ if (bucket == null ) {
114+ return null ;
115+ } else {
116+ int foundIndex = bucket .find (pairToRemove );
117+ if (foundIndex == -1 ) {
118+ return null ;
119+ } else {
120+ HTPair pair = bucket .getAt (foundIndex );
121+ bucket .removeAt (foundIndex );
122+ this .size --;
123+ return pair .value ;
124+ }
125+ }
59126 }
60127
61128 public void display () {
@@ -64,16 +131,9 @@ public void display() {
64131 bucket .display ();
65132 } else {
66133 System .out .println ("NULL" );
67- // System.out.println("***********************************");
68134 }
69135 }
70136 System .out .println ("----------------------------------------------" );
71137 }
72138
73- private int hashFunction (K key ) {
74- int hashCode = key .hashCode ();
75- hashCode = Math .abs (hashCode );
76- int hashIndex = hashCode % this .bucketArray .length ;
77- return hashIndex ;
78- }
79139}
0 commit comments