Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions Data Structures/BST/C/AVL.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
#include<stdio.h>
#include<stdlib.h>

// An AVL tree node
struct Node
{
int key;
struct Node *left;
struct Node *right;
int height;
};

// A utility function to get maximum of two integers
int max(int a, int b);

// A utility function to get the height of the tree
int height(struct Node *N)
{
if (N == NULL)
return 0;
return N->height;
}

// A utility function to get maximum of two integers
int max(int a, int b)
{
return (a > b)? a : b;
}

/* Helper function that allocates a new node with the given key and
NULL left and right pointers. */
struct Node* newNode(int key)
{
struct Node* node = (struct Node*)
malloc(sizeof(struct Node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // new node is initially added at leaf
return(node);
}

// A utility function to right rotate subtree rooted with y
// See the diagram given above.
struct Node *rightRotate(struct Node *y)
{
struct Node *x = y->left;
struct Node *T2 = x->right;

// Perform rotation
x->right = y;
y->left = T2;

// Update heights
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;

// Return new root
return x;
}

// A utility function to left rotate subtree rooted with x
// See the diagram given above.
struct Node *leftRotate(struct Node *x)
{
struct Node *y = x->right;
struct Node *T2 = y->left;

// Perform rotation
y->left = x;
x->right = T2;

// Update heights
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;

// Return new root
return y;
}

// Get Balance factor of node N
int getBalance(struct Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}

// Recursive function to insert a key in the subtree rooted
// with node and returns the new root of the subtree.
struct Node* insert(struct Node* node, int key)
{
/* 1. Perform the normal BST insertion */
if (node == NULL)
return(newNode(key));

if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
else // Equal keys are not allowed in BST
return node;

/* 2. Update height of this ancestor node */
node->height = 1 + max(height(node->left),
height(node->right));

/* 3. Get the balance factor of this ancestor
node to check whether this node became
unbalanced */
int balance = getBalance(node);

// If this node becomes unbalanced, then
// there are 4 cases

// Left Left Case
if (balance > 1 && key < node->left->key)
return rightRotate(node);

// Right Right Case
if (balance < -1 && key > node->right->key)
return leftRotate(node);

// Left Right Case
if (balance > 1 && key > node->left->key)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Case
if (balance < -1 && key < node->right->key)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}

/* return the (unchanged) node pointer */
return node;
}

// A utility function to print preorder traversal
// of the tree.
// The function also prints height of every node
void preOrder(struct Node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}

/* Drier program to test above function*/
int main()
{
struct Node *root = NULL;

/* Constructing tree given in the above figure */
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);

/* The constructed AVL Tree would be
30
/ \
20 40
/ \ \
10 25 50
*/

printf("Preorder traversal of the constructed AVL"
" tree is \n");
preOrder(root);

return 0;
}
77 changes: 77 additions & 0 deletions Data Structures/Heaps/Java/minHeap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.io.*;
import java.util.*;

class min_heap
{
int[] a;int i=0;
min_heap(int n)
{
a=new int[n+1];
}
void insert(int x)
{
i++;
a[i]=x;int j=i;
int par=(int)Math.floor(j/2);
while(j>1 && a[j]<a[par])
{
int t=a[j];a[j]=a[par];a[par]=t;
j=par;
par=(int)Math.floor(j/2);
}
}
void heapify(int j)
{
int left=j*2,right;right=left+1;int smal=j;
if(left<=i && a[j]>a[left])
{
smal=left;
}
if(right<=i && a[smal]>a[right])
{
smal=right;
}
if(smal!=j)
{
int t=a[j];a[j]=a[smal];a[smal]=t;
heapify(smal);
}
}
void delete(int data)
{
int j=1;
for(j=1;j<=i;j++)
{
if(a[j]==data)
break;
}
a[j]=a[i];i--;
heapify(j);
}
}

class NN
{

public static void main(String[] args)throws IOException {
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));

int g=Integer.parseInt(bf.readLine());
min_heap h=new min_heap(g+1);
for(int h1=0;h1<g;h1++)
{
String[] s=bf.readLine().split(" ");
if(s[0].equals("3"))
System.out.println(h.a[1]);
else if(s[0].equals("1"))
h.insert(Integer.parseInt(s[1]));
else
h.delete(Integer.parseInt(s[1]));
}





}
}
79 changes: 79 additions & 0 deletions Graph/Java/BFS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.io.*;
import java.util.*;

// This class represents a directed graph using adjacency list
// representation
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists

// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}

// Function to add an edge into the graph
void addEdge(int v,int w)
{
adj[v].add(w);
}

// prints BFS traversal from a given source s
void BFS(int s)
{
// Mark all the vertices as not visited(By default
// set as false)
boolean visited[] = new boolean[V];

// Create a queue for BFS
LinkedList<Integer> queue = new LinkedList<Integer>();

// Mark the current node as visited and enqueue it
visited[s]=true;
queue.add(s);

while (queue.size() != 0)
{
// Dequeue a vertex from queue and print it
s = queue.poll();
System.out.print(s+" ");

// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
}
}
}
}

// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);

g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);

System.out.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");

g.BFS(2);
}
}
Loading