Friday, June 25, 2010

I always wanted to implement the logic behind the famous data structure of Java, HashMap and here it comes. Though it’s not the complete implementation considering all optimization scenarios, but it will highlight the basic algorithm behind the HashMap.

So let’s start, this implementation will use my LinkedList implementation (reason: for this implementation I thought to write everything on scratch apart from primitive data types. Sounds weird? May be ;-) ). You may refer my earlier post on LinkedList, as I’m going to use it.

HashMap is a key-value pair data structure, where you retrieve value by passing key. To optimize the search of key, a concept of Bucket (an Array of Java Objects) has been introduced. This is used, so that if search hits this Bucket, corresponding value element is instantly retrieved, otherwise it iterates sequentially through the Linked List associated with each Bucket element. So you can say if all HashMap elements get fit into the Bucket, retrieving elements back, will be super fast and this is the Best Case. But that also means you are keeping everything in an array which is not good in-terms of heap memory storage limitation. So we need to consider both the things Bucket and LinkedList. This is really interesting, so let me know, if you guys want to discuss more on this.

To make sure that all Key_Index (Hashcode of Key Object, calculated by calling Object’s hashCode method) becomes less than Bucket_Size, we will do the mod operation i.e.

Key_Index = key.hashCode() % Bucket_Size,

This returns integer value which is always less than Bucket_Size. If multiple key Objects return same Key_Index, then all matching keys will be stored in the associated LinkedList of the Bucket element. So if the bucket size is 10, following figure depicts the behavior of element entry into HashMap-

Thursday, June 17, 2010

This post is a part of my continuous effort to just implement few of the important data structures in Java. Please find my previous post on LinkedList and QuickSort. This post is regarding BST (Binary Search Tree).

Let’s first declare the structure of a BST Node

/**

*BSTNode Structure

*@author prasanta

*/

publicclassBST_Node {

/**

*Leftsubtreepointer

*/

BST_Nodeleft;

/**

*Rightsubtreepointer

*/

BST_Noderight;

/**

*keywhichisusedforcomparison

*/

intvalue;

}

Add Node into BST

BST_Noderoot;

publicvoid insert(int value){

root = add(root, value);

}

privateBST_Node add(BST_Node node, int value){

if(node == null){

node = newBST_Node();

node.value = value;

return node;

}

if(value < node.value)

// Left Node (<)

node.left = add(node.left, value);

else

// Right Node (>=)

node.right = add(node.right, value);

return node;

}

So your Binary Search Tree is complete and you can refer it using root. There are multiple ways you can traverse a BST- Pre-Order, In-Order, Post-Order and Level-Order (get more details on WiKi-http://en.wikipedia.org/wiki/Tree_traversal). Here I’ll show implementation of Pre-Order and In-Order, as I’m facing some problem with my Post-Order implementation.