You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classQueue {
intfront, rear, size;
intcapacity;
intarray[];
publicQueue(intcapacity)
{
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = newint[this.capacity];
}
// Queue is full when size becomes// equal to the capacitybooleanisFull(Queuequeue)
{
return (queue.size == queue.capacity);
}
// Queue is empty when size is 0booleanisEmpty(Queuequeue)
{
return (queue.size == 0);
}
// Method to add an item to the queue.// It changes rear and sizevoidenqueue(intitem)
{
if (isFull(this))
return;
this.rear = (this.rear + 1)
% this.capacity;
this.array[this.rear] = item;
this.size = this.size + 1;
System.out.println(item
+ " enqueued to queue");
}
// Method to remove an item from queue.// It changes front and sizeintdequeue()
{
if (isEmpty(this))
returnInteger.MIN_VALUE;
intitem = this.array[this.front];
this.front = (this.front + 1)
% this.capacity;
this.size = this.size - 1;
returnitem;
}
// Method to get front of queueintfront()
{
if (isEmpty(this))
returnInteger.MIN_VALUE;
returnthis.array[this.front];
}
// Method to get rear of queueintrear()
{
if (isEmpty(this))
returnInteger.MIN_VALUE;
returnthis.array[this.rear];
}
}
// Driver classpublicclassTest {
publicstaticvoidmain(String[] args)
{
Queuequeue = newQueue(1000);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
System.out.println(queue.dequeue()
+ " dequeued from queue\n");
System.out.println("Front item is "
+ queue.front());
System.out.println("Rear item is "
+ queue.rear());
}
}
Binary Tree
classNode
{
intkey;
Nodeleft, right;
publicNode(intitem)
{
key = item;
left = right = null;
}
}
// A Java program to introduce Binary TreeclassBinaryTree
{
// Root of Binary TreeNoderoot;
// ConstructorsBinaryTree(intkey)
{
root = newNode(key);
}
BinaryTree()
{
root = null;
}
publicstaticvoidmain(String[] args)
{
BinaryTreetree = newBinaryTree();
/*create root*/tree.root = newNode(1);
/* following is the tree after above statement 1 / \ null null */tree.root.left = newNode(2);
tree.root.right = newNode(3);
/* 2 and 3 become left and right children of 1 1 / \ 2 3 / \ / \ null null null null */tree.root.left.left = newNode(4);
/* 4 becomes left child of 2 1 / \ 2 3 / \ / \ 4 null null null / \ null null */
}
}
Binary Search Tree
Search
publicNodesearch(Noderoot, intkey)
{
// Base Cases: root is null or key is present at rootif (root==null || root.key==key)
returnroot;
// Key is greater than root's keyif (root.key < key)
returnsearch(root.right, key);
// Key is smaller than root's keyreturnsearch(root.left, key);
}
Insertion, Deletion and Traversal
classBinarySearchTree {
/* Class containing left and right child of current node * and key value*/classNode {
intkey;
Nodeleft, right;
publicNode(intitem)
{
key = item;
left = right = null;
}
}
// Root of BSTNoderoot;
// ConstructorBinarySearchTree() { root = null; }
// This method mainly calls deleteRec()voiddeleteKey(intkey) { root = deleteRec(root, key); }
/* A recursive function to delete an existing key in BST */NodedeleteRec(Noderoot, intkey)
{
/* Base Case: If the tree is empty */if (root == null)
returnroot;
/* Otherwise, recur down the tree */if (key < root.key)
root.left = deleteRec(root.left, key);
elseif (key > root.key)
root.right = deleteRec(root.right, key);
// if key is same as root's// key, then This is the// node to be deletedelse {
// node with only one child or no childif (root.left == null)
returnroot.right;
elseif (root.right == null)
returnroot.left;
// node with two children: Get the inorder// successor (smallest in the right subtree)root.key = minValue(root.right);
// Delete the inorder successorroot.right = deleteRec(root.right, root.key);
}
returnroot;
}
intminValue(Noderoot)
{
intminv = root.key;
while (root.left != null)
{
minv = root.left.key;
root = root.left;
}
returnminv;
}
// This method mainly calls insertRec()voidinsert(intkey) { root = insertRec(root, key); }
/* A recursive function to insert a new key in BST */NodeinsertRec(Noderoot, intkey)
{
/* If the tree is empty, return a new node */if (root == null) {
root = newNode(key);
returnroot;
}
/* Otherwise, recur down the tree */if (key < root.key)
root.left = insertRec(root.left, key);
elseif (key > root.key)
root.right = insertRec(root.right, key);
/* return the (unchanged) node pointer */returnroot;
}
// This method mainly calls InorderRec()voidinorder() { inorderRec(root); }
// A utility function to do inorder traversal of BSTvoidinorderRec(Noderoot)
{
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
// Driver Codepublicstaticvoidmain(String[] args)
{
BinarySearchTreetree = newBinarySearchTree();
/* Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 */tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
System.out.println(
"Inorder traversal of the given tree");
tree.inorder();
System.out.println("\nDelete 20");
tree.deleteKey(20);
System.out.println(
"Inorder traversal of the modified tree");
tree.inorder();
System.out.println("\nDelete 30");
tree.deleteKey(30);
System.out.println(
"Inorder traversal of the modified tree");
tree.inorder();
System.out.println("\nDelete 50");
tree.deleteKey(50);
System.out.println(
"Inorder traversal of the modified tree");
tree.inorder();
}
}
Index Mapping(Trivial Hashing) with -ves allowed
classHashing
{
finalstaticintMAX = 1000;
// Since array is global, it // is initialized as 0. staticboolean[][] has = newboolean[MAX + 1][2];
// searching if X is Present in // the given array or not. staticbooleansearch(intX)
{
if (X >= 0)
{
if (has[X][0] == true)
{
returntrue;
}
else
{
returnfalse;
}
}
// if X is negative take the // absolute value of X. X = Math.abs(X);
if (has[X][1] == true)
{
returntrue;
}
returnfalse;
}
staticvoidinsert(inta[], intn)
{
for (inti = 0; i < n; i++)
{
if (a[i] >= 0)
{
has[a[i]][0] = true;
}
else
{
has[Math.abs(a[i])][1] = true;
}
}
}
// Driver code publicstaticvoidmain(Stringargs[])
{
inta[] = {-1, 9, -5, -8, -5, -2};
intn = a.length;
insert(a, n);
intX = -5;
if (search(X) == true)
{
System.out.println("Present");
}
else
{
System.out.println("Not Present");
}
}
}
Priority Queue using Heap using Array
Ins- O(Log(n))
Del- O(Log(n))
Peek- O(1)
publicclassPQHeap {
privatestaticfinalintMAX_SIZE = 15;
privateint [] heap;
privateintsize;
publicPQHeap() {
heap = newint[MAX_SIZE];
size = 0;
}
// returns the index of the parent nodepublicstaticintparent(inti) {
return (i - 1) / 2;
}
// return the index of the left child publicstaticintleftChild(inti) {
return2*i + 1;
}
// return the index of the right child publicstaticintrightChild(inti) {
return2*i + 2;
}
// insert the item at the appropriate positionpublicvoidenqueue(intdata) {
if (size >= MAX_SIZE) {
System.out.println("The queue is full. Cannot insert");
return;
}
// first insert the time at the last position of the array // and move it upheap[size] = data;
size = size + 1;
// move up until the heap property satisfiesinti = size - 1;
while (i != 0 && heap[PQHeap.parent(i)] < heap[i]) {
inttemp = heap[i];
heap[i] = heap[parent(i)];
heap[parent(i)] = temp;
i = PQHeap.parent(i);
}
}
// moves the item at position i of array a// into its appropriate positionpublicvoidmaxHeapify(inti){
// find left child nodeintleft = PQHeap.leftChild(i);
// find right child nodeintright = PQHeap.rightChild(i);
// find the largest among 3 nodesintlargest = i;
// check if the left node is larger than the current nodeif (left <= size && heap[left] > heap[largest]) {
largest = left;
}
// check if the right node is larger than the current node // and left nodeif (right <= size && heap[right] > heap[largest]) {
largest = right;
}
// swap the largest node with the current node // and repeat this process until the current node is larger than // the right and the left nodeif (largest != i) {
inttemp = heap[i];
heap[i] = heap[largest];
heap[largest] = temp;
maxHeapify(largest);
}
}
// returns the maximum item of the heappublicintpeek() {
returnheap[0];
}
// deletes the max item and returnpublicintdequeue() {
intmaxItem = heap[0];
// replace the first item with the last itemheap[0] = heap[size - 1];
size = size - 1;
// maintain the heap property by heapifying the // first itemmaxHeapify(0);
returnmaxItem;
}
// prints the queuepublicvoidprintQueue() {
for (inti = 0; i < size; i++) {
System.out.print(heap[i] + " ");
}
System.out.println();
}
publicstaticvoidmain(String [] args) {
PQHeapqueue = newPQHeap();
queue.enqueue(43);
queue.enqueue(333);
queue.enqueue(345);
queue.enqueue(45);
queue.enqueue(3);
queue.enqueue(400);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
}
}