Thursday, March 5, 2015

Trees

Insert into complete binary tree :

public void insertintobinarytree(int dt)
    {
        Queue<Node> queue = new Queue<Node>();
        Node temp;
        Node newnode = new Node();
        newnode.data = dt;
        newnode.left = null;
        newnode.right = null;
        if (root == null)
            root = newnode;
        else
        {
            queue.Enqueue(root);
            while (!(queue.Count == 0))
            {
                temp = queue.Dequeue();
                if (temp.left != null)
                    queue.Enqueue(temp.left);
                else
                {
                    temp.left = newnode;
                    return;
                }
                if (temp.right != null)
                    queue.Enqueue(temp.right);
                else
                {
                    temp.right = newnode;
                    return;
                }
            }
        }
    }


Insert into Binary Search Tree :

public void insert(int n)
{
Queue<int> queue= new Queue<int>();
node temp= new node();
temp.left=null;
temp.right=null;
temp.item=n;
if(root==null)
{
root= temp;
}
else
{
queue.Enqueue(root);
while(queue.Count!=0)
{
node t = queue.DeQueue();
if(t.data > n)
{
if(t.left==null)
{
t.left=temp;
return;
}
else
queue.Enqueue(t.left);
}
else
{
if(t.right==null)
{
t.right=temp;
return;
}
else
queue.Enqueue(t.right);
}
}
}

Friday, February 27, 2015

Stacks And Queues

Getmin() operation of stack in O(1) complexity.

 public class Stacks :stack
    {
       stack minstack, normalstack;

        public Stacks(int size)
        {
            minstack = new stack(1);
            normalstack = new stack(size);
        }
        public void Push(int item)
        {
            if (normalstack.isEmpty())
            {
                normalstack.push(item);
                minstack.push(item);
            }
            else
            {
                normalstack.push(item);
                int y = minstack.pop();
                if (item < y)
                    minstack.push(item);
                else
                    minstack.push(y);
            }
        }
        public int minvalue()
        {
            return minstack.pop();
        }

    }
  public class stack
    {
        int[] a;
        int top;
        int _size;
        public stack()
        {
        }
        public stack(int size)
        {
            a = new int[size];
            top = -1;
            _size = size;
        }
        public bool isEmpty()
        {
            if (top == -1)
                return true;
            else
                return false;
        }
        public bool isFull()
        {
            if (top == _size - 1)
                return true;
            else
                return false;
        }
        public void push(int x)
        {
            if (isFull())
                Console.WriteLine("stack is full");
            else
            {
                a[++top] = x;
            }
        }
        public int pop()
        {
            int x=Int32.MinValue;
            if (isEmpty())
                Console.WriteLine("stack is empty");
            else
                x = a[top--];
            return x;
        }
    }

Tuesday, February 24, 2015

Linked List

Reverse double linked list :

  public void ReverseLinkedList()
        {
               doubleNode current = head;
            doubleNode temp = head;
            while (current != null)
            {
                temp = current.prev;
                current.prev = current.next;
                current.next = temp;
                current = current.prev;
            }
            if (temp != null)
            {
                head = temp.prev;
            }
        }

Monday, February 16, 2015

Arrays

Common Elements in three sorted Arrays:

using System;

public class Test
{
 public static void Main()
 {
   int[] ar1 = { 1, 5, 10, 20, 40, 80 };
             int[] ar2 = { 6, 7, 20, 80, 100 };
             int[] ar3 = { 3, 4, 15, 20, 30, 70, 80, 120 };
             Arrays.CommonInthreeSortedArrays(ar1, ar2, ar3, ar1.Length, ar2.Length, ar3.Length);
 }
 public static void CommonInthreeSortedArrays(int[] a,int[] b,int[] c,int n1,int n2,int n3)
        {
            IEnumerable<int> temp = a.Intersect(b).Intersect(c);
          foreach (int i in temp)
          {
              Console.Write(i + " ");
          }
 }
}

Find the first repeating element in an array of integers:

 public static int FirstRepeatingElementinArray(int[] ar)
        {
            int min=-1;
            HashSet<int> hash = new HashSet<int>();
            for (int i = 0; i < ar.Length; i++)
            {
                if (hash.Contains(ar[i]))
                    min = i;
                else
                    hash.Add(ar[i]);
            }
            return ar[min];

        }


Power set of a set :

public static void Powerset(int[] ar,int size)
{
int powercount = 1<<size;
for(int counter=0;counter<powercount;counter++)
{
for(int i=0;i<size;i++)
{
if(Convert.ToBoolean((counter &(1<<i))))
Console.Write(ar[i]);
}
Console.WriteLine("\n");
}
}


Print all the subsets sum to zero in a given set of postive and negative numbers.

  public static void SumToZero(int[] arr)
        {
            int[] sum = new int[arr.Length];
            sum[0]=0;
            for (int i = 1; i < arr.Length; i++)
                sum[i] = sum[i - 1] + arr[i];
            for (int j = 0; j  < arr.Length; j++)
            {
                for (int k = j + 1; k < arr.Length; k++)
                {
                    if (sum[j] == sum[k])
                        printzerosums(j, k, arr);   
                }
            }
        }
        public static void printzerosums(int i, int j, int[] arr)
        {
            Console.WriteLine("subset set is ");
            for(int g=i+1;g<=j;g++)
                Console.Write(arr[g]+" ");
            Console.WriteLine("\n");

        }



Count Inversions in an array :

public static int mergesort(int[] a,int[] temp,int left,int right )
{
int inversecount=0;
if(right>left)
{
int mid = left+(right-left)/2;
inversecount=mergesort(a,temp,left,mid);
inversecount+=mergesort(a,temp,mid+1,right);
inversecount+=merge(a,temp,left,mid+1,right);
}
return inversecount;
}
public static int merge(int[] a, int[] temp,int left,int mid,int right)
{
int i=left,j=mid,k=left,inversecount=0;
while((i<=mid-1)&&(j<=right))
{
if(a[i]<=a[j])
temp[k++]=a[i++];
else
{
temp[k++]=a[j++];
inversecount+=(mid-i);
}
}
while(i<=mid-1)
{
temp[k++]=a[i++];
}
while(j<=right)
{
temp[k++]=a[j++];
}
for(int i1=left;i1<=right;i1++)
{
a[i1]=temp[i1];
}
return inversecount;
}
}

Sum of two array numbers to a given target :

 public static void sumtwonumberstoanumber(int[] ar,int target)
        {
            Dictionary<int, int> dic = new Dictionary<int, int>();
            for (int i = 0; i < ar.Length; i++)
            {
                int k = target - ar[i];
                if (dic.ContainsValue(k))
                {
                    Console.WriteLine(dic.FirstOrDefault(x => x.Value == k).Key + "," + k);
                }
                else
                {
                    dic.Add(k, ar[i]);
                }
            }

        }

Find all the pair of triple sum : a[i]+a[j]+a[k]=target

 public void find3numbers(int[] a, int n, int target)
        {
           Mergesort(a);
            int i = 0, k = n - 1, j= (i + k) / 2;
            while (i<k)
            {
                int sum= a[i] + a[j] + a[k];
                if (sum == target)
                {
                    Console.WriteLine(a[i] + "," + a[j] + "," + a[k]);
                    i++; k--;
                }
                else if (sum < target)
                {
                    if (j + 1 != k)
                        j++;
                    else
                        i++;
                }
                else
                {
                    if (j + 1 != k)
                        k--;
                    else
                        j--;
                }
            }
        }