diff --git a/JAVA/collection/AbstractCollection.java b/JAVA/collection/AbstractCollection.java new file mode 100644 index 00000000..aa5e25d3 --- /dev/null +++ b/JAVA/collection/AbstractCollection.java @@ -0,0 +1,91 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public abstract class AbstractCollection implements Collection { + +protected AbstractCollection() { +} + +@Override +public abstract boolean add(Object object); + +@Override +public boolean isEmpty() { + return (size() == 0); +} + +@Override +public abstract Iterator iterator(); + +public boolean remove(Object object) { + Iterator it = iterator(); + if (object == null) { + while (it.hasNext()) { + if (it.next() == null) { + it.remove(); + return true; + } + } + } else { + while (it.hasNext()) { + if (object.equals(it.next())) { + it.remove(); + return true; + } + } + } + return false; +} + +@Override +public abstract int size(); + +@Override +public boolean contains(Object object) { + Iterator it = iterator(); + if (object == null) { + while (it.hasNext()) { + if (it.next() == null) { + return true; + } + } + } else { + while (it.hasNext()) { + if (object.equals(it.next())) { + return true; + } + } + } + return false; +} + +@Override +public String toString() { + if (isEmpty()) { + return "[ ]"; + } + Iterator it = iterator(); + StringBuffer buffer = new StringBuffer("[" + it.next()); + while (it.hasNext()) { + buffer.append("," + it.next()); + } + return (buffer + "]"); +} + +@Override +public void clear() { + for (Iterator it = iterator(); it.hasNext();) { + it.next(); + it.remove(); + } +} + +} diff --git a/JAVA/collection/ArrayCollection.java b/JAVA/collection/ArrayCollection.java new file mode 100644 index 00000000..36201284 --- /dev/null +++ b/JAVA/collection/ArrayCollection.java @@ -0,0 +1,69 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class ArrayCollection extends AbstractCollection { + +private final int INITIAL_LENGTH = 15; +private int size; +private Object[] a = new Object[INITIAL_LENGTH]; + +@Override +public boolean add(Object object) { + if (size == a.length) { + resizeArray(); + } + a[size++] = object; + return true; +} + +@Override +public int size() { + return size; +} + +@Override +public Iterator iterator() { + return new Iterator() { + private int i = 0; //index of current element + private boolean okToRemove = false; + + @Override + public boolean hasNext() { + return (i < size); + } + + @Override + public Object next() { + if (i == size) { + throw new RuntimeException(); + } + okToRemove = true; + return a[i++]; + } + + @Override + public void remove() { + if (!okToRemove) { + throw new IllegalStateException(); + } + a[--i] = a[--size]; + okToRemove = false; + } + }; +} + +public void resizeArray() { + Object[] aa = a; + a = new Object[2 * aa.length]; + System.arraycopy(aa, 0, a, 0, size); +} + +} diff --git a/JAVA/collection/Collection.java b/JAVA/collection/Collection.java new file mode 100644 index 00000000..1cbba10f --- /dev/null +++ b/JAVA/collection/Collection.java @@ -0,0 +1,20 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public interface Collection { + public boolean add(Object object); + public void clear(); + public boolean contains(Object object); + public boolean isEmpty(); + public Iterator iterator(); + public boolean remove(Object object); + public int size(); +} diff --git a/JAVA/collection/Country.java b/JAVA/collection/Country.java new file mode 100644 index 00000000..95d82763 --- /dev/null +++ b/JAVA/collection/Country.java @@ -0,0 +1,44 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class Country { + +private String name, language, iso; +private int area, population; + +public Country(String name, String iso, String language, int area, int population) { + this.name = name; + this.language = language; + this.iso = iso; + this.area = area; + this.population = population; +} + +@Override +public String toString() { + return ("[" + name + "," + iso + "," + language + "," + area + "," + population + "]"); +} + +public String getValue(String key) { + //mechanism to find the country with this key + return ("[" + name + "," + iso + "," + language + "," + area + "," + population + "]"); +} + +public static void main(String[] args) { + Country pak = new Country("Pakistan", "PK", "Urdu", 333333, 3333333); + Country bang = new Country("Bangladesh", "BN", "Bengali", 4444, 44444); + Country ind = new Country("India", "IN", "Hindi", 555555, 555555); + + System.out.println("Pakistan: " + pak); + System.out.println("Bangladesh: " + bang); + System.out.println("India: " + ind); +} +} diff --git a/JAVA/collection/Iterator.java b/JAVA/collection/Iterator.java new file mode 100644 index 00000000..a04b083a --- /dev/null +++ b/JAVA/collection/Iterator.java @@ -0,0 +1,16 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); +} diff --git a/JAVA/collection/LinkedCollection.java b/JAVA/collection/LinkedCollection.java new file mode 100644 index 00000000..405b91a4 --- /dev/null +++ b/JAVA/collection/LinkedCollection.java @@ -0,0 +1,83 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class LinkedCollection extends AbstractCollection { + +private int size; +private Node head = new Node(); + +@Override +public boolean add(Object object) { + head.previous = head.previous.next = new Node(object, head.previous, head); + size++; + return true; +} + +class Node { + +private Object object; +private Node previous, next; + +public Node() { + previous = this; + next = this; +} + +public Node(Object object, Node previous, Node next) { + this.object = object; + this.previous = previous; + this.next = next; +} +} + +@Override + +public Iterator iterator() { + + return new Iterator() { + private boolean okToRemove = false; + Node cursor = head.next; + + @Override + public boolean hasNext() { + return (cursor != head); + } + + @Override + public Object next() { + if (cursor == head) { + throw new RuntimeException(); + } + okToRemove = true; + Object obj = cursor.object; + cursor = cursor.next; + return obj; + } + + @Override + public void remove() { + if (!okToRemove) { + throw new IllegalStateException(); + } + cursor.previous = cursor.previous.previous; + cursor.previous.next = cursor; + size--; + okToRemove = false; + } + }; +} + +@Override +public int size() { + return size; +} + +} diff --git a/JAVA/collection/TestMyCollection.java b/JAVA/collection/TestMyCollection.java new file mode 100644 index 00000000..3324cdd3 --- /dev/null +++ b/JAVA/collection/TestMyCollection.java @@ -0,0 +1,41 @@ +/* + * 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 collection; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class TestMyCollection { + +public static void main(String[] args) { + ArrayCollection collection = new ArrayCollection(); + System.out.println("ArrayCollection:"); + + collection.add("Hello!"); + collection.add("boyz"); + collection.add("and"); + collection.add("girlz"); + System.out.println(collection); + collection.remove("and"); + System.out.println(collection); + + System.out.println("\n\nLinkedCollection:"); + LinkedCollection collection2 = new LinkedCollection(); + collection2.add("Hello!"); + collection2.add("boyz"); + collection2.add("and"); + collection2.add("girlz"); + System.out.println(collection2); + collection2.remove("and"); + System.out.println(collection2); + + System.out.println("Contains: "+collection.contains("boyz")); + System.out.println("Contains: "+collection2.contains("and")); + +} + +} diff --git a/JAVA/graphs/DPQ.java b/JAVA/graphs/DPQ.java new file mode 100644 index 00000000..bea17283 --- /dev/null +++ b/JAVA/graphs/DPQ.java @@ -0,0 +1,129 @@ +/* + * 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 graphs; + +/** + * + * @author Zohaib Hassan Soomro + * implementataion DijkStra's algorithm for finding shortest path using Priority Queue + */ +import java.util.*; +public class DPQ { + private int dist[]; //distance + private Set settled; + private PriorityQueue pq; + private int v; //number of vertices + List>adj; + + public DPQ(int v){ + this.v=v; + dist=new int[v]; + settled=new HashSet(); + pq=new PriorityQueue(v,new Node()); + } + + //Function for Dijkstra's algo + public void dijkstra(List> adj,int src){ + this.adj=adj; + for (int i=0;i> adj=new ArrayList>(); + + ////initialize List for every node + for (int i=0;i item=new ArrayList(); + adj.add(item); + } + + //inputs for DPQ graph + adj.get(0).add(new Node(1,9)); + adj.get(0).add(new Node(2,6)); + adj.get(0).add(new Node(3,5)); + adj.get(0).add(new Node(4,3)); + adj.get(2).add(new Node(1,2)); + adj.get(2).add(new Node(3,4)); + + //calculate single source shortest path + DPQ dpq=new DPQ(v); + dpq.dijkstra(adj,source); + + + //print the shortest path to all the nodes from the source node + System.out.println("The shortest path from node : "); + for (int i=0;i{ + public int node;public int cost; + + public Node(){} + public Node(int node,int cost){ + this.node=node; + this.cost=cost; + } + + @Override + public int compare(Node node1,Node node2){ + if(node1.cost < node2.cost) + return -1; + if(node1.cost > node2.cost) + return 1; + return 0; + } +} diff --git a/JAVA/graphs/LinkedGraph.java b/JAVA/graphs/LinkedGraph.java new file mode 100644 index 00000000..0e255e95 --- /dev/null +++ b/JAVA/graphs/LinkedGraph.java @@ -0,0 +1,88 @@ +/* + * 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 graphs; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class LinkedGraph { + int size; + List[] a; ////adjacency list + + public LinkedGraph(String args[]){ + size=args.length; + a=new List[size]; + for (int i=0;i loadFactor * entries.length) { + reHash(); + } + int h = hash(key); + for (int i = 0; i < entries.length; i++) { + int k = nextProbe(h, i); //finding next empty space to put our data + if (entries[k] == null) { + entries[k] = new Entry(key, value); + ++size; + ++used; + return null; + } + } + throw new IllegalStateException(); //table overflow +} + + +public int nextProbe(int hashedIndex, int loopVariable) { + return (hashedIndex + loopVariable) % entries.length; +} + +@Override +public Object get(Object key) { + int h = hash(key); + System.out.println("The key for " + key + " is " + h); + for (int i = 0; i < entries.length; i++) { + int k = nextProbe(h, i); + if (entries[k] == null) { + break; //means linear probing is not applied + } + if (entries[k] == NIL) { //means current element has no data skip this iteration + continue; + } + if (entries[k].key.equals(key)) { + return entries[k].value; + } + + } + return "Value not found!"; +} + +@Override +public Object remove(Object key) { + int h = hash(key); + for (int i = 0; i < entries.length; i++) { + int j = nextProbe(h, i); //finding next probe + if (entries[j] == null) { + break; + } + if (entries[j] == NIL) { + continue; + } + if (entries[j].key.equals(key)) { + Object value = entries[j].value; + entries[j] = NIL; + --size; + return value; + } + } + return "Value not found!"; +} + +public void reHash() { + Entry[] oldEntries = entries; + entries = new Entry[2 * oldEntries.length + 1]; //+1 make it odd number as even number causes max linear probing. + for (int i = 0; i < oldEntries.length; i++) { + Entry entry = oldEntries[i]; + if (entry == null || entry == NIL) { + continue; + } + int h = hash(entry.key); + for (int j = 0; j < entries.length; j++) { + int k = nextProbe(h, j); + if (entries[k] == null) { + entries[k] = entry; + break; + } + } + + } + used = size; +} + +@Override +public int size() { + return size; +} + +public void printAllTables() { + for (int i = 0; i < entries.length; i++) { + if (entries[i] != null) { + System.out.println(i + " " + entries[i].key + " has value " + entries[i].value); + } else { + System.out.println(i + " is empty."); + } + } +} + +public static void main(String[] args) { + CountryTable[] countries = new CountryTable[6]; + countries[0] = new CountryTable("Pakistan", "Urdu", 33, 222222222); + countries[1] = new CountryTable("Portugal", "Portugese", 33, 222222222); + countries[2] = new CountryTable("India", "Hindi", 33, 222222222); + countries[3] = new CountryTable("Japan", "Japanese", 33, 222222222); + countries[4] = new CountryTable("Australia", "English", 33, 222222222); + countries[5] = new CountryTable("Sweden", "Swedish", 33, 222222222); + HashTable table = new HashTable(9,0.5f); + table.put("PK", countries[0]); + table.put("PT", countries[1]); + table.put("IT", countries[2]); + table.put("AT", countries[3]); + table.put("DE", countries[4]); + table.put("SE", countries[5]); + table.put("GB", new CountryTable("United Kingdom", "English", 33, 222222222)); + table.remove("GB"); + table.printAllTables(); + System.out.println(table.get("GB")); +} + +} diff --git a/JAVA/map/Map.java b/JAVA/map/Map.java new file mode 100644 index 00000000..4b6714a6 --- /dev/null +++ b/JAVA/map/Map.java @@ -0,0 +1,21 @@ +/* + * 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 map; + +/** + * + * @author Zohaib Hassan Soomro + */ +public interface Map { + +public Object put(Object key, Object value); + +public Object get(Object key); + +public Object remove(Object key); + +public int size(); +} diff --git a/JAVA/queue/ArrayQueue.java b/JAVA/queue/ArrayQueue.java new file mode 100644 index 00000000..f7fcdd3c --- /dev/null +++ b/JAVA/queue/ArrayQueue.java @@ -0,0 +1,74 @@ +/* + * 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 queue; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class ArrayQueue implements Queue { + +private int size; +private Object array[]; + +public ArrayQueue(int capacity) { + array = new Object[capacity]; +} + +@Override +public Object first() { + if (this.isEmpty()) { + throw new IllegalStateException("Queue is empty!"); + } + return array[0]; +} + +@Override +public Object remove() { + if (this.isEmpty()) { + throw new IllegalStateException("Queue is empty!"); + } + Object obj = array[0]; + System.arraycopy(array, 1, array, 0, size); + array[--size] = null; + return obj; +} + +@Override +public void add(Object obj) { + if (size == this.array.length) { + resizeArray(); + } + array[size++] = obj; +} + +@Override +public int size() { + return size; +} + +@Override +public boolean isEmpty() { + return size == 0; +} + +public void resizeArray() { + Object[] array2 = this.array; + this.array = new Object[2 * size]; + System.arraycopy(array2, 0, this.array, 0, array2.length); +} + +public static void main(String[] args) { + ArrayQueue queue = new ArrayQueue(2); + queue.add(5); + queue.add(50); + queue.add("Hello!"); + System.out.println(queue.remove()); + System.out.println(queue.remove()); + System.out.println(queue.first()); +} + +} diff --git a/JAVA/queue/LinkedQueue.java b/JAVA/queue/LinkedQueue.java new file mode 100644 index 00000000..8e6ce02a --- /dev/null +++ b/JAVA/queue/LinkedQueue.java @@ -0,0 +1,87 @@ +/* + * 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 queue; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class LinkedQueue implements Queue { + +private int size; +private Node head = new Node(null); + +private class Node { + +private Object object; +private Node previous = this; +private Node next = this; + +public Node(Object obj) { + this.object = obj; +} + +public Node(Object object, Node previous, Node next) { + this.object = object; + this.previous = previous; + this.next = next; +} + +} + +@Override +public Object first() { + if (this.isEmpty()) { + throw new IllegalStateException("Queue is empty!"); + } + return head.next.object; +} + +@Override +public Object remove() { + if (this.isEmpty()) { + throw new IllegalStateException("Queue is empty!"); + } + Object firstElement = head.next.object; + head.next = head.next.next; + head.next.previous = head; + size--; + return firstElement; +} + +@Override +public void add(Object obj) { + head.previous = head.previous.next = new Node(obj, head.previous, head); + size++; +} + +@Override +public int size() { + return size; +} + +@Override +public boolean isEmpty() { + return size == 0; +} + +public static void main(String[] args) { + LinkedQueue carsBought = new LinkedQueue(); + LinkedQueue carsSold = new LinkedQueue(); + carsBought.add(1000); + carsBought.add(2000); + carsBought.add(500); + carsSold.add(1100); + carsSold.add(1999); + carsSold.add(600); + int totalProfit = 0; + int size = carsBought.size(); + for (int i = 0; i < size; i++) { + totalProfit += (int) carsSold.remove() - (int) carsBought.remove(); + } + System.out.println("Total Profit On these Three Cars: "+totalProfit); +} +} diff --git a/JAVA/queue/Queue.java b/JAVA/queue/Queue.java new file mode 100644 index 00000000..43066368 --- /dev/null +++ b/JAVA/queue/Queue.java @@ -0,0 +1,18 @@ +/* + * 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 queue; + +/** + * + * @author Zohaib Hassan Soomro + */ +public interface Queue { + public Object first(); + public Object remove(); + public void add(Object obj); + public int size(); + public boolean isEmpty(); +} diff --git a/JAVA/recursion/FabonacciRecursion.java b/JAVA/recursion/FabonacciRecursion.java new file mode 100644 index 00000000..9e1661f9 --- /dev/null +++ b/JAVA/recursion/FabonacciRecursion.java @@ -0,0 +1,28 @@ +/* + * 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 recursion; + +/** + * + * @author Zohaib Hassan Soomro + */ +//program to find nth term of fabonacci series: 0 1 1 2 3 5 8 13 21 34... +public class FabonacciRecursion { + public static void main(String[] args) { + /*for (int i=0;i<=10;i++) { + System.out.println("fabonacci("+i+") : "+fabonacci(i)); + }*/ + long start=System.currentTimeMillis(); + fabonacci(100); + long end=System.currentTimeMillis(); + System.out.println("Total Time: "+(end-start)+" mili-seconds."); + } + public static long fabonacci(int n){ + if(n<1) return 0; + if(n<3) return 1; + return fabonacci(n-1)+fabonacci(n-2); + } +} diff --git a/JAVA/recursion/FabonacciRecursionAnother.java b/JAVA/recursion/FabonacciRecursionAnother.java new file mode 100644 index 00000000..7f05cc46 --- /dev/null +++ b/JAVA/recursion/FabonacciRecursionAnother.java @@ -0,0 +1,32 @@ +/* + * 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 recursion; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class FabonacciRecursionAnother { + public static void main(String[] args){ + long array[]= new long[12]; + array[0]=0; + array[1]=1; + for (int i=2;i= 0; i--) { //as it is an stack so last element willl be the first element + string += String.valueOf(array[i] + ","); + } + string = string.substring(0, string.lastIndexOf(',')) + "]"; + return string; +} + +/////equals() compares two stacks +public boolean equals(Stack obj) { + if (this.size() != obj.size()) { + return false; + } + Object array[] = new Object[this.size()]; + Object array2[] = new Object[this.size()]; //as size of both are equal + boolean areEqual = true; + int i = 0; //counter variable + for (; i < array.length; i++) { + array[i] = this.pop(); //storing elements in array by popping so that we can store later same elements + array2[i] = obj.pop(); + if (!array[i].equals(array2[i])) { + areEqual = false; + } + } + while (--i >= 0) { + this.push(array[i]); //again inserting those elements in + obj.push(array2[i]); + } + return areEqual; +} + +////findLast() finds last element in the stack +public Object findLast() { + if (this.isEmpty()) { + throw new IllegalStateException("Stack is empty!"); + } + return array[0]; +} + +/////toLinkedStack() returns LinkedStack object equivalent to curent ArrayStack object +public LinkedStack toLinkedStack() { + if (this.isEmpty()) { + return null; + } + LinkedStack stack = new LinkedStack(); + for (int i = 0; i < this.size; i++) { + stack.push(array[i]); + } + return stack; +} + +public static void main(String[] args) { + ArrayStack stack = new ArrayStack(2); + stack.push(30); + stack.push("Hello1"); + stack.push(20); + ArrayStack stack2 = new ArrayStack(2); + stack2.push(30); + stack2.push("Hello"); + stack2.push(20); + System.out.println("stack.toString(): " + stack.toString()); + System.out.println("stack2.toString(): " + stack2.toString()); + System.out.println("stack.equals(stack2): " + stack.equals(stack2)); + System.out.println("stack.findLast(): " + stack.findLast()); + System.out.println("stack.toLinkedStack().toString(): " + stack.toLinkedStack().toString()); +} + +} diff --git a/JAVA/stack/LinkedStack.java b/JAVA/stack/LinkedStack.java new file mode 100644 index 00000000..e393c37a --- /dev/null +++ b/JAVA/stack/LinkedStack.java @@ -0,0 +1,148 @@ +/* + * 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. + */ +/** + * + * @author Zohaib Hassan Soomro + */ +package stack; + +import java.util.*; + +public class LinkedStack implements Stack { + +private int size; +private Node top; + +private class Node { + +private Object object; +private Node next; + +public Node(Object object, Node next) { + this.object = object; + this.next = next; +} + +} + +@Override +public Object peek() { + if (this.isEmpty()) { + throw new NoSuchElementException("Stack is Empty!"); + } + return top.object; +} + +@Override +public Object pop() { + if (this.isEmpty()) { + throw new NoSuchElementException("Stack is Empty!"); + } + Object obj = top.object; + top = top.next; + --size; + return obj; +} + +@Override +public void push(Object obj) { + top = new Node(obj, top); + size++; +} + +@Override +public int size() { + return size; +} + +@Override +public boolean isEmpty() { + return (size == 0); +} + +/////toString() converts all data of current object into an string +public String toString() { + if (this.isEmpty()) { + return ""; + } + String string = "["; + for (Node i = top; i != null; i = i.next) { + string += String.valueOf(i.object + ","); + } + string = string.substring(0, string.lastIndexOf(',')) + "]"; + return string; +} + +/////equals() compares two stacks +public boolean equals(Stack obj) { + if (this.size() != obj.size()) { + return false; + } + Object array[] = new Object[this.size()]; + Object array2[] = new Object[this.size()]; //as size of both are equal + boolean areEqual = true; + int i = 0; //counter variable + for (; i < array.length; i++) { + array[i] = this.pop(); //storing elements in array by popping so that we can store later same elements + array2[i] = obj.pop(); + if (!array[i].equals(array2[i])) { + areEqual = false; + } + } + while (--i >= 0) { + this.push(array[i]); //again inserting those elements in + obj.push(array2[i]); + } + return areEqual; +} + +////findLast() finds last element in the stack +public Object findLast() { + if (this.isEmpty()) { + throw new NoSuchElementException("Stack is empty!"); + } + Node i = top; + for (; i.next != null; i = i.next) //because if i.next=null it means i will be pointing to the last Object + { + } + return i.object; +} + +/////toArrayStack() returns ArrayStack object equivalent to curent LinkedStack object +public ArrayStack toArrayStack() { + if (this.isEmpty()) { + return null; + } + Object array[] = new Object[this.size]; + int count = 0; + ArrayStack stack = new ArrayStack(this.size); + for (Node i = top; i != null; i = i.next) { + array[count++] = i.object; //for preserving same order that's why storing elements in an Object array + } + for (int i = this.size - 1; i >= 0; i--) { + stack.push(array[i]); + } + return stack; +} + +public static void main(String[] args) { + LinkedStack stack = new LinkedStack(); + stack.push(4); + stack.push("Hello"); + stack.push(200); + LinkedStack stack2 = new LinkedStack(); + stack2.push(4); + stack2.push("Hello"); + stack2.push(200); + System.out.println("stack.toString(): " + stack.toString()); + System.out.println("stack2.toString(): " + stack2.toString()); + System.out.println("stack.equals(stack2): " + stack.equals(stack2)); + System.out.println("stack.findLast(): " + stack.findLast()); + System.out.println("stack.toArrayStack().toString(): " + stack.toArrayStack().toString()); + +} + +} diff --git a/JAVA/stack/Stack.java b/JAVA/stack/Stack.java new file mode 100644 index 00000000..b64345b3 --- /dev/null +++ b/JAVA/stack/Stack.java @@ -0,0 +1,19 @@ +/* + * 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 stack; + +/** + * + * @author Zohaib Hassan Soomro + */ +public interface Stack { + public Object peek(); + public Object pop(); + public void push(Object obj); + public int size(); + public boolean isEmpty(); + +} diff --git a/JAVA/tree/AVLTree.java b/JAVA/tree/AVLTree.java new file mode 100644 index 00000000..d3ac8ecb --- /dev/null +++ b/JAVA/tree/AVLTree.java @@ -0,0 +1,93 @@ +/* + * 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 tree; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class AVLTree { + + private int key,height; + private AVLTree left,right; + public static final AVLTree NIL=new AVLTree(); + + public AVLTree(int key){ + this.key=key; + left=right=NIL; + } + private AVLTree(){ + left=right=this; + height=-1; + } + private AVLTree(int key,AVLTree left,AVLTree right){ + this.key=key; this.left=left; this.right=right; + height= 1 + Math.max(left.height,right.height); + } + public int size(){ + if(this==NIL) + return 0; + return 1 + left.size() +right.size(); + } + + public String toString(){ + if(this==NIL) + return ""; + return left+" "+key+" "+right; + } + + public boolean add(int key){ + int oldSize=size(); + grow(key); + return size()>oldSize; + } + public AVLTree grow(int key){ + if(this==NIL) + return new AVLTree(key); + if(key==this.key) return this; //avoid key duplication + if(keyleft.height+1) { + if(right.left.height >right.right.height) + right.rotateRight(); + rotateLeft(); + } + else if (left.height>right.height+1) { + if(left.right.height > left.left.height) + left.rotateLeft(); + rotateRight(); + } + } + + public void rotateRight(){ + right=new AVLTree(key,left.right,right); + key=left.key; + left=left.left; + } + public void rotateLeft(){ + left=new AVLTree(key,left,right.left); + key=right.key; + right=right.right; + } + + public static void main(String[] args) { + AVLTree tree=new AVLTree(4); + tree.add(3); + tree.add(13); + tree.add(11); + tree.add(9); + tree.add(45); + tree.add(5); + System.out.println(tree.height); + System.out.println(tree); + } +} diff --git a/JAVA/tree/BinarySearchTree.java b/JAVA/tree/BinarySearchTree.java new file mode 100644 index 00000000..f8d30ceb --- /dev/null +++ b/JAVA/tree/BinarySearchTree.java @@ -0,0 +1,255 @@ +/* + * 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 tree; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class BinarySearchTree { +int key; +BinarySearchTree left, right; + +BinarySearchTree(int key) { + this.key = key; +} + +BinarySearchTree(int key, BinarySearchTree left, BinarySearchTree right) { + this.key = key; + this.left = left; + this.right = right; +} + +/*//in-order traversal of Binary Tree +public void inOrder() { + if(this==null) + System.out.println(""); + if(this.left==null) + System.out.print("."); + else{ + System.out.print(left.key+"=>"); + left.inOrder(); + } + System.out.print(key); + if(this.right==null) + System.out.println("."); + else{ + System.out.println("<="+right.key); + right.inOrder(); + } +}*/ + +//in-order traversal of Binary Tree +public String toString() { + if(this==null) + return " "; + String buf=""; + if(left!=null) + buf+=left+" "; + buf+=key; + if (right!=null) + buf+=" "+right; + return buf; +} + +public String preOrder() { + if(this==null) + return " "; + String buf=key+""; + if(left!=null) + buf+=" "+left.preOrder(); + if (right!=null) + buf+=" "+right.preOrder(); + return buf; +} +public String postOrder() { + if(this==null) + return " "; + String buf=""; + if(left!=null) + buf+=left.postOrder()+" "; + if (right!=null) + buf+=right.postOrder()+" "; + buf+=key; + return buf; +} +public void traverse(){ + System.out.println("Inorder : "+toString()); + System.out.println("Preorder : "+preOrder()); + System.out.println("Postorder: "+postOrder()); +} + +public BinarySearchTree insert(int key){ + return insert(this,key); +} +private BinarySearchTree insert(BinarySearchTree root, int key){ + if(root==null) + return new BinarySearchTree(key); + if(keyroot.key){ + root.right=insert(root.right,key); + } + else if(key==root.key){ + System.out.println("Duplicate values not allowed here!"); + return root; + } + return root; +} + +public boolean search(int key){ + return search(this,key); +} +private boolean search(BinarySearchTree root, int key){ + if(root==null) + return false; + if(keyroot.key){ + return search(root.right,key); + } + else if(key==root.key){ + return true; + } + return false; +} + +public boolean delete(int key){ + return delete(this,key); +} + +public boolean delete(BinarySearchTree root,int key){ + if(root==null) + return false; + if(keyroot.key) + return right.delete(right,key); + if(root.left==null && root.right==null){ + root=null; + return true; + } + if (root.right==null) { + root=root.left; + root.right=root.left.right; + root.left=root.left.left; + return true; + } + + if (root.left==null) { + root=root.right; + root.left=root.right.left; + root.right=root.right.right; + return true; + } + + if(root.right.left==null){ + root=root.right; + root.right=root.right.right; + return true; + } + + root=deleteMinimum(root.right); + return true; +} + +private BinarySearchTree deleteMinimum(BinarySearchTree root){ + if(root.left.left==null){ + BinarySearchTree temp=root.left; + root.left=root.left.right; + return temp; + } + return root.deleteMinimum(root.left); +} +/*private BinarySearchTree delete(BinarySearchTree root, int key){ + if(root==null) + return root; + if(!search(root,key)){ + System.out.println("Value not found!"); + return root; + } + if(keyroot.key) + return delete(root.right,key); + if(key==root.key && root.left==null && root.right==null){ + BinarySearchTree p=getParent(root); + if(p.left==root) + return p.left=root.left; //return root=root.right + else if(p.right==root) + return p.right=root.right; + } + if(root.right==null){ + BinarySearchTree p=getParent(root); + if(p.left==root) + return p.left=root.left; //return root=root.right + else if(p.right==root) + return p.right=root.left; + } + if(root.left==null){ + BinarySearchTree p=getParent(root); + if(p.left==root) + return p.left=root.right; //return root=root.right + else if(p.right==root) + return p.right=root.right; + } + BinarySearchTree p=getParent(root); + if(p.left==root) + return p.left=deleteMinimum(root.right);//return root=root.right + else + return p.right=deleteMinimum(root.right); +} +private BinarySearchTree getParent(BinarySearchTree node){ + BinarySearchTree p=this; + if(p==node) + return null; //there's no parent + if(p.left==node || p.right==node) + return p; + if(p.left!=null) + p=p.left.getParent(node); + if(p.right!=null) + p=p.right.getParent(node); + return p; +}*/ + +/*private BinarySearchTree deleteMinimum(BinarySearchTree root){ + if(root==null) + return root; + if(root.left==null){ + BinarySearchTree temp=root; + root=root.right; + return temp; + } + if(root.left.left==null && root.left.right==null){ + BinarySearchTree temp=root; + root=root.left; + return temp; + } + return deleteMinimum(root.left); +}*/ + +public static void main(String[] args) { + + BinarySearchTree treeA=new BinarySearchTree(3); + treeA=treeA.insert(9); + treeA=treeA.insert(2); + treeA=treeA.insert(8); + treeA=treeA.insert(6); + treeA=treeA.insert(4); + treeA=treeA.insert(10); + treeA=treeA.insert(11); + treeA=treeA.insert(56); + //treeA.inOrder(); + System.out.println(treeA); + treeA.delete(4); + System.out.println(treeA); + //System.out.println(treeA.search(57)); + + +} +} diff --git a/JAVA/tree/BinaryTree.java b/JAVA/tree/BinaryTree.java new file mode 100644 index 00000000..f7ae5a05 --- /dev/null +++ b/JAVA/tree/BinaryTree.java @@ -0,0 +1,162 @@ +/* + * 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 tree; + +/** + * + * @author Zohaib Hassan Soomro + */ +public class BinaryTree { + +Object root; +BinaryTree left, right; + +BinaryTree(Object root) { + this.root = root; +} + +BinaryTree(Object root, BinaryTree left, BinaryTree right) { + this.root = root; + this.left = left; + this.right = right; +} + +//in-order traversal of Binary Tree +@Override +public String toString() { + String buf = ""; + if (left != null) + buf += left + ","; // or buf+=left.toString()+"," + + buf += root; + + if (right != null) + buf += "," + right; // or buf+=left.toString()+"," + + return buf; +} + +//pre-order traversal of Binary Tree +public String preOrder() { + String buf = ""; + buf += root; + if (left != null) + buf += "," + left.preOrder(); + + if (right != null) + buf += "," + right.preOrder(); + + return buf; +} + +//post-order traversal of Binary Tree +public String postOrder() { + String buf = ""; + if (left != null) + buf += left.postOrder() + ","; + + if (right != null) + buf +=right.postOrder()+","; + buf+=root; + return buf; +} + +public boolean isLeaf(){ + if(left==null && right==null) + return true; + return false; +} +public int size(){ + if(root==null) return 0; + if(left==null && right==null) return 1; + if(left==null) + return 1+right.size(); + if(right==null) + return 1+left.size(); + return 1+left.size()+right.size(); +} + +public int height(){ + if(root==null) return -1; + if(left==null && right==null) return 0; + int lefts=0,rights=0; + if(left!=null) + lefts= 1+left.height(); + if(right!=null) + rights= 1+right.height(); + return (lefts>rights) ? lefts : rights; +} +public boolean contains(BinaryTree node){ + if(node.root.equals(root)) // if(node.root==root) + return true; + if(left!=null){ + if(node==left) + return true; + left.contains(node); + } + if(right!=null){ + if(node==right) + return true; + right.contains(node); + } + return false; +} +/* +public boolean contains(BinaryTree node){ + if(this.toString().contains(node.toString())) + return true; + return false; +} +*/ + +/* +Sir's method +public boolean contains(Object obj){ + if(root==object) + return true; + boolean contained=false; + if(left!=null){ + if(left.toString().contains(obj.toString())) + return true; + contained=left.contains(obj); + } + if(right!=null){ + if(right.toString().contains(obj.toString())) + return true; + contained=right.contains(node); + } + return contained; +} +*/ + + +public static void main(String[] args) { + //level wise tree nodes spacing(b/w lines of code) + + //leaf nodes //lower level + BinaryTree treeG=new BinaryTree("G"); + BinaryTree treeH=new BinaryTree("H"); + BinaryTree treeI=new BinaryTree("I"); + BinaryTree treeJ=new BinaryTree("J"); + + //2nd lower level + BinaryTree treeD=new BinaryTree("D",treeG,treeH); + BinaryTree treeE=new BinaryTree("E"); + BinaryTree treeF=new BinaryTree("F",treeI,treeJ); + + BinaryTree treeB=new BinaryTree("B",treeD,treeE); + BinaryTree treeC=new BinaryTree("C",treeF,null); + + BinaryTree treeA=new BinaryTree("A",treeB,treeC); + + System.out.println(treeA.postOrder()); + System.out.println(treeA.size()); + System.out.println(treeA.isLeaf()); + System.out.println(treeA.height()); + System.out.println(treeG.contains(treeB)); + +} +}