Saturday, 6 December 2014


Binary Tree:
A binary tree is a tree in which each
node can have maximum two children. Thus each  node can have no child, one child or two children.  The pointers help us to identify whether it is a left child or a right child.

1.     The root of the tree has level 0

2.     The level of any other node in the tree is one more  than the level of its parent.



Full Binary Tree
How many nodes?
Level0 : 1 node ( height 1)
Level 1: 2 nodes ( height 2)
Level 3 : 4 nodes (height 3)
Level 3: 8 nodes (height 4)

Total number of nodes
n = 2^h – 1 ( maximum)
h = log ( n+1)



Implementation
•A binary tree has a natural implementation in linked storage. A tree is referenced with a pointer to its root . Recursive definition of a binary tree .A binary tree is either Empty, or A node (called root) together with two binary trees (called left subtree and the right subtree of the root)

Each node of a binary tree has both left and right  subtrees which can be reached with pointers:

struct tree_node{
int data;
struct tree_node *left_child;
struct tree_node *right_child;
};

Traversal of Binary Trees
·      Inorder
·      PreOrder
·      PostOrder


·      With inorder traversal the order is left-child, root node, right-child
·      With preorder traversal the order is root node, left child , right child.
·      With postorder traversal the order is left child, right child, root node.


InOrder:
void inorder(struct tree_node *p)
{
if (p !=NULL)
{
inorder(p->left_child);
printf(“%d\n”, p->data);
inorder(p->right_child);
}
}

PreOrder:
void preOrder(struct tree_node *p)
{
if (p !=NULL)
{
printf(“%d\n”, p->data);
preOrder(p->left_child);
preOrder(p->right_child);
}
}

PostOrder:

void postOrder(struct tree_node *p)
{
if (p !=NULL)
{
postOrder(p->left_child);
postOrder(p->right_child);
printf(“%d\n”, p->data);

}
}



Finding Maximum value in a given tree p :
It’s a recursion function. Idea is to find the maximum value between left sub tree, right sub tree and root.

Maximum Between 2 number :

Public int maximum(int a, int b)
{
            return (a>b?a:b) ;
}


int findMax (struct tree_node *p)
{
int node_data, leftmax, rightmax, max;
max = -1
//assume all values in the tree are positive integers
If (p != NULL)
{
 node_data = p -> data;
leftmax = findMax(p -> left_child);
rightmax = findMax(p->right_child);
//find the largest of the tree values.
max =  maximum((node_data,leftmax),rightmax)
}
return max;
}


Finding sum of values of all the nodes of a tree:

To find the sum, add to the value of the current node, the sum of values of all nodes of left subtree and the sum of  values of all nodes in right subtree.

Int sumOfAllNode(struct *p)
{
int sum =0;
            if(p!=null)
{
sum = sum + p->data + sumOfAllNode(p->right) +
sumOfAllNode(p->left);
}
return sum;
}

Depth Of a Node :
The depth of a node is the number of edges from the root node to node.
A root node will have a depth of 0.
Heigh Of a node :
The height of a node is the logest number of edges from the node to a leaf.
A leaf node will have a height of 0.
Description:  tree, with height and depth of each node

Height of a Binary Tree:
public int heightOfBinaryTree(Node node) {
          if (node == null) {
               return 0;
          } else {
               return 1 + Math.max(heightOfBinaryTree(node.left),
                          heightOfBinaryTree(node.right));
          }
     }

Depth Of a Node: (level of a node)
public int depthOfNode(Node node, int data,int depth) {
          if (node == null) {
               return 0;
          }
          else if(node.data == data)
               return depth;
          else {
               return Math.max(DepthOfNode(node.left, depth + 1),
                          depthOfNode(node.right, depth + 1));
          }
     }

Complete Binary Tree:
It a special kind of binary tree.

Application of trees:
·      Class hierarchy in Java
·      File system
·      Storing hierarchies in organizations

Thursday, 13 November 2014

Data Structure Problem From Cormen


How many people should be invited to a party in order to make it likely that there are two people with the same birthday? 

Ans :
 Probability reaches 100% when the number of people reaches 367 (since there are 366 possible birthdays, including Leap year). However, 99.9% probability is reached with just 70 people, and 50% probability with 23 people.

Explanation :
Idea is 1st calculate the probability of 2 person who don't share same birthday .

Find number of pair form 23 people = 
\displaystyle{\frac{23 \cdot 22}{2} = 253}
 
1st person birthday will be 1/365
The chance of 2 people having different birthdays is: 364/365
The chance of 3 people having different birthdays is: 363/365
Change of 23 people having different birthday will be 365/365 × 364/365 × 363/365 × 362/365 × ... × 343/365 =
(1/365)23 × (365 × 364 × 363 × ... × 343)
≈ 0.492703

The chance that we have a match is: 1 – 49.95% = 50.05%, or just over half! If you want to find the probability of a match for any number of people n the formula is: \displaystyle{p(n) = 1 - \left(\frac{364}{365}\right)^{C(n,2)} = 1 - \left(\frac{364}{365}\right)^{n(n-1)/2} }
Why Automation testing?
1) You have some new releases and bug fixes in working module. So how will you ensure that the new bug fixes have not introduced any new bug in previous working functionality? You need to test the previous functionality also. So will you test manually all the module functionality every time you have some bug fixes or new functionality addition? Well you might do it manually but then you are not doing testing effectively. Effective in terms of company cost, resources, Time etc. Here comes need of Automation.
- So automate your testing procedure when you have lot of regression work.
2) You are testing a web application where there might be thousands of users interacting with your application simultaneously. How will you test such a web application? How will you create those many users manually and simultaneously? Well very difficult task if done manually.
- Automate your load testing work for creating virtual users to check load capacity of your application.
3) You are testing application where code is changing frequently. You have almost same GUI but functional changes are more so testing rework is more.
- Automate your testing work when your GUI is almost frozen but you have lot of frequently functional changes.
What are the Risks associated in Automation Testing?
There are some distinct situations where you can think of automating your testing work. I have covered some risks of automation testing here. If you have taken decision of automation or are going to take sooner then think of following scenarios first.
1) Do you have skilled resources? 
For automation you need to have persons having some programming knowledge. Think of your resources. Do they have sufficient programming knowledge for automation testing? If not do they have technical capabilities or programming background that they can easily adapt to the new technologies? Are you going to invest money to build a good automation team? If your answer is yes then only think to automate your work.
2) Initial cost for Automation is very high:
I agree that manual testing has too much cost associated to hire skilled manual testers. And if you are thinking automation will be the solution for you, Think twice. Automation cost is too high for initial setup i.e. cost associated to automation tool purchase, training and maintenance of test scripts is very high.
There are many unsatisfied customers regretting on their decision to automate their work. If you are spending too much and getting merely some good looking testing tools and some basic automation scripts then what is the use of automation?
3) Do not think to automate your UI if it is not fixed:
Beware before automating user interface. If user interface is changing extensively, cost associated with script maintenance will be very high. Basic UI automation is sufficient in such cases.
4) Is your application is stable enough to automate further testing work?
It would be bad idea to automate testing work in early development cycle (Unless it is agile environment). Script maintenance cost will be very high in such cases.
5) Are you thinking of 100% automation?
Please stop dreaming. You cannot 100% automate your testing work. Certainly you have areas like performance testing, regression testing, load/stress testing where you can have chance of reaching near to 100% automation. Areas like User interface, documentation, installation, compatibility and recovery where testing must be done manually.