Drop Down

Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Tuesday, March 5, 2019

Binary Tree 1

package tree;

public class Node
{
int key;
Node left;
Node right;
}
=======================================================================
package tree;

public class BinaryTree 
{
Node root= null;
Node temp;
public void add(int value) 
{
root = insertRec(root, value);
}
//---------------------------------------
public void inOrderintraverse() 
{
intraverse(root);
}
public void preOrderTraverse() 
{
   pretraverse(root);
}
public void postOrderTraverse() 
{
posttraverse(root);
}
//----------------------------------------
//*****************************************
private Node insertRec(Node root, int value) 
{
if(root == null)
{
Node node = new Node();
node.key = value;
node.left = null;
node.right = null;
root = node;
return node;
}
else
{
if(value < root.key)
root.left = insertRec(root.left, value);
else if(value > root.key)
root.right = insertRec(root.right, value);
}
  
return root;
}
//*************************************
private void intraverse(Node root) 
{
  if(root!= null)
  {    
  intraverse(root.left);
  System.out.println(root.key+" ");
  intraverse(root.right);  
  }
}
//*************************************
private void pretraverse(Node root) 
{
if(root!= null)
  {  
System.out.println(root.key+" ");
pretraverse(root.left);
pretraverse(root.right);  
  }   
}
//*************************************
private void posttraverse(Node root) 
{
if(root!= null)
  {  
posttraverse(root.left);
posttraverse(root.right);
  System.out.println(root.key+" ");
  }   
}
}
======================================================================

package tree;

public class Runner {

public static void main(String[] args) 
{
  BinaryTree tree = new BinaryTree();
  tree.add(50);
  tree.add(30);
  tree.add(20);
  tree.add(35);
  tree.add(55);
  tree.add(52);
  tree.add(60);  
/* System.out.println("InOrder:");
  tree.inOrderintraverse();*/
System.out.println("PreOrder:");
  tree.preOrderTraverse();
  /* System.out.println("PostOrder:");
  tree.postOrderTraverse();
*/
}

}
===================================================================

Stack implementation

package ds.Stack;

public class Runner {

public static void main(String[] args)
{
  Stack stack = new Stack();
  stack.push(10);
  //stack.push(20);
// stack.push(30);

  stack.show();
  int pop1 = stack.pop();
  System.out.println("");
  System.out.println("Popped: "+pop1);
  stack.show();
 
  int peek1 = stack.peek();
  System.out.println("");
  System.out.println("Peeked: "+peek1);
  stack.show();
 
  System.out.println("");
  int size = stack.size();
  System.out.println("Size is:"+size);
 
  System.out.println("");
  stack.isEmpty();
 

}

}
=====================================================================

package ds.Stack;

public class Stack 
{
int[] stack = new int[5];
int  top = 0;
public void push(int i) 
{
if(top==5)
{
System.out.println("Stack is full");
return;
}
else
{
stack[top] = i;
top++;
}
}

public void show() 
{
for(int n :stack)
System.out.print(n+" ");
}

public int pop() 
{
if(top<=0)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int data;
top--;
data  = stack[top];
stack[top]= 0;
return data;
}
}

public int  peek() 
{
if(top<=0)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int data;
data  = stack[top -1];
return data;
}
}

public int size() 
{
  return (top);
}

public void  isEmpty() 
{
  if(top<=0)
  System.out.println("Stack is Empty");
  
  else
  System.out.println("Stack is NOT Empty");
  
}

}

========================================================================

Linked list Implementation

package ds.Linked_List;

public class Node
{
   int data;
   Node next;
 
}

========================================================================
package ds.Linked_List;

public class Runner {

public static void main(String[] args) 
{
Linked_List list = new Linked_List();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.show();
System.out.println("After delting 50");
list.delete(50);
list.show();
System.out.println("add at first - 100");
list.addAtFirst(100);
list.show();
}

}
========================================================================
package ds.Linked_List;

public class Linked_List 
{
Node head;
  void add(int data)
  {
  Node node = new Node();
  node.data=data;
  node.next=null;
  if(head == null)
  {
  head = node;
  }
  else
  {
  Node n = head;
  while(n.next != null)
  {
n = n.next;
  }
  n.next = node;
  }
  
  }
  void show()
  {
  Node n = head;
  while(n.next!= null)
  {
  System.out.println(n.data);
  n = n.next;
  }
  System.out.println(n.data);
  }
  void delete(int data)
  {
  Node n = head;
  Node pre = null;
  //For first nod
  if(n.data == data)
  {
  head = n.next;
  }
  //for middle
  else
  {   
  while(n.data!= data)
  {
  pre = n;
  n = n.next;
  }
  pre.next = n.next;
  n.next = null;
  }
  
  }
  void addAtFirst(int data)
  {
 
  Node node = new Node();
  node.data = data;
  node.next = head;
  head = node;
  
  }
}
========================================================================

SelectionSort

package ds.interview.dataStructure.Algo;

import java.util.Arrays;

//Selection Sort : Select min. value and swap with first element
//In Every Lap Smallest element comes at first
public class SelectionSort {

public static void main(String[] args)
{
int min,temp;
int arr[] = {5,8,2,9,1,4,3,7};
for(int i =0;i<arr.length;i++)
{
min = i;
for(int j = i+1;j<arr.length;j++)
{
if(arr[j]<arr[min])
{
min = j;
}
}
temp = arr[i];
arr[i] = arr[min];
arr[min]= temp;
}
System.out.println(Arrays.toString(arr));

}

}

MergeSort

package ds.interview.dataStructure.Algo;
//-----------------------------------------PENDING
import java.util.Arrays;

//https://www.youtube.com/watch?v=TzeBrDU-JaY&list=PL2_aWCzGMAwKedT2KfDMB9YA5DgASZb3U&index=5
//MergeSort
public class MergeSort
{

public static void main(String[] args)
{
  int[] arr = {3,5,8,9,1,6,4,2};
  mergesort(arr);
  System.out.println(Arrays.toString(arr));
}

private static void mergesort(int[] arr)
{
  int length = arr.length;
  if(length<2)
  {
  return;
  }
  else
  {
  int mid = length/2; //-------divide
  int[]left = new int[mid];//-------Left Array
  int[]right = new int[length-mid];//-------Right Array
  //Fill Left Array
  for(int i=0;i<left.length;i++)
  {
  left[i] = arr[i];
  }
  //Fill Right Array
  for(int i=mid+1;i<right.length;i++)
  {
  right[i] = arr[i];
  }
  mergesort(left);
  mergesort(right);
  merge(left,right,arr);
 
 
  }

}

private static void merge(int[] left, int[] right, int[] arr)
{

int i=0; //-----for left
int j=0;//------for right
int k=0;//------for main array
while(i<left.length && j<right.length)
{
if(left[i]<right[j])
{
arr[k] = left[i];
i++;k++;
}
else
    if(left[i]>right[j])
{
arr[k] = right[j];
j++;k++;
}
while(i<left.length)
{
arr[k] = left[i];
i++;k++;
}
while(i<left.length)
{
arr[k] = right[j];
j++;k++;
}
}
}

}

InsertionSort

package ds.interview.dataStructure.Algo;
//Insertion sort
//starts from index 1, and compares itself with previous values to adjust itself
import java.util.Arrays;
public class InsertionSort {

public static void main(String[] args)
{
int temp;
  int arr[] = {4,8,6,2,9,1,7,3};
  for(int i = 1;i<arr.length;i++)
  {
for(int j =i ;j>0 ;j--)
{
if(arr[j]<arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1]= temp;
}
}
  }
  System.out.println(Arrays.toString(arr));
 
}

}

BubbleSort

package ds.interview.dataStructure.Algo;
//Bubble Sort
//In Every Lap Biggest element comes at last

import java.util.Arrays;

public class BubbleSort {

public static void main(String[] args)
{
  int[] arr = {4,7,8,3,2,9,10,1};
  System.out.println("Original: "+Arrays.toString(arr));
  for(int i = 0;i<arr.length-1;i++)
  {
  for(int j=0;j<arr.length-1-i;j++) //"-i" coz. 2nd round me last wale se compare se bache
  {
  if(arr[j]>arr[j+1])
  {
  arr[j]= arr[j]+arr[j+1];
  arr[j+1]= arr[j]-arr[j+1];
  arr[j]=arr[j]-arr[j+1];
  }
  }
  }
 
  //***************Display Sorted elements**********
  System.out.println("Sorted: "+Arrays.toString(arr));
 

}

}

Dynamic_Stack_Implementation

package ds.Dynamic_Stack;

public class Runner {

public static void main(String[] args)
{
  Stack stack = new Stack();
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.size();
  stack.push(10);
  stack.size();
  stack.push(10);
  stack.size();
  stack.pop();
  stack.size();
  stack.pop();
  stack.pop();
  stack.size();
  stack.pop();
  stack.size();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.pop();
  stack.size();

}

}
====================================================================

package ds.Dynamic_Stack;

public class Stack 
{
int[] stack = new int[2];
int  top = 0;
int capacity = 2;
public void push(int data) 
{
if(top == capacity)
expand();
stack[top] = data;
top++;
}

private void expand() 
{
System.out.println("Expanding");
int[] newStack = new int[capacity*2];
System.arraycopy(stack, 0, newStack, 0, stack.length);
stack = newStack;
capacity = capacity *2;
}

public void show() 
{
for(int n :stack)
System.out.print(n+" ");
}

public void pop() 
{
int data;
if(top<=0)
{
System.out.println("Stack is Empty");
return;
}
else
{
top--;
data  = stack[top];
stack[top]= 0;
shrink();
}
System.out.println("Popped Data: "+data);
}

private void shrink() 
{
System.out.println("Shrinked");
  int size = stack.length;
  if(size<capacity/2)
  {
  int s1 = capacity/2;
  int[] ShrinkedArray = new int[s1];
  System.arraycopy(stack, 0, ShrinkedArray, 0, stack.length);
  stack = ShrinkedArray;
  }
}

public int  peek() 
{
if(top<=0)
{
System.out.println("Stack is Empty");
return 0;
}
else
{
int data;
data  = stack[top -1];
return data;
}
}

public void size() 
{
  //return (top);
  System.out.println("Size: "+top);
}

public void  isEmpty() 
{
  if(top<=0)
  System.out.println("Stack is Empty");
  
  else
  System.out.println("Stack is NOT Empty");
  
}

}
===========================================================

Java 8 Notes Pics