diff --git a/2014/collections/src/com/collections/BitSet.java b/2014/collections/src/com/collections/BitSet.java new file mode 100644 index 0000000..eafce3f --- /dev/null +++ b/2014/collections/src/com/collections/BitSet.java @@ -0,0 +1,36 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +public class BitSet { + + byte[] array; + + public BitSet(int size) { + array = new byte[(int) Math.ceil(size / 8.0)]; + } + + public boolean get(int bitPos) { + + // Go to the bit position in byte(8 bits) array + // Prepare by left shifting it appropriate times + return ((array[(bitPos - 1) / 8] & (1 << ((bitPos - 1) % 8))) != 0); + } + + public void set(int bitPos) { + // Set the bit position to 1 by logical OR operation on byte array + array[((bitPos - 1) / 8)] |= (1 << (bitPos - 1) % 8); + } + + public void clear(int bitPos) { + + // Set the bit position to 0(1's complement) by logical AND operation on + // byte array + // Because 1 0 => 0 and 1 1 => 1 so we will not loose any other bit data + array[((bitPos - 1) / 8)] &= ~(1 << (bitPos - 1) % 8); + } + +} diff --git a/2014/collections/src/com/collections/HPriorityQueue.java b/2014/collections/src/com/collections/HPriorityQueue.java new file mode 100644 index 0000000..a68d106 --- /dev/null +++ b/2014/collections/src/com/collections/HPriorityQueue.java @@ -0,0 +1,112 @@ +package com.collections; + +import java.util.ArrayList; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +public class HPriorityQueue implements PriorityQueue { + + private ArrayList heap; + + public HPriorityQueue() { + heap = new ArrayList(); + } + + @Override + public boolean add(T data) { + + heap.add(data); + int current = size() - 1; + + // Bottom-Up heap adjustment strategy + while (current > 0) { + + int parent = (current - 1) >> 1; + int maxChild = findMaxChild(parent); + + // compare the max child with parent + if (getDiff(parent, maxChild) < 0) { + // Swap max child node with parent + swap(parent, maxChild); + } + current = parent; + } + + return (current == 0); + } + + private void swap(int node1, int node2) { + + T temp = heap.get(node1); + heap.set(node1, heap.get(node2)); + heap.set(node2, temp); + + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private int getDiff(int index1, int index2) { + return ((Comparable) heap.get(index1)).compareTo(heap.get(index2)); + } + + private int findMaxChild(int parent) { + + int lastIndex = size() - 1; + int leftIndex = (parent << 1) + 1; + + if (lastIndex > leftIndex) { + return (getDiff(leftIndex, leftIndex + 1) > 0 ? leftIndex + : (leftIndex + 1)); + } else if (lastIndex == leftIndex) { + return leftIndex; + } + return -1; // return -1 because it don't have any child + } + + @Override + public T remove() { + + int lastIndex = size() - 1; + + if (lastIndex < 0) { + return null; + } + + // Save root element to return + T returnVal = heap.get(0); + + // Move last element to root + // post decrement index because element is removing + heap.set(0, heap.remove(lastIndex--)); + + // adjust tree for balancing from root to leaf + int parent = 0, lastParent = (lastIndex - 1) >> 1, maxChild; + + while (parent <= lastParent) { + maxChild = findMaxChild(parent); + + // Swap the parent and max child if diff is less than zero + if (getDiff(parent, maxChild) < 0) { + swap(parent, maxChild); + parent = maxChild; + } else { + break; + } + } + + return returnVal; + } + + @Override + public int size() { + return heap.size(); + } + + @Override + public void display() { + System.out.println(heap); + } + +} diff --git a/2014/collections/src/com/collections/HashMap.java b/2014/collections/src/com/collections/HashMap.java new file mode 100644 index 0000000..1382b05 --- /dev/null +++ b/2014/collections/src/com/collections/HashMap.java @@ -0,0 +1,34 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +public class HashMap implements Map { + + @Override + public void set(Key key, Val value) { + // TODO Auto-generated method stub + + } + + @Override + public Val get(Key key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean contains(Val value) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void clear() { + // TODO Auto-generated method stub + + } + +} diff --git a/2014/collections/src/com/collections/List.java b/2014/collections/src/com/collections/List.java new file mode 100644 index 0000000..b49a05b --- /dev/null +++ b/2014/collections/src/com/collections/List.java @@ -0,0 +1,17 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +interface List { + + public int addElement(Type data, int pos); + + public int addElement(Type data); + + public boolean removeElement(int pos); + + public void clear(); +} diff --git a/2014/collections/src/com/collections/Map.java b/2014/collections/src/com/collections/Map.java new file mode 100644 index 0000000..cf0dd39 --- /dev/null +++ b/2014/collections/src/com/collections/Map.java @@ -0,0 +1,14 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +public interface Map { + + public void set(Key key, Val value); + public Val get(Key key); + public boolean contains(Val value); + public void clear(); +} diff --git a/2014/collections/src/com/collections/PriorityQueue.java b/2014/collections/src/com/collections/PriorityQueue.java new file mode 100644 index 0000000..54697b5 --- /dev/null +++ b/2014/collections/src/com/collections/PriorityQueue.java @@ -0,0 +1,14 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +public interface PriorityQueue { + + public boolean add(T data); + public T remove(); + public int size(); + public void display(); +} diff --git a/2014/collections/src/com/collections/Set.java b/2014/collections/src/com/collections/Set.java new file mode 100644 index 0000000..3855a0b --- /dev/null +++ b/2014/collections/src/com/collections/Set.java @@ -0,0 +1,23 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + + +interface Set { + + public boolean add(T data); + + public boolean contains(T data); + + public boolean clear(); + + public void display(); +} diff --git a/2014/collections/src/com/collections/SinglyLinkedList.java b/2014/collections/src/com/collections/SinglyLinkedList.java new file mode 100644 index 0000000..896aafe --- /dev/null +++ b/2014/collections/src/com/collections/SinglyLinkedList.java @@ -0,0 +1,119 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +public class SinglyLinkedList implements List { + + private Node head, tail; + + public SinglyLinkedList() { + this.tail = this.head = new Node<>(null, null); + } + + /** + * Adds an element at specified position + * + * @param data + * @param pos [0 to length -1] + * @return index + */ + @Override + public int addElement(Type data, int pos) { + Node tmp = head; + int index = 0; + + while (tmp != tail) { + if (index++ == pos) { + // Pass the reaming list address to append to new node + tmp.next = new Node<>(data, tmp.next); + return index - 1; + } + tmp = tmp.next; + } + // Adding element at last if position is more than list length + tail = tmp.next = new Node<>(data, null); + return index; + } + + /** + * Adds an element at the end of the SLL + * + * @param data + * @return index + */ + @Override + public int addElement(Type data) { + Node tmp = head; + int index = 0; + while (tmp != tail) { + tmp = tmp.next; + ++index; + } + tail = tmp.next = new Node<>(data, null); + return index; + } + + /** + * Adds an element at the end of the SLL + * + * @param pos + * @return true/false + */ + @Override + public boolean removeElement(int pos) { + + Node tmp = head; + + for (int index = 1; index < pos; ++index) { + if (tmp == tail) { + return false; + } + tmp = tmp.next; + } + + if ((tmp.next != null) && (tmp.next.next != null)) { + tmp.next = tmp.next.next; + } else { + tmp.next = null; + tail = tmp; + } + return true; + } + + /** + * Returns a string representation of SLL + * + * @return String + */ + @Override + public String toString() { + String result; + result = new String(); + for (Node tmp = head.next; tmp != tail.next; tmp = tmp.next) { + result += (tmp.data + " "); + } + return result; + } + + @Override + public void clear() { + head.next = null; + tail = head; + } + + /* Creating node as an inner class avoid outside access */ + @SuppressWarnings("hiding") + private class Node { + + private Type data; + private Node next; + + public Node(Type data, Node next) { + this.data = data; + this.next = next; + } + } +} diff --git a/2014/collections/src/com/collections/SortedSet.java b/2014/collections/src/com/collections/SortedSet.java new file mode 100644 index 0000000..34a3907 --- /dev/null +++ b/2014/collections/src/com/collections/SortedSet.java @@ -0,0 +1,16 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +public interface SortedSet extends Set { + + public T findKthSmallest(int pos); + + public T findMax(); + + public T findMin(); + +} diff --git a/2014/collections/src/com/collections/TreeSet.java b/2014/collections/src/com/collections/TreeSet.java new file mode 100644 index 0000000..001ee76 --- /dev/null +++ b/2014/collections/src/com/collections/TreeSet.java @@ -0,0 +1,223 @@ +package com.collections; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +import java.util.ArrayList; + +public class TreeSet implements SortedSet { + + private TreeNode root; + + public TreeSet(T data) { + this.root = new TreeNode(data); + } + + /* + * Uses to add an element to Tree + * + * TC <= O(h) Maximum traversing can be height(h) of Tree. SC <= O(1) + * Constant space we are using here + * + * @see com.collections.Set#add(java.lang.Object) + */ + + @Override + public boolean add(T data) { + + TreeNode current = root; + TreeNode parent = null; + + int diff = 0; + + while (current != null) { + + parent = current; + + diff = ((Comparable) data).compareTo(current.data); + if (diff == 0) { + return true; + } else if (diff < 0) { + current = current.left; + } else { + current = current.right; + } + } + + if (diff < 0) { + parent.left = new TreeNode(data); + } else if (diff > 0) { + parent.right = new TreeNode(data); + } + + return (diff != 0); + } + + /* + * Uses to check an element presence in Tree + * + * - TC <= O(h) Maximum traversing can be height(h) of Tree. - SC <= O(1) + * Constant space we are using here. + * + * @see com.collections.Set#contains(java.lang.Object) + */ + + @Override + public boolean contains(T data) { + + TreeNode current = root; + int diff = 0; + + while (current != null) { + diff = ((Comparable) data).compareTo(current.data); + if (diff == 0) { + return true; + } else if (diff < 0) { + current = current.left; + } else { + current = current.right; + } + } + + return false; + } + + /* + * Uses to clear the tree + * + * - TC <= O(1) We are just clearing left and right nodes. - SC <= O(1) We + * are using constant space here. + * + * @see com.collections.Set#clear() + */ + + @Override + public boolean clear() { + root.left = root.right = null; + root.data = null; + return false; + } + + /* + * Uses to findMax number in the tree + * + * - TC <= O(h) Maximum traversing can be height(h) of Tree. - SC <= O(1) We + * are using constant space here. + * + * @see com.collections.SortedSet#findMax() + */ + + @Override + public T findMax() { + + TreeNode current = root; + + while (current.right != null) { + current = current.right; + } + + return current.data; + } + + /* + * Uses to findMin number in the tree + * + * - TC <= O(h) Maximum traversing can be height(h) of Tree. - SC <= O(1) We + * are using constant space here. + * + * @see com.collections.SortedSet#findMin() + */ + + @Override + public T findMin() { + TreeNode current = root; + + while (current.left != null) { + current = current.left; + } + + return current.data; + } + + /* + * Uses to display tree + * + * - TC <= O(n) Maximum traversing can be each node. - SC <= O(logn) We are + * using stack space here. + * + * @see com.collections.SortedSet#display() + */ + @Override + public void display() { + ArrayList elements = new ArrayList(); + auxPreOrder(root, elements); + System.out.println(elements); + } + + /* + * Uses to provide display API with toString() + * + * - TC <= O(n) Maximum traversing can be each node. - SC <= O(logn) We are + * using stack space here. + * + * @see com.collections.SortedSet#toString() + */ + public String toString() { + ArrayList elements = new ArrayList(); + auxPreOrder(root, elements); + return elements.toString(); + } + + private void auxPreOrder(TreeNode root, ArrayList elements) { + + if (root == null) { + return; + } + + auxPreOrder(root.left, elements); + elements.add(root.data); + auxPreOrder(root.right, elements); + } + + @Override + public T findKthSmallest(int pos) { + + TreeNode node = auxFindKthSmallest(root, (Integer)pos); + + return (node == null) ? null : node.data; + + } + + private TreeNode auxFindKthSmallest(TreeNode root, Integer counter) { + // Base condition to stop recursion + + if (root == null) { + return null; + } else { + + TreeNode result = auxFindKthSmallest(root.left, counter); + if (result != null) { + return result; + } + if (counter-- == 0) { + return root; + } + return auxFindKthSmallest(root.right, counter); + } + + } + + protected class TreeNode { + private T data; + private TreeNode left; + private TreeNode right; + + public TreeNode(T data) { + this.data = data; + this.left = this.right = null; + } + } + +} diff --git a/2014/collections/src/com/collections/test/BitSetApp.java b/2014/collections/src/com/collections/test/BitSetApp.java new file mode 100644 index 0000000..8baaaca --- /dev/null +++ b/2014/collections/src/com/collections/test/BitSetApp.java @@ -0,0 +1,32 @@ +package com.collections.test; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +import java.util.Random; + +import com.collections.BitSet; + +public class BitSetApp { + public static void main(String[] args) { + + BitSet bitSet = new BitSet(256); + + Random rand = new Random(); + + for (int index = 1; index <= 20; ++index) { + int random = rand.nextInt(40); + + System.out.println("Ramdom numnber is = " + random); + + if (bitSet.get(random)) { + System.out.println("\nDuplicate found = " + random); + break; + } else { + bitSet.set(random); + } + } + } +} diff --git a/2014/collections/src/com/collections/test/CountingWords.java b/2014/collections/src/com/collections/test/CountingWords.java new file mode 100644 index 0000000..6d6c070 --- /dev/null +++ b/2014/collections/src/com/collections/test/CountingWords.java @@ -0,0 +1,41 @@ +package com.collections.test; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CountingWords { + static String input = "Red lorry, yellow lorry, red lorry, yellow lorry"; + + public static void main(String[] args) { + long start = System.currentTimeMillis(); + input = input.toLowerCase(); + int countOfDifferWords = 0; + Pattern pat = Pattern.compile("\\w+"); + Matcher mat = pat.matcher(input); + StringBuilder sb = new StringBuilder(input); + while (mat.find()) { + countOfDifferWords++; + int i = 0; + System.out.print(mat.group() + " = "); + Pattern pat1 = Pattern.compile("\\b" + mat.group() + "\\b"); + Matcher mat1 = pat1.matcher(input); + while (mat1.find()) { + i++; + int len = mat.group().length(); + sb.replace(mat1.start(), (mat1.start() + len), new String( + new char[len])); + } + System.out.println(i); + mat = pat.matcher(sb.toString()); + } + long alltime = System.currentTimeMillis() - start; + System.out.println("\ncount of different words is " + + countOfDifferWords); + System.out.println("execution time: ~" + alltime + " ms"); + } +} \ No newline at end of file diff --git a/2014/collections/src/com/collections/test/HMapApp.java b/2014/collections/src/com/collections/test/HMapApp.java new file mode 100644 index 0000000..e469ef7 --- /dev/null +++ b/2014/collections/src/com/collections/test/HMapApp.java @@ -0,0 +1,43 @@ +package com.collections.test; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +import java.util.Arrays; +import com.collections.HashMap; + +public class HMapApp { + + /** + * @param args + */ + public static void main(String[] args) { + int[] elements = { 15, 12, 21, 6, 11, 18, 12, 98 }; + + HashMap hmap; + hmap = new HashMap<>(); + + for (int i = 0; i < elements.length; i++) { + hmap.set((2 * i + 2), elements[i]); + } + + System.out.println("Array form = " + Arrays.toString(elements)); + System.out.println("Map form = " + hmap.toString()); + + System.out.println("hmap.set(14, 35)"); + hmap.set(14, 35); + System.out.println("Map form = " + hmap.toString()); + + System.out.println("hmap.set(5, 31)"); + hmap.set(5, 31); + System.out.println("Map form = " + hmap.toString()); + + System.out.println("hmap.set(35, 39)"); + hmap.set(35, 39); + System.out.println("Map form = " + hmap.toString()); + + } + +} diff --git a/2014/collections/src/com/collections/test/HPQueueApp.java b/2014/collections/src/com/collections/test/HPQueueApp.java new file mode 100644 index 0000000..37be740 --- /dev/null +++ b/2014/collections/src/com/collections/test/HPQueueApp.java @@ -0,0 +1,34 @@ +package com.collections.test; + +import com.collections.HPriorityQueue; +import com.collections.PriorityQueue; + + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ +//HeapPriorityQueue +public class HPQueueApp{ + + public static void main(String[] args) { + + PriorityQueue pqueue = new HPriorityQueue(); + + for (int index = 9; index <= 24; ++index) { + System.out.print(index + " "); + pqueue.add(index); + } + + + System.out.println("\n === HPriorityQueue display() ==="); + pqueue.display(); + + System.out.println(" === HPriorityQueue remove() ===> " + pqueue.remove()); + pqueue.display(); + + System.out.println(" === HPriorityQueue remove() ===> " + pqueue.remove()); + pqueue.display(); + } + +} diff --git a/2014/collections/src/com/collections/test/SLLApp.java b/2014/collections/src/com/collections/test/SLLApp.java new file mode 100644 index 0000000..4077a53 --- /dev/null +++ b/2014/collections/src/com/collections/test/SLLApp.java @@ -0,0 +1,49 @@ +package com.collections.test; + +/** + * @author Srinivas Reddy + * @Email srinivas96alluri@gmail.com + */ + +import java.util.Arrays; + +import com.collections.SinglyLinkedList; + +public class SLLApp { + + /** + * @param args + */ + public static void main(String[] args) { + int[] elements = { 15, 12, 21, 6, 7, 16, 11, 18, 23, 98 }; + + SinglyLinkedList sll; + sll = new SinglyLinkedList<>(); + + for (int i = 0; i < elements.length; i++) { + sll.addElement(elements[i]); + } + + System.out.println("Array form = " + Arrays.toString(elements)); + + sll.removeElement(9); + System.out.println("Array form = " + sll.toString()); + + sll.removeElement(9); + System.out.println("Array form = " + sll.toString()); + + System.out.println("Linked list form = " + sll.toString()); + + sll.addElement(59, 3); + System.out.println("\nsll.addElement(59, 3) => \nLinked list = " + + sll.toString()); + + System.out.println(sll.addElement(60, 25)); + System.out.println("\nsll.addElement(60, 25) => \nLinked list = " + + sll.toString()); + + sll.addElement(49, 0); + System.out.println("\nsll.addElement(49, 0) => \nLinked list = " + + sll.toString()); + } +} diff --git a/2014/collections/src/com/collections/test/TreeSetApp.java b/2014/collections/src/com/collections/test/TreeSetApp.java new file mode 100644 index 0000000..1d8449a --- /dev/null +++ b/2014/collections/src/com/collections/test/TreeSetApp.java @@ -0,0 +1,45 @@ +package com.collections.test; + +/** + * @author Srinivas Reddy + * @email srinivas96alluri@gmail.com + */ + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import com.collections.SortedSet; +import com.collections.TreeSet; + +public class TreeSetApp { + public static void main(String[] args) { + + List elements = new ArrayList(); + + Random rand = new Random(); + + for (int index = 0; index < 20; ++index) { + elements.add(rand.nextInt(99)); + } + + SortedSet treeSet = new TreeSet(elements.get(0)); + + int limit = elements.size(); + + for (int index = 1; index < limit; index++) { + treeSet.add(elements.get(index)); + } + + System.out.println("Array form = " + elements); + + System.out.println("TreeSet form = " + treeSet.toString()); + + System.out.println("TreeSet Contains(20) = " + treeSet.contains(20)); + + System.out.println("TreeSet Max = " + treeSet.findMax()); + System.out.println("TreeSet Min = " + treeSet.findMin()); + System.out.println("TreeSet Kth = " + treeSet.findKthSmallest(3)); + + } +}