Skip to main content

Posts

Showing posts with the label Data Structure

Its about Bytes in Java

I have seen people struggle in handling data in Java when it reaches at the byte level. As Java doesn't support any concept of Un-signed data type, so it makes things more complex. So thought lets share my understanding. Java stores negative values in 2's complement format and to get absolute value of any negative number you need to type cast variable with higher byte value datatype. In simple word, to get absolute value of -ve byte data type variable it need to type cast/upgrade to some other date type (e.g. short/int) having more number of bytes and. Example, byte mSample = 142 ; If you print its value, it will show -114 . So, why does the JVM shows -114 ? Binary of 142 : 1 0 0 0 1 1 1 0 . The left most bit is Signed bit, so JVM considers this number as -ve value and converts it into 2's Complement, which means, 0 1 1 1 0 0 1 0 = - 114. So, its simple, right ? So, to get absolute value of mSample , we need to type cast it to higher byte enabled data type ...

Excel Column Sequence Algorithm

One of my friend came up with a question..."How do you find out the sequence of MS Excel column ?", lets put this in an another way, If I tell you one Excel Sheet column sequence e.g. ABA how do you find out the index of the column e.g. AA is 27th column, AB is 28th column. So, I thought to put this as an Algo; with this if you have any sequence, you will be able to compute the column number- Column Number = Alphabet Seq Number (right most) + Alphabet Seq Number * 26 + Alphabet Seq Number * (26*26) + .......... + Alphabet Seq Number (left most) * (p-1 times multiplication of 26) Where, Alphabet Seq Number = 1-26, (A = 1 and Z = 26) p = number of Alphabets in the Column Sequence e.g. for "ABC", p = 3. Here  is C at position 1, B at 2 and A at 3. So, with the above formula, Column number for ABC = C + B * 26 + A * (26 * 26)   = 3 + 2 * 26 + 1* (26*26) = 731 So, what about doing the reverse i.e. generate the sequence from column number. To...

Alphanumeric Random Number

Sometime basic things becomes tricky :-) Was trying to find out ways to generate some alphanumeric random number whose size (i.e. number of digits/alphabets) will be fixed and I should be able to decide that size. So, I came up with following algo- 1. ASCII table sequentially lists digits and alphabets e.g. ASCII code of 0 to 9 are ranging from 48 to 57. So I can use this sequential ASCII coding and convert them to Java Characters. Benefit, of course I can get rid of any array of predefined characters in my code. 2. I want to add current time to this "randomness", so need to initialize the seed of Java Random object with current timestamp. So every time I execute this, I can make sure it will be something different than previous instance. 3. As I already explained, it will be alphanumeric based, so I'll exclude special characters and thus, total number of valid ASCII chars left for me is 62 ( 0-9, A-Z, a-z ) . Now, let's see how the Java code looks like. Ple...

HashMap Internal

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 from the 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...

Binary Search Tree in Java

--> 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 /**   * BST Node Structure   * @author prasanta   */ public class BST_Node {       /**         * Left subtree pointer         */       BST_Node left ;       /**         * Right subtree pointer         */       BST_Node right ;       /**         * key which is used for comparison         */   ...