Skip to content

Commit 6f51d8b

Browse files
committed
DS - Stack, Queue, Linkedlist, Hashing, Binary Search, Sorting, BST
0 parents  commit 6f51d8b

22 files changed

+1738
-0
lines changed

BST/BinarySearchTree.java

+498
Large diffs are not rendered by default.

BST/BinarySearchTreeGeneric.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package tree.bst;
2+
3+
import lombok.Getter;
4+
5+
public class BinarySearchTreeGeneric<E extends Comparable<E>> {
6+
@Getter
7+
Node<E> root;
8+
9+
public void insert(E data){
10+
Node<E> newNode = new Node<E>(data);
11+
12+
}
13+
14+
static class Node<E extends Comparable<E>>{
15+
E data;
16+
Node<E> left;
17+
Node<E> right;
18+
Node(E data) {
19+
this.data = data;
20+
left = right = null;
21+
}
22+
}
23+
}

BST/ConsumeTree.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tree.bst;
2+
3+
public class ConsumeTree {
4+
public static void main(String[] args) {
5+
BinarySearchTree bst = new BinarySearchTree();
6+
bst.root = bst.insertRecursive(25, bst.root);
7+
bst.root = bst.insertRecursive(15, bst.root);
8+
bst.root = bst.insertRecursive(17, bst.root);
9+
bst.root = bst.insertRecursive(18, bst.root);
10+
bst.root = bst.insertRecursive(10, bst.root);
11+
bst.root = bst.insertRecursive(30, bst.root);
12+
bst.root = bst.insertRecursive(26, bst.root);
13+
bst.root = bst.insertRecursive(31, bst.root);
14+
bst.root = bst.insertRecursive(27, bst.root);
15+
16+
System.out.println("Element found : "+bst.searchRecursive(bst.root, 15));
17+
System.out.println("Minimum : "+bst.findMinRecursive(bst.root) + " | Maximum : "+bst.findMaxRecursive(bst.root));
18+
System.out.println("Height or Maximum depth : "+bst.findHeightIterative());
19+
System.out.print("Level Order Traversal : "); bst.levelOrderTraversal();
20+
System.out.print("\nPre-Order Traversal : "); bst.preOrderTraversal(bst.root);
21+
System.out.print("\nIn-Order Traversal : "); bst.inOrderTraversal(bst.root);
22+
System.out.print("\nPost-Order Traversal : "); bst.postOrderTraversal(bst.root);
23+
24+
System.out.println("\nIs BST : "+bst.isBST());
25+
System.out.println("Is BST (Using inorder) : "+bst.isBSTUsingInorder());
26+
System.out.println("Is BST (Using inorder with extra array/list) : "+bst.isBSTUsingInorderWithExtraArray());
27+
28+
System.out.println("Root after deletion : " + bst.delete(bst.root, 18).getData());
29+
System.out.print("In-Order traversal after deletion : "); bst.inOrderTraversal(bst.root);
30+
31+
Integer successor = bst.findInOrderSuccessor(bst.root, 15) != null ? bst.findInOrderSuccessor(bst.root, 15).getData() : null;
32+
System.out.println("\nIn-Order successor : "+successor);
33+
34+
}
35+
}
36+
37+
38+
/*Node n = new Node(10);
39+
bst.root = n;
40+
n.left = new Node(20);
41+
n.right = new Node(30);*/

BST/ConsumeTreeGeneric.java

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tree.bst;
2+
3+
public class ConsumeTreeGeneric {
4+
5+
}

BinarySearch/BinarySearch.java

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package binarySearch.basic;
2+
3+
public class BinarySearch {
4+
/*
5+
A[] = {1, 2, 4, 5, 7, 8, 9};
6+
*/
7+
8+
/**
9+
* @param A - Input array. Array must be sorted in ascending order.
10+
* @param key - Element to be searched.
11+
* @return - Index of <b>{@code key}</b> if found, -1 otherwise.
12+
*/
13+
public static int binarySearch(Integer A[], Integer key){
14+
int mid, low=0, high=A.length-1;
15+
16+
while(low<=high){
17+
mid = (high + low)/2;
18+
19+
if(key==A[mid]){
20+
return mid;
21+
} else if(key<A[mid]) { // Discard right half of underlying array.
22+
high = mid-1;
23+
} else if(key>A[mid]) { // Discard left half of underlying array.
24+
low = mid+1;
25+
}
26+
}
27+
28+
return -1;
29+
}
30+
31+
/*
32+
A[] = {8, 9, 1, 2, 4, 5, 7};
33+
*/
34+
35+
/**
36+
* @param A - Input array. Array must not contain duplicate elements and array must be circularly sorted. e.g-
37+
* <p><code>A[] = {8, 9, 1, 2, 4, 5, 7}</code></p>
38+
* @param key - Element to be searched.
39+
* @return - Index of <b>{@code key}</b> if found, -1 otherwise.
40+
*/
41+
public static int binarySearchInCircularArray(Integer A[], Integer key) {
42+
int mid, low=0, high=A.length-1;
43+
44+
while(low<=high){
45+
mid = (high + low)/2;
46+
47+
if(key==A[mid])
48+
return mid;
49+
else if(A[low]<=A[mid]) { // Left half is sorted.
50+
if(key>=A[low] && key<A[mid]){
51+
high = mid-1;
52+
} else{
53+
low = mid+1;
54+
}
55+
} else if(A[mid]<=A[high]) { // Right half is sorted.
56+
if(key>A[mid] && key<=A[high]){
57+
low = mid+1;
58+
} else{
59+
high = mid-1;
60+
}
61+
62+
}
63+
}
64+
return -1;
65+
}
66+
67+
/*
68+
A[] = {8, 9, 1, 2, 4, 5, 7};
69+
*/
70+
71+
/**
72+
*
73+
* @param A - Input array. Array must not contain duplicate elements and array must be circularly sorted. e.g-
74+
* <p><code>A[] = {8, 9, 1, 2, 4, 5, 7}</code></p>
75+
* @return - A numeric value indicating the number of times this <code>array</code> has been rotated,
76+
* which is equal to the index of minimum element in this <code>array</code>.
77+
* <p>
78+
* For example if <code>A[] = {8, 9, 1, 2, 4, 5, 7}</code><br/>
79+
* then this function will return 2 (index of min. element). Which means that A is rotated 2 times.
80+
* </p>
81+
*/
82+
public static int rotateCountInCircularArray(Integer A[]) {
83+
int low = 0, high = A.length-1, mid;
84+
while(low<=high) {
85+
mid = (low+high)/2;
86+
87+
if(A[mid] < A[mid-1]){
88+
if(A[mid] < A[mid+1]){ // pivot property satisfied. And A[mid] is the minimum elt.
89+
return mid;
90+
} else { // if A[mid] > A[mid+1] then discard left half.
91+
low = mid + 1;
92+
}
93+
} else { // A[mid] > A[mid-1]
94+
if(A[mid] < A[mid+1]){ // discard right half.
95+
high = mid -1;
96+
} else { // if A[mid] > A[mid-1] then discard left half.
97+
low = mid + 1;
98+
}
99+
}
100+
}
101+
return -1;
102+
}
103+
104+
public static void main(String[] args) {
105+
Integer A[] = {5, 7, 8, 9, 1, 2, 4}; //{8, 9, 1, 2, 4, 5, 7};
106+
System.out.println("Index : " + binarySearchInCircularArray(A, 9));
107+
System.out.println("This array has been rotated "+ rotateCountInCircularArray(A)+" times.");
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package custom.hashtable;
2+
3+
public class ConsumeHashtable {
4+
public static void main(String... w) {
5+
CustomHashtable<Integer, String> hashTable = new CustomHashtable<Integer, String>(20);
6+
hashTable.put(10, "Ramu");
7+
hashTable.put(6, "Lanka");
8+
hashTable.put(3, "Vishu");
9+
hashTable.put(7, "Sood");
10+
hashTable.put(1, "Sashi");
11+
hashTable.put(5, "Nareddi");
12+
hashTable.put(2, "Dhar");
13+
hashTable.put(3, "Sona");
14+
hashTable.put(20, "Chaandi");
15+
hashTable.put(12, "Silver");
16+
hashTable.put(9, "Gold");
17+
hashTable.put(89, "Smith");
18+
hashTable.put(15, "Iron");
19+
hashTable.put(90, "Bronze");
20+
hashTable.put(14, "Metal");
21+
hashTable.put(80, "Hyderabad");
22+
hashTable.put(60, "Ramya");
23+
hashTable.put(70, "Telangana");
24+
25+
hashTable.remove(10);
26+
System.out.println("Size : "+hashTable.getSize()+"\n"+hashTable);
27+
28+
//System.out.println(hashTable.get(90));
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package custom.hashtable;
2+
3+
import lombok.Getter;
4+
5+
/**
6+
* Hashtable implementation similar to <code>java.util.HashMap</code> {@link java.util.HashMap}.
7+
* @author Suyash
8+
*/
9+
public class CustomHashtable<K, V> {
10+
/**
11+
* Each position in this array will represent a bucket, And this whole array will serve
12+
* as a hashtable.
13+
*/
14+
private Node<K,V> table[];
15+
16+
/**
17+
* Total number of buckets available in this hashtable, which is equal to <code>table.length</code>
18+
*/
19+
private Integer capacity;
20+
21+
/**
22+
* Total number of elements present in the hashtable at any moment.
23+
*/
24+
@Getter
25+
private Integer size;
26+
27+
public CustomHashtable(Integer capacity){
28+
this.capacity = capacity;
29+
table = new Node[capacity];
30+
size = 0;
31+
}
32+
33+
public V put(K key,V value){
34+
// Fetching the bucket(i.e. index) in which this element(key, value pair) needs to be stored.
35+
int index = getIndex(key);
36+
37+
// Creating a new node.
38+
Node<K,V> newNode = new Node<K,V>(key, value, null);
39+
40+
// Fetching the first element present(stored) in this bucket, if bucket is not empty. otherwise storedNode will be null.
41+
Node<K,V> storedNode = table[index];
42+
43+
// If target bucket is empty, Just store this element as a first element in this bucket.
44+
if(storedNode == null){
45+
table[index] = newNode;
46+
} else {
47+
// If target bucket is not empty then iterate through all the elements in this bucket in order to check if the this is a duplicate element(key).
48+
for(Node<K,V> i=storedNode; i!=null; i=i.next){
49+
// If this is duplicate element(key), replace the older value with newer value for this key.
50+
if(i.key.equals(key)){
51+
V oldVal = i.value;
52+
i.value = value;
53+
size++;
54+
return oldVal;
55+
}
56+
}
57+
58+
// If we came here, Which means that no such element was there and just insert this element as a first element in target bucket
59+
// in the form of linked list and adjust the link.
60+
table[index] = newNode;
61+
newNode.next = storedNode;
62+
}
63+
size++;
64+
return null;
65+
}
66+
67+
public V get(K key){
68+
int index = getIndex(key);
69+
Node<K,V> storedNode = table[index];
70+
for(Node<K,V> i=storedNode; i!=null; i=i.next){
71+
if(i.key.equals(key)) return i.value;
72+
}
73+
return null;
74+
}
75+
76+
public void remove(K key){
77+
// Fetching the bucket(i.e. index) in which this element(key, value pair) needs to be stored.
78+
int index = getIndex(key);
79+
80+
// Fetching the first element present(stored) in this bucket, if bucket is not empty. otherwise storedNode will be null.
81+
Node<K,V> storedNode = table[index];
82+
83+
if(storedNode!=null) {
84+
Node<K,V> prevStoredNode = null;
85+
86+
// Iterate through all the elements in this bucket until we found the match, Or all the elements have been visited.
87+
while (storedNode.next != null && !storedNode.key.equals(key)) {
88+
prevStoredNode = storedNode;
89+
storedNode = storedNode.next;
90+
}
91+
92+
if (storedNode.key.equals(key)) {
93+
94+
// If we found the match at the first position inside target bucket (Note-above while loop didn't run in this case.)
95+
if (prevStoredNode == null) {
96+
table[index] = storedNode.next;
97+
} else {
98+
prevStoredNode.next = storedNode.next;
99+
}
100+
size--;
101+
}
102+
}
103+
}
104+
105+
@Override
106+
public String toString() {
107+
String str="";
108+
for(int i=0; i<table.length; i++){
109+
str = str+"\nBucket-"+i+" : ";
110+
for(Node<K,V> j=table[i]; j!=null; j=j.next){
111+
str = str + j.value+" ";
112+
}
113+
}
114+
return str;
115+
}
116+
117+
/**
118+
*
119+
* @param key
120+
* @return bucket number corresponding to this key. Bucket number is calculated on the basis of hashcode of <code>key</code>.
121+
*/
122+
private int getIndex(K key){
123+
int hashCode = key.hashCode();
124+
int index = hashCode % capacity;
125+
return index;
126+
}
127+
128+
/**
129+
* An instance of this class represents a node/element.
130+
* Elements are distinguished on the basis of their <code>key</code>.
131+
*/
132+
static class Node<K, V>{
133+
final K key;
134+
V value;
135+
Node<K,V> next;
136+
137+
Node(K key, V value, Node<K,V> next){
138+
this.key = key;
139+
this.value = value;
140+
this.next = next;
141+
}
142+
}
143+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package custom.linked.list;
2+
3+
public class ConsumeDoublyLinkedList {
4+
public static void main(String[] args) {
5+
DoublyLinkedList<Integer> dll = new DoublyLinkedList<Integer>();
6+
dll.addFirst(1);
7+
dll.addLast(2);
8+
dll.addLast(3);
9+
dll.addLast(4);
10+
dll.addLast(5);
11+
dll.addLast(6);
12+
dll.addLast(7);
13+
dll.addLast(8);
14+
dll.addFirst(0);
15+
16+
dll.add(50, 2);
17+
18+
dll.delete(50);
19+
System.out.println(dll);
20+
}
21+
}

0 commit comments

Comments
 (0)