Skip to content

Commit 20d0bb2

Browse files
Merge branch 'master' of https://github.com/freitzzz/Java
# 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
2 parents 467b917 + 7e3a8c5 commit 20d0bb2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2697
-114
lines changed

Conversions/AnyBaseToAnyBase.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import java.util.Arrays;
2+
import java.util.HashSet;
3+
import java.util.InputMismatchException;
4+
import java.util.Scanner;
5+
6+
/**
7+
* Class for converting from "any" base to "any" other base, when "any" means from 2-36.
8+
* Works by going from base 1 to decimal to base 2. Includes auxiliary method for
9+
* determining whether a number is valid for a given base.
10+
*
11+
* @author Michael Rolland
12+
* @version 2017.10.10
13+
*
14+
*/
15+
public class AnyBaseToAnyBase {
16+
17+
// Smallest and largest base you want to accept as valid input
18+
static final int MINIMUM_BASE = 2;
19+
static final int MAXIMUM_BASE = 36;
20+
21+
// Driver
22+
public static void main(String[] args) {
23+
Scanner in = new Scanner(System.in);
24+
String n;
25+
int b1=0,b2=0;
26+
while (true) {
27+
try {
28+
System.out.print("Enter number: ");
29+
n = in.next();
30+
System.out.print("Enter beginning base (between "+MINIMUM_BASE+" and "+MAXIMUM_BASE+"): ");
31+
b1 = in.nextInt();
32+
if (b1 > MAXIMUM_BASE || b1 < MINIMUM_BASE) {
33+
System.out.println("Invalid base!");
34+
continue;
35+
}
36+
if (!validForBase(n, b1)) {
37+
System.out.println("The number is invalid for this base!");
38+
continue;
39+
}
40+
System.out.print("Enter end base (between "+MINIMUM_BASE+" and "+MAXIMUM_BASE+"): ");
41+
b2 = in.nextInt();
42+
if (b2 > MAXIMUM_BASE || b2 < MINIMUM_BASE) {
43+
System.out.println("Invalid base!");
44+
continue;
45+
}
46+
break;
47+
} catch (InputMismatchException e) {
48+
System.out.println("Invalid input.");
49+
in.next();
50+
}
51+
}
52+
System.out.println(base2base(n, b1, b2));
53+
}
54+
55+
/**
56+
* Checks if a number (as a String) is valid for a given base.
57+
*/
58+
public static boolean validForBase(String n, int base) {
59+
char[] validDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
60+
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
61+
'W', 'X', 'Y', 'Z'};
62+
// digitsForBase contains all the valid digits for the base given
63+
char[] digitsForBase = Arrays.copyOfRange(validDigits, 0, base);
64+
65+
// Convert character array into set for convenience of contains() method
66+
HashSet<Character> digitsList = new HashSet();
67+
for (int i=0; i<digitsForBase.length; i++)
68+
digitsList.add(digitsForBase[i]);
69+
70+
// Check that every digit in n is within the list of valid digits for that base.
71+
for (char c : n.toCharArray())
72+
if (!digitsList.contains(c))
73+
return false;
74+
75+
return true;
76+
}
77+
78+
/**
79+
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
80+
* then decimal to b2.
81+
* @param n The integer to be converted.
82+
* @param b1 Beginning base.
83+
* @param b2 End base.
84+
* @return n in base b2.
85+
*/
86+
public static String base2base(String n, int b1, int b2) {
87+
// Declare variables: decimal value of n,
88+
// character of base b1, character of base b2,
89+
// and the string that will be returned.
90+
int decimalValue = 0, charB2;
91+
char charB1;
92+
String output="";
93+
// Go through every character of n
94+
for (int i=0; i<n.length(); i++) {
95+
// store the character in charB1
96+
charB1 = n.charAt(i);
97+
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
98+
if (charB1 >= 'A' && charB1 <= 'Z')
99+
charB2 = 10 + (charB1 - 'A');
100+
// Else, store the integer value in charB2
101+
else
102+
charB2 = charB1 - '0';
103+
// Convert the digit to decimal and add it to the
104+
// decimalValue of n
105+
decimalValue = decimalValue * b1 + charB2;
106+
}
107+
108+
// Converting the decimal value to base b2:
109+
// A number is converted from decimal to another base
110+
// by continuously dividing by the base and recording
111+
// the remainder until the quotient is zero. The number in the
112+
// new base is the remainders, with the last remainder
113+
// being the left-most digit.
114+
115+
// While the quotient is NOT zero:
116+
while (decimalValue != 0) {
117+
// If the remainder is a digit < 10, simply add it to
118+
// the left side of the new number.
119+
if (decimalValue % b2 < 10)
120+
output = Integer.toString(decimalValue % b2) + output;
121+
// If the remainder is >= 10, add a character with the
122+
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
123+
else
124+
output = (char)((decimalValue % b2)+55) + output;
125+
// Divide by the new base again
126+
decimalValue /= b2;
127+
}
128+
return output;
129+
}
130+
}

Data Structures/HashMap/HashMap.java

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<<<<<<< HEAD:Data Structures/HashMap/HashMap.java
12

23

34
import java.util.ArrayList;
@@ -139,3 +140,144 @@ private void rehash() throws Exception{
139140

140141
}
141142
}
143+
=======
144+
import java.util.ArrayList;
145+
import java.util.LinkedList;
146+
147+
public class HashMap<K,V> {
148+
public class hmnodes{ //HashMap nodes
149+
K key;
150+
V value;
151+
}
152+
153+
private int size=0; //size of hashmap
154+
private LinkedList<hmnodes> buckets[]; //array of addresses of list
155+
156+
public HashMap(){
157+
buckets=new LinkedList[4]; //initially create bucket of any size
158+
for(int i=0;i<4;i++)
159+
buckets[i]=new LinkedList<>();
160+
}
161+
162+
public void put(K key,V value) throws Exception{
163+
int bi=bucketIndex(key); //find the index,the new key will be inserted in linklist at that index
164+
int fountAt=find(bi,key); //check if key already exists or not
165+
if(fountAt==-1){
166+
hmnodes temp=new hmnodes(); //if doesn't exist create new node and insert
167+
temp.key=key;
168+
temp.value=value;
169+
buckets[bi].addLast(temp);
170+
this.size++;
171+
}else{
172+
buckets[bi].get(fountAt).value=value;//if already exist modify the value
173+
}
174+
175+
double lambda = (this.size*1.0)/this.buckets.length;
176+
if(lambda>2.0){
177+
rehash(); //rehashing function which will increase the size of bucket as soon as lambda exceeds 2.0
178+
}
179+
180+
return;
181+
}
182+
183+
184+
public V get(K key) throws Exception{
185+
int bi=bucketIndex(key);
186+
int fountAt=find(bi,key);
187+
if(fountAt==-1){
188+
return null;
189+
}else{
190+
return buckets[bi].get(fountAt).value;
191+
}
192+
}
193+
194+
public V remove(K key) throws Exception{
195+
int bi=bucketIndex(key);
196+
int fountAt=find(bi,key);
197+
if(fountAt==-1){
198+
return null;
199+
}else{
200+
this.size--;
201+
return buckets[bi].remove(fountAt).value;
202+
}
203+
}
204+
205+
public boolean containskey(K key) throws Exception{
206+
int bi=bucketIndex(key);
207+
int fountAt=find(bi,key);
208+
if(fountAt==-1){
209+
return false;
210+
}else{
211+
return true;
212+
}
213+
}
214+
215+
public int size(){
216+
return this.size;
217+
}
218+
219+
220+
public boolean isempty(){
221+
return this.size==0;
222+
}
223+
224+
public ArrayList<K> keyset() throws Exception{
225+
ArrayList<K> arr=new ArrayList<>();
226+
for(int i=0;i<buckets.length;i++){
227+
for(int j=0;j<buckets[i].size();j++){
228+
arr.add(buckets[i].get(j).key);
229+
}
230+
}
231+
return arr;
232+
}
233+
234+
public ArrayList<V> valueset() throws Exception{
235+
ArrayList<V> arr=new ArrayList<>();
236+
for(int i=0;i<buckets.length;i++){
237+
for(int j=0;j<buckets[i].size();j++){
238+
arr.add(buckets[i].get(j).value);
239+
}
240+
}
241+
return arr;
242+
}
243+
244+
public void display() throws Exception{
245+
for(int i=0;i<buckets.length;i++){
246+
System.out.print("Bucket: "+i+" ");
247+
for(int j=0;j<buckets[i].size();j++){
248+
hmnodes temp=buckets[i].get(j);
249+
System.out.print("["+temp.key+"->"+temp.value+"]");
250+
}
251+
System.out.println();
252+
}
253+
}
254+
255+
public int find(int bi,K key) throws Exception{
256+
for(int i=0;i<buckets[bi].size();i++){
257+
if(key.equals(buckets[bi].get(i).key))
258+
return i;
259+
}
260+
return -1;
261+
}
262+
263+
public int bucketIndex(K key) throws Exception{
264+
int bi=key.hashCode();
265+
return Math.abs(bi%buckets.length);
266+
}
267+
268+
private void rehash() throws Exception{
269+
LinkedList<hmnodes> ob[]= buckets;
270+
buckets=new LinkedList[ob.length*2];
271+
for(int i=0;i<ob.length*2;i++)
272+
buckets[i]=new LinkedList<>();
273+
274+
size = 0;
275+
for(int i=0;i<ob.length;i++){
276+
for(int j=0;j<ob[i].size();j++){
277+
put(ob[i].get(j).key,ob[i].get(j).value);
278+
}
279+
}
280+
281+
}
282+
}
283+
>>>>>>> 7e3a8c55c865471a33f6932a022a1059c5243fc3:data_structures/HashMap/HashMap.java

Data Structures/Lists/CircleLinkedList.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,44 @@ private Node(E value, Node<E> next){
77
this.next = next;
88
}
99
}
10-
private int size; //For better O.O design this should be private allows for better black box design
11-
private Node<E> head; //this will point to dummy node;
12-
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;
13-
head = new Node<E>(null,head); //creation of the dummy node
10+
//For better O.O design this should be private allows for better black box design
11+
private int size;
12+
//this will point to dummy node;
13+
private Node<E> head;
14+
//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;
15+
public CircleLinkedList(){
16+
//creation of the dummy node
17+
head = new Node<E>(null,head);
1418
size = 0;
1519
}
16-
public int getSize(){ return size;} // getter for the size... needed because size is private.
17-
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.
20+
// getter for the size... needed because size is private.
21+
public int getSize(){ return size;}
22+
// 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.
23+
public void append(E value){
1824
if(value == null){
19-
throw new NullPointerException("Cannot add null element to the list"); // we do not want to add null elements to the list.
25+
// we do not want to add null elements to the list.
26+
throw new NullPointerException("Cannot add null element to the list");
2027
}
21-
head.next = new Node<E>(value,head); //head.next points to the last element;
28+
//head.next points to the last element;
29+
head.next = new Node<E>(value,head);
2230
size++;}
2331
public E remove(int pos){
2432
if(pos>size || pos< 0){
25-
throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); //catching errors
33+
//catching errors
34+
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
2635
}
2736
Node<E> iterator = head.next;
28-
Node<E> before = head; //we need to keep track of the element before the element we want to remove we can see why bellow.
37+
//we need to keep track of the element before the element we want to remove we can see why bellow.
38+
Node<E> before = head;
2939
for(int i = 1; i<=pos; i++){
3040
iterator = iterator.next;
3141
before = before.next;
3242
}
3343
E saved = iterator.value;
34-
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.
35-
iterator.next = null; // scrubbing
44+
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
45+
before.next = iterator.next;
46+
// scrubbing
47+
iterator.next = null;
3648
iterator.value = null;
3749
return saved;
3850

Data Structures/Stacks/Stacks.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void push(int value){
4242
top++;
4343
stackArray[top] = value;
4444
}else{
45-
System.out.println("The stack is full, can't insert value");
45+
resize(maxSize*2);
4646
}
4747
}
4848

@@ -54,7 +54,12 @@ public void push(int value){
5454
public int pop(){
5555
if(!isEmpty()){ //Checks for an empty stack
5656
return stackArray[top--];
57-
}else{
57+
}
58+
59+
if(top < maxSize/4){
60+
resize(maxSize/2);
61+
}
62+
else{
5863
System.out.println("The stack is already empty");
5964
return -1;
6065
}
@@ -74,6 +79,16 @@ public int peek(){
7479
}
7580
}
7681

82+
private void resize(int newSize){
83+
private int[] transferArray = new int[newSize];
84+
85+
for(int i = 0; i < stackArray.length(); i++){
86+
transferArray[i] = stackArray[i];
87+
stackArray = transferArray;
88+
}
89+
maxSize = newSize;
90+
}
91+
7792
/**
7893
* Returns true if the stack is empty
7994
*

0 commit comments

Comments
 (0)