Implementation1
Implementation1
Implementation 1 Implementation 2
return i + 1;
import java.util.Arrays;
}
Implementation 3 Implementation 4
// Try visiting every city that has not been visited private static class HeapNode
// yet implements Comparable<HeapNode> {
for (int i = 0; i < n; i++) { int x;
if ((mask & (1 << i)) int y;
== 0) { int value;
Implementation 5 Implementation 6
Implementation 7 Implementation 8
smallest = li; }
if (ri < size && arr[ri] < arr[smallest]) }
smallest = ri; static void Delete(int i) {
/*smallest will store the minvalue index*/ Decreasekey(i, Integer.MIN_VALUE);
/*If the Minimum among the three nodes is the parent itself, ExtractMin();
that is Heap property satisfied so stop else call function recursively on Min }
if (smallest != ind) { static void print() {
int temp = arr[ind]; for (int i = 0; i < size; i++)
arr[ind] = arr[smallest]; System.out.print(arr[i] + " ");
arr[smallest] = temp; System.out.println();
Heapify(smallest); }
} }
}
static int getMin() { Sorting
return arr[0];
count inversions in array - for every 2 sorted arrays cout inversions using
}
merge sort
static int ExtractMin() {
if (size <= 0) median of 2 sorted arrays - how many from 1st array and second array
return Integer.MAX_VALUE; until n
if (size == 1) {
size--; static double medianOf2(int[] a, int[] b) {
return arr[0]; int n = a.length, m = b.length;
} int i = 0, j = 0;
int mini = arr[0];
arr[0] = arr[size - 1]; /*Copy last Node value to root Node value*/ // m1 to store the middle element
size--; // m2 to store the second middle element
Heapify(0); /*Call heapify on root node*/ int m1 = -1, m2 = -1;
return mini;
} // Loop till (m + n)/2
static void Decreasekey(int i, int val) { for (int count = 0; count <= (m + n) / 2; count++) {
arr[i] = val; /*Updating the new_val*/ m2 = m1;
while (i != 0 && arr[parent(i)] > arr[i]) /*Fixing the Min heap*/ {
int temp = arr[parent(i)]; // If both the arrays have remaining elements
arr[parent(i)] = arr[i]; if (i != n && j != m)
arr[i] = temp; m1 = (a[i] > b[j]) ? b[j++] : a[i++];
i = parent(i);
Implementation 9 Implementation 10
Implementation 11 Implementation 12
// If the predecessor's right child PreOrder Stack
// is already linked, remove the link,
// add current node to inorder list, // Java program to implement iterative preorder traversal
// and move to the right child
prev.right = null; class BinaryTree {
inorder.add(cur.val);
cur = cur.right; Node root;
}
} void iterativePreorder()
} {
iterativePreorder(root);
// Return the inorder }
// traversal result void iterativePreorder(Node node)
return inorder; {
}
// Base Case
public static void main(String[] args) { if (node == null) {
TreeNode root = new TreeNode(1); return;
root.left = new TreeNode(2); }
root.right = new TreeNode(3);
root.left.left = new TreeNode(4); // Create an empty stack and push root to it
root.left.right = new TreeNode(5); Stack<Node> nodeStack = new Stack<Node>();
root.left.right.right = new TreeNode(6); nodeStack.push(root);
Solution sol = new Solution(); /* Pop all items one by one. Do following for every popped item
a) print it
List<Integer> inorder = sol.getInorder(root); b) push its right child
c) push its left child
System.out.print("Binary Tree Morris Inorder Traversal: "); Note that right child is pushed first so that left is processed first */
for (int i = 0; i < inorder.size(); i++) { }
System.out.print(inorder.get(i) + " ");
} PostOrder 2 stacks
System.out.println(); class GfG {
}
} // Function to do post-order traversal
// using two stacks
Implementation 13 Implementation 14
// Collect all elements from second stack // If right child is also null
while (!stk2.isEmpty()) { // or already visited, it means
curr = stk2.pop(); // we can process the top node now
result.add(curr.data); if (temp == null) {
} // Get the top node from stack
Implementation 15 Implementation 16
temp = stack.pop();
// Add the node's data to // Change the prev for subsequent node
// the postorder traversal list next = node;
postorder.add(temp.data);
// Finally, set the next pointer in left subtree
// Check if there are more populateNext(node.left);
// nodes to pop whose right }
// subtree is also processed }
while (!stack.isEmpty() && temp == stack.peek().right) {
temp = stack.pop();
postorder.add(temp.data); //min max tournament
} ```
} else { static Pair getMinMax(int arr[], int low, int high)
// If right child exists, {
// set current node to it Pair minmax =new Pair();
curr = temp; Pair mml =new Pair();
} Pair mmr =new Pair();
} int mid;
}
// Return the list containing the // If there is only one elementif (low == high) {
// Postorder Traversal Sequence minmax.max = arr[low];
return postorder; minmax.min = arr[low];
} return minmax;
}
// Java program to populate inorder successor of all nodes
void populateNext(Node node) /* If there are two elements */if (high == low + 1) {
{ if (arr[low] > arr[high]) {
// The first visited node will be the rightmost node minmax.max = arr[low];
// next of the rightmost node will be NULL minmax.min = arr[high];
if (node != null) { }
// First set the next pointer in right subtree else {
populateNext(node.right); minmax.max = arr[high];
minmax.min = arr[low];
// Set the next as previously visited node in }
// reverse Inorder return minmax;
node.next = next; }
Implementation 17 Implementation 18
Implementation 19 Implementation 20
final K key;
Deque<String> deque = new LinkedList<>(); V value;
Node<K, V> next;
Add/Remove: addFirst(E e) , addLast(E e) , offerFirst(E e) , offerLast(E e) , removeFirst() ,
removeLast() , pollFirst() , pollLast() – Add or remove elements from both ends.
Node(K key, V value, Node<K, V> next) {
Peek: peekFirst() , peekLast() – View the first or last element without removing. this.key = key;
this.value = value;
Access: getFirst() , getLast() – Retrieve the first or last element.
this.next = next;
Size: size() , isEmpty() – Check the number of elements and if the deque is }
empty. }
Hashmap
private int hash(K key) {
Working return key.hashCode() % capacity;
}
public class MyHashMap<K, V> {
private static final int DEFAULT_CAPACITY = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f; public void put(K key, V value) {
int index = hash(key);
Node<K, V> node = table[index];
private int capacity; while (node != null) {
private float loadFactor; if (node.key.equals(key)) {
private int size; node.value = value;
private Node<K, V>[] table; return;
}
public MyHashMap() { node = node.next;
this(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR); }
} Node<K, V> newNode = new Node<>(key, value);
newNode.next = table[index];
table[index] = newNode;
public MyHashMap(int capacity, float loadFactor) { size++;
this.capacity = capacity; if (size > capacity * loadFactor) {
this.loadFactor = loadFactor; resize();
this.table = (Node<K, V>[]) new Node[capacity]; }
} }
Implementation 21 Implementation 22
Implementation 23 Implementation 24