Monday, February 17, 2014

C: Create Binary Search Tree from a Array

Hi, Today we will create a Binary Search Tree, which is nothing but a Sorted Binary Tree. The methods which we have used here are:

  1. Create Tree to iterate the Tree to the length of the array, with a reference to the array and to the root of the Tree
  2. Create a Method to create a node and on the basis of the value at the index in the array and condition to put the node in the Tree should satisfy 
  3. Print theTree
#include <stdio.h>
#include <stdlib.h>

struct BST
{
    struct BST* left;
    int info;
    struct BST* right;
};

void createTree(struct BST*, int*, int);
void makeNode(struct BST*, int*);
void printTree(struct BST*);

int main()
{
    int arr[100] = {6,5,3,7,9,4,1,10};
    struct BST* root = (struct BST*) malloc(sizeof(struct BST));
    root->info = arr[0];
    root->left = root->right = NULL;
    
    createTree(root, arr, 7);
    printTree(root);
    return 0;
}

void createTree(struct BST* root, int* arr_ref, int arr_len)
{
    struct BST* temproot = root;
    
    while(arr_len > 0)
    {
        arr_ref++;
        makeNode(temproot, arr_ref);
        arr_len--;
    }
}

void makeNode(struct BST* root, int* arr_ref)
{
    struct BST* temp = root;
    
        if(temp->info > *arr_ref)
        {
            if(temp->left == NULL)
            {
                struct BST* newnode = (struct BST*) malloc(sizeof(struct BST));
                newnode->info = *arr_ref;
                newnode->left = newnode->right = NULL;
                temp->left = newnode;
            }
            else
                makeNode(temp->left, arr_ref);
        }
        else if(temp->info <= *arr_ref)
        {
            if(temp->right == NULL)
            {
                struct BST* newnode = (struct BST*) malloc(sizeof(struct BST));
                newnode->info = *arr_ref;
                newnode->left = newnode->right = NULL;
                temp->right = newnode;
            }
            else
                makeNode(temp->right, arr_ref);
        }
}

void printTree(struct BST* root)
{
    struct BST* temp = root;
    
    if(temp == NULL)
        return;
    else
    {
        printTree(temp->left);
        printf(" %d ",temp->info);
        printTree(temp->right);
    }
}

Monday, January 6, 2014

Given a Binary Tree, replace the data of each node by the sum of data of all its descendent nodes.(Leaf nodes will have 0)



The Solution for this problem is simple and recursive one. In this problem you have to traverse to the last parent node which has at least 1 child.

So the conditions possible are:

  1. Parent has only one leaf  node a) only Left Leaf Node or b) only Right Leaf Node
  2. Parent has both children, Left and Right Leaf Nodes.
Now the only thing left is to traverse recursively to the last parent and check wether it has one child or 2 children and store the value of its child or sum of children into its info and if the children do not have any further children then set their value to 0.


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

struct BST
{
    struct BST* left;
    int info;
    struct BST* right;
};

struct BST* newNode(int);
void printTree(struct BST*);
void replaceParentWithSumOfChildren(struct BST*);

Wednesday, December 18, 2013

Adding 3 Linked Lists


Last Time we discussed the basics of Linked List Today we will try adding more more than 2 Linked Lists. Although in the program you will see that I have manually created Lists and then adding them in a very constrained manner which can be overcome by introducing dynamic looping as per the program requirements.


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

struct Node
{
    int info;
    struct Node* next;
};

int lenghtOfLinkedList(struct Node*); // This method is count the number of elements in a list and on the basis of max count neutralize all other to bring them to the format of largest one.
void createLinkedListA(struct Node*); // create a sample linked list
void createLinkedListB(struct Node*); // create a sample linked list
void createLinkedListC(struct Node*); // create a sample linked list

Now we have linked lists
A = head->6->7->8->9
B = head->1->2->3
c = head->4->5

void neutralizeLinkedList(struct Node*, int, int); 


Now after neutralizing we have linked lists
A = head->6->7->8->9
B = head->0->1->2->3
C = head->0->0->4->5

Tuesday, December 10, 2013

Back To Primitive Code: Creating a Linked List


The below code explains procedure to how to create a Singly-Linked-List and Print Data in that List.

#include <stdio.h>

/*This part of the code is to create a structure in which we create the simplest entity in the List which is a Node
 <Code below is definition of single Node>
The Node consists of the internal Data which can be anything from primitive data to an array to another structure.
*/
struct Node
{
    int info;
    struct Node *next;
};

/*Now to create a linked list what we require is a HEAD to mark the beginning of the Linked-List followed by a TEMP to track the current node being processed, followed by a Temp Definition of NEWNODE which will be created at run-time*/

int main()
{
    int i = 0;
    struct Node *head = (struct Node*)malloc(sizeof(struct Node));
    struct Node *temp = NULL;  
    struct Node *newnode = NULL;  

Saturday, November 2, 2013

Most Optimized Code for Sorting


Hi,

All of you must have come across statements and questions online or technical exams or placement exams asking for the MOST OPTIMIZED Code for sorting. Below I have shared a code to do that.
The Code is easy to understand and implement.

#include <stdio.h>
#include <math.h>

void createArray(int *array_players, int nlenght);
void createTournament(int *arry_players, int nlenght);
int findNextMax(int arry_players[], int nlenght);


int main()
{
    int i, arry_players[100], nlenght, max_value, nextMax_value, sorted_array[100];

    printf("Enter Number of Elements in Array ");
    scanf("%d",&nlenght);

    createArray(arry_players, nlenght);

Saturday, July 27, 2013

Project Euler Solved using C# : 10001st prime


By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?

what you have to do is traverse and check if the number is prime. The most important concept to remember is
  • 2 is the only EVEN prime number
  • 3 onwards all prime numbers are ODD
  • a number is considered to be prime if it is Divisible by any number Less Than or Equal To SQUAREROOT of (Suspected Number).
Answer to the Problem is : 104743

 
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace ProjectEuler  
 {  
   class PrimeAtPlace  
   {  
   

Project Euler Solved using C# : Sum square difference


The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Remember Sigma(n) = (n*(n+1))/2
and Sigma(n^2) = (n*(n+1)*(2n+1))/6 

Equating them w.r.t to given problem = Sigma(n)^2 - Sigma(n^2) is

(n * ((n * n) - 1) * ((3 * n) + 2)) / 12

Answer to the Problem is :  25164150
 using System;  
 namespace ProjectEuler  
 {  
   class SquareAndSum  
   {