# Conflicts:
#	Data Structures/HashMap/HashMap.java
#	Huffman.java
#	Misc/FloydTriangle.java
#	Misc/Huffman.java
#	Misc/InsertDeleteInArray.java
#	Misc/RootPrecision.java
#	Misc/ft.java
#	Misc/root_precision.java
#	Others/FloydTriangle.java
#	Others/Huffman.java
#	Others/insert_delete_in_array.java
#	Others/root_precision.java
#	insert_delete_in_array.java
This commit is contained in:
DESKTOP-0VAEMFL\joaom
2017-10-28 12:59:58 +01:00
58 changed files with 2697 additions and 114 deletions

View File

@ -1,3 +1,4 @@
<<<<<<< HEAD:Data Structures/HashMap/HashMap.java
import java.util.ArrayList;
@ -139,3 +140,144 @@ public class HashMap<K,V> {
}
}
=======
import java.util.ArrayList;
import java.util.LinkedList;
public class HashMap<K,V> {
public class hmnodes{ //HashMap nodes
K key;
V value;
}
private int size=0; //size of hashmap
private LinkedList<hmnodes> buckets[]; //array of addresses of list
public HashMap(){
buckets=new LinkedList[4]; //initially create bucket of any size
for(int i=0;i<4;i++)
buckets[i]=new LinkedList<>();
}
public void put(K key,V value) throws Exception{
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
int fountAt=find(bi,key); //check if key already exists or not
if(fountAt==-1){
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
temp.key=key;
temp.value=value;
buckets[bi].addLast(temp);
this.size++;
}else{
buckets[bi].get(fountAt).value=value;//if already exist modify the value
}
double lambda = (this.size*1.0)/this.buckets.length;
if(lambda>2.0){
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
}
return;
}
public V get(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return null;
}else{
return buckets[bi].get(fountAt).value;
}
}
public V remove(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return null;
}else{
this.size--;
return buckets[bi].remove(fountAt).value;
}
}
public boolean containskey(K key) throws Exception{
int bi=bucketIndex(key);
int fountAt=find(bi,key);
if(fountAt==-1){
return false;
}else{
return true;
}
}
public int size(){
return this.size;
}
public boolean isempty(){
return this.size==0;
}
public ArrayList<K> keyset() throws Exception{
ArrayList<K> arr=new ArrayList<>();
for(int i=0;i<buckets.length;i++){
for(int j=0;j<buckets[i].size();j++){
arr.add(buckets[i].get(j).key);
}
}
return arr;
}
public ArrayList<V> valueset() throws Exception{
ArrayList<V> arr=new ArrayList<>();
for(int i=0;i<buckets.length;i++){
for(int j=0;j<buckets[i].size();j++){
arr.add(buckets[i].get(j).value);
}
}
return arr;
}
public void display() throws Exception{
for(int i=0;i<buckets.length;i++){
System.out.print("Bucket: "+i+" ");
for(int j=0;j<buckets[i].size();j++){
hmnodes temp=buckets[i].get(j);
System.out.print("["+temp.key+"->"+temp.value+"]");
}
System.out.println();
}
}
public int find(int bi,K key) throws Exception{
for(int i=0;i<buckets[bi].size();i++){
if(key.equals(buckets[bi].get(i).key))
return i;
}
return -1;
}
public int bucketIndex(K key) throws Exception{
int bi=key.hashCode();
return Math.abs(bi%buckets.length);
}
private void rehash() throws Exception{
LinkedList<hmnodes> ob[]= buckets;
buckets=new LinkedList[ob.length*2];
for(int i=0;i<ob.length*2;i++)
buckets[i]=new LinkedList<>();
size = 0;
for(int i=0;i<ob.length;i++){
for(int j=0;j<ob[i].size();j++){
put(ob[i].get(j).key,ob[i].get(j).value);
}
}
}
}
>>>>>>> 7e3a8c55c865471a33f6932a022a1059c5243fc3:data_structures/HashMap/HashMap.java

View File

@ -7,32 +7,44 @@ public class CircleLinkedList<E>{
this.next = next;
}
}
private int size; //For better O.O design this should be private allows for better black box design
private Node<E> head; //this will point to dummy node;
public CircleLinkedList(){ //constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
head = new Node<E>(null,head); //creation of the dummy node
//For better O.O design this should be private allows for better black box design
private int size;
//this will point to dummy node;
private Node<E> head;
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
public CircleLinkedList(){
//creation of the dummy node
head = new Node<E>(null,head);
size = 0;
}
public int getSize(){ return size;} // getter for the size... needed because size is private.
public void append(E value){ // for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
// getter for the size... needed because size is private.
public int getSize(){ return size;}
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
public void append(E value){
if(value == null){
throw new NullPointerException("Cannot add null element to the list"); // we do not want to add null elements to the list.
// we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list");
}
head.next = new Node<E>(value,head); //head.next points to the last element;
//head.next points to the last element;
head.next = new Node<E>(value,head);
size++;}
public E remove(int pos){
if(pos>size || pos< 0){
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); //catching errors
//catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
Node<E> iterator = head.next;
Node<E> before = head; //we need to keep track of the element before the element we want to remove we can see why bellow.
//we need to keep track of the element before the element we want to remove we can see why bellow.
Node<E> before = head;
for(int i = 1; i<=pos; i++){
iterator = iterator.next;
before = before.next;
}
E saved = iterator.value;
before.next = iterator.next; // assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
iterator.next = null; // scrubbing
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
before.next = iterator.next;
// scrubbing
iterator.next = null;
iterator.value = null;
return saved;

View File

@ -42,7 +42,7 @@ class Stack{
top++;
stackArray[top] = value;
}else{
System.out.println("The stack is full, can't insert value");
resize(maxSize*2);
}
}
@ -54,7 +54,12 @@ class Stack{
public int pop(){
if(!isEmpty()){ //Checks for an empty stack
return stackArray[top--];
}else{
}
if(top < maxSize/4){
resize(maxSize/2);
}
else{
System.out.println("The stack is already empty");
return -1;
}
@ -74,6 +79,16 @@ class Stack{
}
}
private void resize(int newSize){
private int[] transferArray = new int[newSize];
for(int i = 0; i < stackArray.length(); i++){
transferArray[i] = stackArray[i];
stackArray = transferArray;
}
maxSize = newSize;
}
/**
* Returns true if the stack is empty
*

View File

@ -1,37 +1,50 @@
import java.util.LinkedList;
/**
*
* @author Varun Upadhyay (https://github.com/varunu28)
*
*/
*
* @author Varun Upadhyay (https://github.com/varunu28)
*
*/
// Driver Program
public class TreeTraversal {
public static void main(String[] args) {
Node tree = new Node(5);
tree.insert(3);
tree.insert(2);
tree.insert(7);
tree.insert(4);
tree.insert(6);
tree.insert(8);
// Prints 3 5 7
tree.printInOrder();
System.out.println();
// Prints 5 3 7
// Prints 5 3 2 4 7 6 8
System.out.println("Pre order traversal:");
tree.printPreOrder();
System.out.println();
// Prints 3 7 5
// Prints 2 3 4 5 6 7 8
System.out.println("In order traversal:");
tree.printInOrder();
System.out.println();
// Prints 2 4 3 6 8 7 5
System.out.println("Post order traversal:");
tree.printPostOrder();
System.out.println();
// Prints 5 3 7 2 4 6 8
System.out.println("Level order traversal:");
tree.printLevelOrder();
System.out.println();
}
}
/**
* The Node class which initializes a Node of a tree
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
* printInOrder: LEFT -> ROOT -> RIGHT
* printPreOrder: ROOT -> LEFT -> RIGHT
* printPostOrder: LEFT -> RIGHT -> ROOT
*/
* The Node class which initializes a Node of a tree
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
* printInOrder: LEFT -> ROOT -> RIGHT
* printPreOrder: ROOT -> LEFT -> RIGHT
* printPostOrder: LEFT -> RIGHT -> ROOT
* printLevelOrder: Prints by level (starting at root), from left to right.
*/
class Node {
Node left, right;
int data;
@ -88,5 +101,24 @@ class Node {
}
System.out.print(data + " ");
}
}
/**
* O(n) time algorithm.
* Uses O(n) space to store nodes in a queue to aid in traversal.
*/
public void printLevelOrder() {
LinkedList<Node> queue = new LinkedList<>();
queue.add(this);
while (queue.size() > 0) {
Node head = queue.remove();
System.out.print(head.data + " ");
// Add children of recently-printed node to queue, if they exist.
if (head.left != null) {
queue.add(head.left);
}
if (head.right != null) {
queue.add(head.right);
}
}
}
}