Sunday 28 July 2013

Binary Tree representation using Array:

a Binary Tree T can be represented by Array in 2 different way;
  1. Sequential representation (via singular linear array)
  2. Linked representation


Sequential representation:
if T is a complete Binary tree, then its-
  • root R will be stored in array element T[0]
  • if K number of node occupies, will be stored in array element T[K]
  • left child will be stored in array element T[ 2*K ]
  • right child will be stored in array element T [ 2*K+1 ]
** if T[ 1 ] = NULL, then it is an empty Tree.


diagrammatic explanation:










Linked representation:
if K is the correspond  location pointer :
  • need 3 parallel array INFO, LEFT and RIGHT
  • data at node will be stored in array element INFO[K]
  • location of left child of node N will be stored in array element LEFT[ K ]
  • location of right child of node N will be stored in array element RIGHT [ K ]
  • root R of T will be stored in ROOT
diagrammatic explanation:





various standard ways of Binary Tree Traversal:

3 standard ways are out there for traversing a Binary Tree. these ways or ( you may call it Algorithms) are given billow:
  • PreOrder
  • InOrder
  • PostOrder

working process: if the tree is T with root R:

PreOrder:
  • process the root R
  • traverse the Left SubTree of R in PreOrder
  • traverse the Right SubTree of R in PreOrder
InOrder:
  •  traverse the Left SubTree of R in InOrder
  •  process the root R
  •  traverse the Right SubTree of R in InOrder
PostOrder:
  • traverse the Left SubTree of R in PostOrder
  • traverse the Right SubTree of R in PostOrder
  • process the root R

example:
lets consider an  mathematical equation 
       [ a + ( b - c ) ] * [ ( d - e ) / ( f + g -h ) ]
now the orders will be like these:
PreOrder   :        * + a - b c / - d e - + f g h 
InOrder     :        a + b - c * d - e / f  + g - h
PostOrder  :        a b c - + d e - f g + h - / * 



Saturday 27 July 2013

difference between Full Binary Tree and Complete Binary Tree

topics
Full Binary Tree
Complete Binary Tree
1.node assemble
it's a regular binary tree with no odd number of leaves.
Every node or level of a tree is completely filled exception can happen only in the last level.

 2.last node
Last and each node must have either 0 (zero) or 2 (two) children.
Children on the last level node must start from the left most position with no spot left unoccupied in between any tow form the left counting.

3.application
 n/a
 Some specific data structure type (i.e. Heaps) need to be in Complete Binary Tree shape, when they need not to be a Full Binary Tree.

4.formula   
If one can find any of

  •  Number of Total Nodes or
  •  Number of Laves or
  •  Number of Internal Nodes
can find the other 2 at instant.

 In complete binary tree there is no relating connection among these 3 attributes.
5. leaf level
Not necessarily have to be in the same depth.
Tree must have the entire leaves node in the exact same depth.

6. diagram
     
   




what is Binary Search Tree (BST) in Data Structure?

binary search tree (BST):
a node-based data structure, which at most can have two child nodes counting from the left child. also known as Ordered or Sorted Binary Search Tree.

properties:
  • each child must be either a leaf node or the root of another binary search sub tree.
  • left sub-tree can contain only nodes, whom are less than the parent node.
  • right sub-tree can contain only nodes, whom are greater than the parent node
  • non of the nodes can be alike.
diagram:

specialties:
  • basis for many of highly efficient sorting and searching algorithms
  • can be used to construct abstract data structures including sets, multi-sets, associative arrays etc.



Thursday 25 July 2013

UVA sloved: problem number: 10055 - Hashmat the Brave Warrior

question:
Problem A
Hashmat the brave warrior
Input: standard input

Output: standard output

Hashmat is a brave warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before fighting he just calculates one thing, the difference between his soldier number and the opponent's soldier number. From this difference he decides whether to fight or not. Hashmat's soldier number is never greater than his opponent.

Input
The input contains two integer numbers in every line. These two numbers in each line denotes the number of soldiers in Hashmat's army and his opponent's army or vice versa. The input numbers are not greater than 2^32. Input is terminated by End of File.

Output
 For each line of input, print the difference of number of soldiers between Hashmat's army and his opponent's army. Each output should be in seperate line.

Sample Input:
10 12
10 14
100 200

Sample Output:
2
4
100
   ____________________________________________________________________


answer:
#include<cstdio>
using namespace std;

int main()

{
long long h,a;
while(scanf("%lld%lld",&h,&a)==2)
{
long long data=h-a;
if(data<0)
data=data*-1;
printf("%lld\n",data);
}
return 0;

}


**use the code only for practice purpose. don't ruin your programming career copy-pasting this.

Tuesday 9 July 2013

source code: QuickSort using C++ [ simple, with little details and user friendly likewise ever ]

#include<iostream>
using namespace std;


class quicksort

{
public:
        int element_count, elements[100];

        void sorted_array();

        void sorting(int [], int, int);
        void partitioning(int [], int, int, int&);
        void display();
};


void quicksort :: sorted_array()

{
    cout <<"how many elements gonna be in the list?\nans: ";
    cin>> element_count;
    cout <<endl <<endl;

    cout <<"now insert elements 1 by 1 n do hit ENTER or SPACE afterwards every entry:\n=>";

    for(int h=0; h<element_count; h++)
        {
            cin>> elements[h];
        }
}

void quicksort :: sorting(int ary[], int lower_bound, int upper_bound)

{
    int x; //will be used as pointer

    if(lower_bound >= upper_bound)

        return;
 
    display();
 
    partitioning(ary, lower_bound, upper_bound, x);
    sorting(ary, lower_bound, x-1);        // right to left  check
    sorting(ary, x+1, upper_bound);        // left to right check
}

void quicksort :: partitioning(int x[], int lower_bound, int upper_bound, int &pj)

{
    int a= x[lower_bound], down= lower_bound, temp;

    upper_bound= upper_bound;


    while(down < upper_bound)

    {
        while(x[down] <= a)            // left to right check
            down++;
     
        while(x[upper_bound] > a)    // right to left  check
            upper_bound--;
     
        if(down < upper_bound)
        {
            temp= x[down];
            x[down]= x[upper_bound];
            x[upper_bound]= temp;
        }
    }


    x[lower_bound]= x[upper_bound];

    x[upper_bound]= a;
    pj= upper_bound;
}

void quicksort :: display()

{
    for(int h= 0; h<element_count; h++)
    {
        cout <<elements[h] <<" ";
    }
    cout<<endl;
}


int main()

{
    quicksort qs_obj;
    qs_obj.sorted_array();

    cout <<"\n\nnow showing each sorting in step by step process:\n";

    qs_obj.sorting(qs_obj.elements, 0, qs_obj.element_count-1);
 
    qs_obj.display();
}

Friday 5 July 2013

how to access Google USA [ some might call it Original Google Head Office's server ] from any other country?

this question has been bugging me for a long time, i think i should share the best way to fix it with everyone.

for this you can change 'preference' form your Google account or can do many of hubble bubble stupid 'setting' stuffs.

OR

you can just use www.google.com/ncr to get access the USA Google server network (or you may call it the main Google server).

thank you :)




Tuesday 2 July 2013

source code: class room Number Analysis [ easy and simple version and user friendly likewise always]

#include<iostream>
using namespace std;

char rn[4];

void room_number()
{
cout <<"tell me your room number: ";
for(int x= 0; x<4; x++)
{
cin>> rn[x];
}
cout <<"got it (Y)\n\n";

if(rn[1] == '0')
{
cout <<"your room is in campus number: " <<rn[0];
cout <<"\nroom number: " <<rn[3] <<"\nat floor: " <<rn[2] <<endl;
}

else if(rn[0] == '0')
{
cout <<"your room is in campus number: " <<rn[1];
cout <<"\nroom number: " <<rn[3] <<"\nat floor: " <<rn[2] <<endl;
}

else
cout <<"aint working";
}

int main()
{
room_number();
cout <<"\n\n\nthank you :)\n\n\n";
}

Monday 1 July 2013

source code: Stack using C++ [user friendly]

#include <iostream>
using namespace std;

int top=-1;
int stack[100];


void stklist()
{
    cout <<"\nstack you just made is:\n";
cout <<"| ___ |\n";

    for(int i=top;i>=0;i--)
       cout <<"|_ " <<stack[i] <<" _|\n";

cout <<"| ___ |\n";
}


void check(int length)
{
if(top+1 ==length)
    {
       cout <<"stack OVERFLOW WARING!!\n";
       return;
    }
else if (top==0)
{
cout <<"stack is emply :/\n";
}
}


void push()
{
bool proceed= true;
int data, length, p;

cout <<"\n\nwhat would be the lenth?\nans: ";
cin>> length;

check(length);

while(proceed)
{
cout <<"wat u wanna put into the stack?\nans: ";
cin>> data;

stack[++top]=data;

cout <<"any more?\n press 1 for yes n 0 for no:";
cin>> p;

try

{
if(p>1 || p<0)
throw 1;
else{
proceed=p;
}
}

catch(...)
{
cout <<"como man, it says 1 or 0, dont be a douch!\n";
cout <<"do it straight now, got any more?\n press '1' for YES or '0' for NO:";
cin>> p;
}
stklist();
}
}


int pop()
{
    int temp;
   
if(top==-1)
    {
       cout <<"stack is in underflow state. ";
       return(-1);
    }
   
temp= stack[top];
    top--;
    return temp;
}


int main()
{  
int k;
char ch;
while(1)
{
cout <<"1: push\n2: pop\n3: exit.\n";
cout <<"enter ur choice: "; cin >> ch;

switch(ch)
{
case '1':
{
push();
break;
}

case '2':
{
cout << "data poped: " << pop();
stklist();
break;
}

case '3':
cout <<"\n\n\n\n\nthank you\n\n\n";
return 3;
}

cout <<"\n\nrepeating\n";
} ;

}