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 








              

Friday, May 31, 2013

Core Animation 1 - Simple Animation by just changing frame size


In this series of tutorials , we are going to study how can achieve various kinds of animations of UIView . Animations play a very important role in better looking applications and increases its quality . They make the apps look ... WOW !!!Also , animations can help make us various kinds of games .


In this tutorial , we are going to make a very simple animation of UIView . In the beginning there are no animating views . Then we click on the    "Show" button , then four views originate from four corners of the iPhone screen and accumulate in the center of the screen and join together .

We have created four animating views as follows . To make views appears as if they are invisible before clicking the "show" button , the height and width of views are 0, 0.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        animatingView1 = [[UIView alloc] init];
        [animatingView1 setBackgroundColor:[UIColor redColor]];
        animatingView1.frame = CGRectMake(0, 0, 50, 50);
        
        
        animatingView2 = [[UIView alloc] init];
        [animatingView2 setBackgroundColor:[UIColor yellowColor]];
        animatingView2.frame = CGRectMake(320, 0, 0, 0);
        
        animatingView3 = [[UIView alloc] init];
        [animatingView3 setBackgroundColor:[UIColor greenColor]];
        animatingView3.frame = CGRectMake(0, 480, 0, 0);
        
        animatingView4 = [[UIView alloc] init];
        [animatingView4 setBackgroundColor:[UIColor blueColor]];
        animatingView4.frame = CGRectMake(320, 480, 0, 0);
        
        [self.view setBackgroundColor:[UIColor lightGrayColor]];
        
    }
    return self;
}
 
In the following function we add the animating views to our viewcontroller's view and also create two button "show" and "hide" as follows . 

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view addSubview:animatingView1];
    [self.view addSubview:animatingView2];
    [self.view addSubview:animatingView3];
    [self.view addSubview:animatingView4];
 
    UIButton *nShowBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nShowBtn setFrame:CGRectMake(20 , 50 , 100 ,  50)];
    [nShowBtn setTitle:@"show" forState:UIControlStateNormal];
    [nShowBtn addTarget:self action:@selector(normalShowAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nShowBtn];
    
    UIButton *nHideBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [nHideBtn setFrame:CGRectMake(200 , 50 , 100 ,  50)];
    [nHideBtn setTitle:@"hide" forState:UIControlStateNormal];
    [nHideBtn addTarget:self action:@selector(normalHideAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:nHideBtn];

} 



Following are the selector function for touchUpInside event of two buttons. We used here a class method of UIView  which take the duration of animation and a block where we can put the final state of our animation . In our animation , we are putting the final frame of our views with height and width of 50,50 and location at the centre of the screen . This makes views appear as if we they are originating from the four corners of the screen .


-(void)normalShowAnimation:(id)sender
{
    [UIView animateWithDuration:0.6 animations:^(void){
        animatingView1.frame = CGRectMake(110, 180, 50, 50);
        animatingView2.frame = CGRectMake(160, 180, 50, 50);
}

-(void)normalHideAnimation:(id)sender
{
    [UIView animateWithDuration:0.6 animations:^(void){
        animatingView1.frame = CGRectMake(0, 0, 0, 0);
        animatingView2.frame = CGRectMake(320, 0, 0, 0);
}

Here are the final output of our animation .
                                 

Thursday, May 30, 2013

Iron Man 3 : TV spots


Iron Man 3 - TV Spot 1



Iron Man 3 - TV Spot 2



Iron Man 3 - TV Spot 3



Iron Man 3 - TV Spot 4



Iron Man 3 - TV Spot 5



Iron Man 3 - TV Spot 6



Iron Man 3 - TV Spot  7