Skip to content

Collections code has been added #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 2014/collections/src/com/collections/BitSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.collections;

/**
* @author Srinivas Reddy
* @email [email protected]
*/

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);
}

}
112 changes: 112 additions & 0 deletions 2014/collections/src/com/collections/HPriorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.collections;

import java.util.ArrayList;

/**
* @author Srinivas Reddy
* @email [email protected]
*/

public class HPriorityQueue<T> implements PriorityQueue<T> {

private ArrayList<T> heap;

public HPriorityQueue() {
heap = new ArrayList<T>();
}

@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);
}

}
34 changes: 34 additions & 0 deletions 2014/collections/src/com/collections/HashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.collections;

/**
* @author Srinivas Reddy
* @Email [email protected]
*/

public class HashMap<Key, Val> implements Map<Key, Val> {

@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

}

}
17 changes: 17 additions & 0 deletions 2014/collections/src/com/collections/List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.collections;

/**
* @author Srinivas Reddy
* @Email [email protected]
*/

interface List<Type> {

public int addElement(Type data, int pos);

public int addElement(Type data);

public boolean removeElement(int pos);

public void clear();
}
14 changes: 14 additions & 0 deletions 2014/collections/src/com/collections/Map.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.collections;

/**
* @author Srinivas Reddy
* @Email [email protected]
*/

public interface Map<Key, Val> {

public void set(Key key, Val value);
public Val get(Key key);
public boolean contains(Val value);
public void clear();
}
14 changes: 14 additions & 0 deletions 2014/collections/src/com/collections/PriorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.collections;

/**
* @author Srinivas Reddy
* @Email [email protected]
*/

public interface PriorityQueue<T> {

public boolean add(T data);
public T remove();
public int size();
public void display();
}
23 changes: 23 additions & 0 deletions 2014/collections/src/com/collections/Set.java
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/


interface Set<T> {

public boolean add(T data);

public boolean contains(T data);

public boolean clear();

public void display();
}
119 changes: 119 additions & 0 deletions 2014/collections/src/com/collections/SinglyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.collections;

/**
* @author Srinivas Reddy
* @Email [email protected]
*/

public class SinglyLinkedList<Type> implements List<Type> {

private Node<Type> 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<Type> 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<Type> 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<Type> 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<Type> 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<Type> {

private Type data;
private Node<Type> next;

public Node(Type data, Node<Type> next) {
this.data = data;
this.next = next;
}
}
}
16 changes: 16 additions & 0 deletions 2014/collections/src/com/collections/SortedSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.collections;

/**
* @author Srinivas Reddy
* @email [email protected]
*/

public interface SortedSet<T> extends Set<T> {

public T findKthSmallest(int pos);

public T findMax();

public T findMin();

}
Loading