removed duplciated data-structures

This commit is contained in:
Christian Bender
2018-07-25 16:37:10 +02:00
parent 89b94a8f52
commit c86ba856a8
52 changed files with 75 additions and 30618 deletions

View File

@@ -0,0 +1,78 @@
import java.util.Scanner;
public class FloydWarshall
{
private int DistanceMatrix[][];
private int numberofvertices;//number of vertices in the graph
public static final int INFINITY = 999;
public FloydWarshall(int numberofvertices)
{
DistanceMatrix = new int[numberofvertices + 1][numberofvertices + 1];//stores the value of distance from all the possible path form the source vertex to destination vertex
Arrays.fill(DistanceMatrix, 0);
this.numberofvertices = numberofvertices;
}
public void floydwarshall(int AdjacencyMatrix[][])//calculates all the distances from source to destination vertex
{
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
DistanceMatrix[source][destination] = AdjacencyMatrix[source][destination];
}
}
for (int intermediate = 1; intermediate <= numberofvertices; intermediate++)
{
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
if (DistanceMatrix[source][intermediate] + DistanceMatrix[intermediate][destination]
< DistanceMatrix[source][destination])//if the new distance calculated is less then the earlier shortest calculated distance it get replaced as new shortest distance
DistanceMatrix[source][destination] = DistanceMatrix[source][intermediate]
+ DistanceMatrix[intermediate][destination];
}
}
}
for (int source = 1; source <= numberofvertices; source++)
System.out.print("\t" + source);
System.out.println();
for (int source = 1; source <= numberofvertices; source++)
{
System.out.print(source + "\t");
for (int destination = 1; destination <= numberofvertices; destination++)
{
System.out.print(DistanceMatrix[source][destination] + "\t");
}
System.out.println();
}
}
public static void main(String... arg)
{
int Adjacency_Matrix[][];
int numberofvertices;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of vertices");
numberofvertices = scan.nextInt();
Adjacency_Matrix = new int[numberofvertices + 1][numberofvertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int source = 1; source <= numberofvertices; source++)
{
for (int destination = 1; destination <= numberofvertices; destination++)
{
Adjacency_Matrix[source][destination] = scan.nextInt();
if (source == destination)
{
Adjacency_Matrix[source][destination] = 0;
continue;
}
if (Adjacency_Matrix[source][destination] == 0)
{
Adjacency_Matrix[source][destination] = INFINITY;
}
}
}
System.out.println("The Transitive Closure of the Graph");
FloydWarshall floydwarshall = new FloydWarshall(numberofvertices);
floydwarshall.floydwarshall(adjacency_matrix);
scan.close();
}
}

View File

@@ -0,0 +1,39 @@
class HashMap {
private int hsize;
private LinkedList[] buckets;
public HashMap(int hsize) {
buckets = new LinkedList[hsize];
for (int i = 0; i < hsize ; i++ ) {
buckets[i] = new LinkedList();
// Java requires explicit initialisaton of each object
}
this.hsize = hsize;
}
public int hashing(int key) {
int hash = key % hsize;
if(hash < 0)
hash += hsize;
return hash;
}
public void insertHash(int key) {
int hash = hashing(key);
buckets[hash].insert(key);
}
public void deleteHash(int key) {
int hash = hashing(key);
buckets[hash].delete(key);
}
public void displayHashtable() {
for (int i = 0;i < hsize ; i++) {
System.out.printf("Bucket %d :",i);
buckets[i].display();
}
}
}

View File

@@ -0,0 +1,62 @@
class LinkedList {
private Node Head;
private int size;
public LinkedList() {
Head = null;
size = 0;
}
public void insert(int data) {
Node temp = Head;
Node newnode = new Node(data);
size++;
if(Head == null) {
Head = newnode;
}
else {
newnode.next = Head;
Head = newnode;
}
}
public void delete(int data) {
if(size == 0) {
System.out.println("UnderFlow!");
return;
}
else {
Node curr = Head;
if (curr.data == data) {
Head = curr.next;
size--;
return;
}
else {
while(curr.next.next != null) {
if(curr.next.data == data){
curr.next = curr.next.next;
return;
}
}
System.out.println("Key not Found");
}
}
}
public void display() {
Node temp = Head;
while(temp != null) {
System.out.printf("%d ",temp.data);
temp = temp.next;
}
System.out.println();
}
}

View File

@@ -0,0 +1,45 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int choice, key;
HashMap h = new HashMap(7);
while (true) {
System.out.println("Enter your Choice :");
System.out.println("1. Add Key");
System.out.println("2. Delete Key");
System.out.println("3. Print Table");
System.out.println("4. Exit");
Scanner In = new Scanner(System.in);
choice = In.nextInt();
switch (choice) {
case 1: {
System.out.println("Enter the Key: ");
key = In.nextInt();
h.insertHash(key);
break;
}
case 2: {
System.out.println("Enter the Key delete: ");
key = In.nextInt();
h.deleteHash(key);
break;
}
case 3: {
System.out.println("Print table");
h.displayHashtable();
break;
}
case 4: {
return;
}
}
}
}
}

View File

@@ -0,0 +1,9 @@
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}

View File

@@ -1,59 +1,54 @@
public class CircleLinkedList<E>{
private static class Node<E>{
Node<E> next;
E value;
private Node(E value, Node<E> next){
this.value = value;
this.next = next;
}
}
//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;
private Node<E> tail;
//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(){
head = new Node<>(null, head);
tail = head;
}
// 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){
// we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list");
}
//add new node at the end of the list and update tail node to point to new node
tail.next = new Node(value, head);
tail = tail.next;
size++;
}
public E remove(int pos){
if(pos>=size || pos< 0){
//catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
Node<E> iterator = head.next;
//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++){
private static class Node<E>{
Node<E> next;
E value;
private Node(E value, Node<E> next){
this.value = value;
this.next = next;
}
}
//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;
}
// 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){
// we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list");
}
//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){
//catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
Node<E> iterator = head.next;
//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 removedValue = iterator.value;
// assigning the next reference 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;
size--;
before = before.next;
}
E saved = iterator.value;
// 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;
return removedValue;
}
}
}
}

View File

@@ -0,0 +1,89 @@
package data_structures.Stacks;
import java.util.Scanner;
import java.util.Stack;
/**
*
* The nested brackets problem is a problem that determines if a sequence of
* brackets are properly nested. A sequence of brackets s is considered properly
* nested if any of the following conditions are true: - s is empty - s has the
* form (U) or [U] or {U} where U is a properly nested string - s has the form
* VW where V and W are properly nested strings For example, the string
* "()()[()]" is properly nested but "[(()]" is not. The function called
* is_balanced takes as input a string S which is a sequence of brackets and
* returns true if S is nested and false otherwise.
*
* @author akshay sharma
* @date: 2017-10-17
* @author <a href="https://github.com/khalil2535">khalil2535<a>
*
*/
class BalancedBrackets {
/**
*
* @param s
* @return
*/
static boolean is_balanced(String s) {
Stack<Character> bracketsStack = new Stack<>();
char[] text = s.toCharArray();
for (char x : text) {
switch (x) {
case '{':
case '<':
case '(':
case '[':
bracketsStack.push(x);
break;
case '}':
if (bracketsStack.peek() == '{') {
bracketsStack.pop();
break;
} else {
return false;
}
case '>':
if (bracketsStack.peek() == '<') {
bracketsStack.pop();
break;
} else {
return false;
}
case ')':
if (bracketsStack.peek() == '(') {
bracketsStack.pop();
break;
} else {
return false;
}
case ']':
if (bracketsStack.peek() == '[') {
bracketsStack.pop();
break;
} else {
return false;
}
}
}
return bracketsStack.empty();
}
/**
*
* @param args
* @TODO remove main method and Test using JUnit or other methodology
*/
public static void main(String args[]) {
try (Scanner in = new Scanner(System.in)) {
System.out.println("Enter sequence of brackets: ");
String s = in.nextLine();
if (is_balanced(s)) {
System.out.println(s + " is balanced");
} else {
System.out.println(s + " ain't balanced");
}
}
}
}

View File

@@ -15,6 +15,7 @@ class StackOfLinkedList {
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.printStack();
@@ -23,6 +24,8 @@ class StackOfLinkedList {
stack.pop();
stack.pop();
System.out.println("Top element of stack currently is: " + stack.peek());
}
}
@@ -75,12 +78,20 @@ class LinkedListStack {
System.out.println("Popped element is: " + temp.data);
}
public int peek() {
if (getSize() == 0) {
return -1;
}
return head.data;
}
public void printStack() {
Node temp = head;
System.out.println("Stack is printed as below: ");
while (temp != null) {
System.out.print(temp.data + " ");
System.out.println(temp.data + " ");
temp = temp.next;
}
System.out.println();
@@ -94,5 +105,5 @@ class LinkedListStack {
public int getSize() {
return size;
}
}

View File

@@ -53,16 +53,18 @@ class Stack{
* @return value popped off the Stack
*/
public int pop(){
if(isEmpty()){ //Checks for an empty stack
System.out.println("The stack is already empty");
return -1;
if(!isEmpty()){ //Checks for an empty stack
return stackArray[top--];
}
if(top < maxSize/4){
resize(maxSize/2);
return pop();// don't forget pop after resizing
}
else{
System.out.println("The stack is already empty");
return -1;
}
return stackArray[top--];
}
/**

View File

@@ -0,0 +1,330 @@
/**
*
* @author jack870131
*/
public class RedBlackBST {
private final int R = 0;
private final int B = 1;
private class Node {
int key = -1, color = B;
Node left = nil, right = nil, p = nil;
Node(int key) {
this.key = key;
}
}
private final Node nil = new Node(-1);
private Node root = nil;
public void printTree(Node node) {
if (node == nil) {
return;
}
printTree(node.left);
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
printTree(node.right);
}
public void printTreepre(Node node) {
if (node == nil) {
return;
}
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
printTree(node.left);
printTree(node.right);
}
private Node findNode(Node findNode, Node node) {
if (root == nil) {
return null;
}
if (findNode.key < node.key) {
if (node.left != nil) {
return findNode(findNode, node.left);
}
} else if (findNode.key > node.key) {
if (node.right != nil) {
return findNode(findNode, node.right);
}
} else if (findNode.key == node.key) {
return node;
}
return null;
}
private void insert(Node node) {
Node temp = root;
if (root == nil) {
root = node;
node.color = B;
node.p = nil;
} else {
node.color = R;
while (true) {
if (node.key < temp.key) {
if (temp.left == nil) {
temp.left = node;
node.p = temp;
break;
} else {
temp = temp.left;
}
} else if (node.key >= temp.key) {
if (temp.right == nil) {
temp.right = node;
node.p = temp;
break;
} else {
temp = temp.right;
}
}
}
fixTree(node);
}
}
private void fixTree(Node node) {
while (node.p.color == R) {
Node y = nil;
if (node.p == node.p.p.left) {
y = node.p.p.right;
if (y != nil && y.color == R) {
node.p.color = B;
y.color = B;
node.p.p.color = R;
node = node.p.p;
continue;
}
if (node == node.p.right) {
node = node.p;
rotateLeft(node);
}
node.p.color = B;
node.p.p.color = R;
rotateRight(node.p.p);
} else {
y = node.p.p.left;
if (y != nil && y.color == R) {
node.p.color = B;
y.color = B;
node.p.p.color = R;
node = node.p.p;
continue;
}
if (node == node.p.left) {
node = node.p;
rotateRight(node);
}
node.p.color = B;
node.p.p.color = R;
rotateLeft(node.p.p);
}
}
root.color = B;
}
void rotateLeft(Node node) {
if (node.p != nil) {
if (node == node.p.left) {
node.p.left = node.right;
} else {
node.p.right = node.right;
}
node.right.p = node.p;
node.p = node.right;
if (node.right.left != nil) {
node.right.left.p = node;
}
node.right = node.right.left;
node.p.left = node;
} else {
Node right = root.right;
root.right = right.left;
right.left.p = root;
root.p = right;
right.left = root;
right.p = nil;
root = right;
}
}
void rotateRight(Node node) {
if (node.p != nil) {
if (node == node.p.left) {
node.p.left = node.left;
} else {
node.p.right = node.left;
}
node.left.p = node.p;
node.p = node.left;
if (node.left.right != nil) {
node.left.right.p = node;
}
node.left = node.left.right;
node.p.right = node;
} else {
Node left = root.left;
root.left = root.left.right;
left.right.p = root;
root.p = left;
left.right = root;
left.p = nil;
root = left;
}
}
void transplant(Node target, Node with) {
if (target.p == nil) {
root = with;
} else if (target == target.p.left) {
target.p.left = with;
} else
target.p.right = with;
with.p = target.p;
}
Node treeMinimum(Node subTreeRoot) {
while (subTreeRoot.left != nil) {
subTreeRoot = subTreeRoot.left;
}
return subTreeRoot;
}
boolean delete(Node z) {
if ((z = findNode(z, root)) == null)
return false;
Node x;
Node y = z;
int yorigcolor = y.color;
if (z.left == nil) {
x = z.right;
transplant(z, z.right);
} else if (z.right == nil) {
x = z.left;
transplant(z, z.left);
} else {
y = treeMinimum(z.right);
yorigcolor = y.color;
x = y.right;
if (y.p == z)
x.p = y;
else {
transplant(y, y.right);
y.right = z.right;
y.right.p = y;
}
transplant(z, y);
y.left = z.left;
y.left.p = y;
y.color = z.color;
}
if (yorigcolor == B)
deleteFixup(x);
return true;
}
void deleteFixup(Node x) {
while (x != root && x.color == B) {
if (x == x.p.left) {
Node w = x.p.right;
if (w.color == R) {
w.color = B;
x.p.color = R;
rotateLeft(x.p);
w = x.p.right;
}
if (w.left.color == B && w.right.color == B) {
w.color = R;
x = x.p;
continue;
} else if (w.right.color == B) {
w.left.color = B;
w.color = R;
rotateRight(w);
w = x.p.right;
}
if (w.right.color == R) {
w.color = x.p.color;
x.p.color = B;
w.right.color = B;
rotateLeft(x.p);
x = root;
}
} else {
Node w = x.p.left;
if (w.color == R) {
w.color = B;
x.p.color = R;
rotateRight(x.p);
w = x.p.left;
}
if (w.right.color == B && w.left.color == B) {
w.color = R;
x = x.p;
continue;
} else if (w.left.color == B) {
w.right.color = B;
w.color = R;
rotateLeft(w);
w = x.p.left;
}
if (w.left.color == R) {
w.color = x.p.color;
x.p.color = B;
w.left.color = B;
rotateRight(x.p);
x = root;
}
}
}
x.color = B;
}
public void insertDemo() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Add items");
int item;
Node node;
item = scan.nextInt();
while (item != -999) {
node = new Node(item);
insert(node);
item = scan.nextInt();
}
printTree(root);
System.out.println("Pre order");
printTreepre(root);
break;
}
}
public void deleteDemo() {
Scanner scan = new Scanner(System.in);
System.out.println("Delete items");
int item;
Node node;
item = scan.nextInt();
node = new Node(item);
System.out.print("Deleting item " + item);
if (delete(node)) {
System.out.print(": deleted!");
} else {
System.out.print(": does not exist!");
}
System.out.println();
printTree(root);
System.out.println("Pre order");
printTreepre(root);
}
}

View File

@@ -19,19 +19,19 @@ public class TreeTraversal {
tree.insert(8);
// Prints 5 3 2 4 7 6 8
System.out.println("Preorder traversal:");
System.out.println("Pre order traversal:");
tree.printPreOrder();
System.out.println();
// Prints 2 3 4 5 6 7 8
System.out.println("Inorder traversal:");
System.out.println("In order traversal:");
tree.printInOrder();
System.out.println();
// Prints 2 4 3 6 8 7 5
System.out.println("Postorder traversal:");
System.out.println("Post order traversal:");
tree.printPostOrder();
System.out.println();
// Prints 5 3 7 2 4 6 8
System.out.println("Levelorder traversal:");
System.out.println("Level order traversal:");
tree.printLevelOrder();
System.out.println();
}
@@ -39,7 +39,7 @@ public class TreeTraversal {
/**
* The Node class which initializes a Node of a tree
* Consists of all 3 traversal methods: printInOrder, printPostOrder & printPreOrder
* Consists of all 4 traversal methods: printInOrder, printPostOrder printPreOrder & printLevelOrder
* printInOrder: LEFT -> ROOT -> RIGHT
* printPreOrder: ROOT -> LEFT -> RIGHT
* printPostOrder: LEFT -> RIGHT -> ROOT