Thursday, June 27, 2013

Height of Binary Tree using recursion


Question : Write a program that finds the height of a binary tree .

Solution :

//program that prints the height of a binary tree , here used recursion method

#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    int data ;
    struct node *left;
    struct node *right;
} node ;


node *insert(int d)
{
    node *tmp = (node *)malloc(sizeof(node));
    tmp->data = d ;
    tmp->left = NULL ;
    tmp->right = NULL ;
    return tmp ;
}

int height(node *root)
{
    if(root == NULL)
        return 0;
    else
    {
        int lHeight = height(root->left);
        int rHeight = height(root->right);

        if(lHeight > rHeight)
            return (lHeight + 1);
        else
            return (rHeight + 1);
    }

}

int main()
{
    node *btree = insert(10);
    btree->left = insert(5);
    btree->right = insert(7);
    btree->left->left = insert(2);
    btree->left->right = insert(8);

    printf("\nHeight Of Binary Tree : %d", height(btree));
}



Tuesday, June 11, 2013

Sorting - Merge Sort tutorial

In previous tutorial , we have learned how to sort an array using Bubble Sort Algorithm   .

In this tutorial , we are going to learn how to make a sorted array using two unsorted arrays , using Merge Sort .

For this purpose , first we need to convert the two unsorted arrays to sorted arrays . For that you can use any Sorting Algorithm like Bubble Sort .

Next step is implemented using C as follows

#include <stdio.h>

int main()
{
    int a1[]  = {2,9,11,13,57};
    int a2[]  = {1,3,17,25,90};
    int i = 0, j = 0, k = 0 , t ;
    int a3[10];

    while(1)
    {

        if(a1[i] < a2[j])
        {
            a3[k++] = a1[i++];
            printf("\na3[%d] = %d",k-1,a3[k-1]);
        }

        else if(a2[j] < a1[i])
        {
            a3[k++] = a2[j++];
            printf("\na3[%d] = %d",k-1,a3[k-1]);
        }

        if(i == 5 || j == 5)
            break ;
    }

    for(;i<5;)
    {
        a3[k++] = a1[i++];
        printf("\na3[%d] = %d",k-1,a3[k-1]);
    }
    for(;j<5;)
     {
         a3[k++] = a2[j++] ;
        printf("\na3[%d] = %d",k-1,a3[k-1]);
     }
}

Thursday, June 6, 2013

Sorting - Bubble Sort Tutorial

There are two types of Sorting - Internal and External Sorting.
Internal Sorting refers to sorting in which all the data that is to be sorted should be in memory at a time. External memory refers to sorting in which some data is in the memory and some data should be in auxillary memory like floppy , harddisk ,etc .

One of the Internal Sorting Algorithms is Bubble Sort . 

pseudo code for Bubble sort :

 For I = 0 to N - 2
       For J = 0 to N - 2
         If (A(J) > A(J + 1)
           Temp = A(J)
           A(J) = A(J + 1)
           A(J + 1) = Temp
         End-If
       End-For
     End-For


here is the implementation of Bubble Sort in C .
  

#include <stdio.h>

int main()
{
    int arr[] = {8,7,6,5,4,3,2,1};
    int i,j,t,k;
    
    for (i = 0; i < 7; i++)
    {
        printf("\n Cycle : %d",i);
        for (j = 0; j < 7; j++)
        {
            if (arr[j] > arr[j+1])
            {
                t = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = t ;
                printf("\narr[%d] = %d  , arr[%d] = %d",j,arr[j],j+1,arr[j+1]);
            }
        }
        printf("\n");
        for (k = 0 ; k < 8; k++)
            printf("arr[%d] = %d\t ",k,arr[k]);
        printf("\n");
       
    }
}
 
complexity :

Worst Case : O(n2)
Average Case : O(n2)
Best Case : O(n2)


Tips :

1) For applying bubble sort on n elements , the number of iteration required is (n-1)
2) Each iteration separates out a biggest element from given elements and put it in the end .

Wednesday, June 5, 2013

How to remove Delta search from different browsers

HOW TO REMOVE DELTA SEARCH?

 

When trying to remove Delta Search virus from the system, you should firstly eliminate it from Add/Remove Programs list. For that, click the Start button and then select Settings -> Control Panel -> Add/Remove Programs. Additionally, remove Delta Search from your browsers:


Internet Explorer:
Open IE -> Tools -> Internet Option -> General tab.Enter Google or other address to make it the default start page.


Mozilla Firefox:
Open Mozilla Firefox, go to Tools -> Options -> General -> Startup. Now select 'Show a blank page' when Firefox Starts or set a certain website, like Google or similar..

 Google Chrome:
Open Google Chrome, click on wench icon, go to settings and choose 'Manage search engines'. Change search engine to google or other and delete Delta Search from the list. Then Go to section “On start” and make sure you get blank page while creating new tab.
The most important thing: if you didin't installed it by yourself, you should run a full system scan with updated SpyHunter.

Saturday, June 1, 2013

Core Animation 2 - Using UIBezierPath and CAKeyFrameAnimation

In the first post of this series , we have studied how to bring about a simple animation by just changing the frame of UIView .

In this tutorial , we will use UIBezierPath and CAKeyFrameAnimation .
With UIBezierPath , we can make a path  which the view should follow along with its animation .
Apple provides a set of function on UIBezierPath for creating lines , arcs , curve, quadratic curve , etc .
Here we are going to use simple Linear motion of a ball using linear bezier path.

First we create an object of Bezier path and then we set the points of the path . And then we set path of CAKeyFrameAnimation as the path object created. fillMode  property determines the final state of the view on which animation is done .  Set the fillMode property to be  kCAFillModeForwards which means that the view would remain in its final state as it is when animation is completed . We set the duration of the animation as 6s . Also , set the removeOnCompletion property as NO .


Here is the code .



-(void)showAnimation:(id)sender
{
    animatingView.frame = CGRectMake(0, 0, 50, 50);
    UIBezierPath *trackPath = [UIBezierPath bezierPath];
    [trackPath moveToPoint:CGPointMake(0, 0)];
    [trackPath addLineToPoint:CGPointMake(320, 100)];
    [trackPath addLineToPoint:CGPointMake(0, 200)];
    [trackPath addLineToPoint:CGPointMake(320, 300)];
    [trackPath addLineToPoint:CGPointMake(0, 400)];
    
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = trackPath.CGPath ;
    animation.fillMode = kCAFillModeForwards ;
    animation.duration = 6.0f;
    animation.removedOnCompletion = NO ;
    [animatingView.layer addAnimation:animation forKey:@"Animation3"];
}

This is how our output looks