shahidnitt
BAN USERAssume val1 is less than val2.
Node * FindCommonAncestor(Node * root, int val1, int val2)
{
Node *ancestor;
if(root == NULL || root->data == val1 || root->data == val2)
return NULL;
if(root->data > val1 && root->data < val2)
return root;
else if(root->data < val1)
{
ancestor = FindCommonAncestor(root->right, val1, val2);
}
else if(root->data > val2)
{
ancestor = FindCommonAncestor(root->left, val1, val2);
}
if(ancestor == NULL)
return root;
else
return ancestor;
}
Node * ConvertBSTintoDoublyLinkList(Node *root)
{
if(root == NULL)
return NULL;
Node *leftSubTree = ConvertBSTintoDoublyLinkList(root->left);
Node *rightSubTree = ConvertBSTintoDoublyLinkList(root->right);
while((leftSubTree != NULL) && (leftSubTree->right != NULL))
leftSubTree=leftSubTree->right;
while((rightSubTree != NULL) && (rightSubTree->left != NULL))
rightSubTree = rightSubTree->left;
root->left= leftSubTree;
root->right = rightSubTree;
if(leftSubTree != NULL)
leftSubTree->right = root;
if(rightSubTree != NULL)
rightSubTree->left = root;
return root;
}
double powerofN(double num, unsigned int power)
{
double reminder=1;
if( power < 1)
return 1;
while(power > 1)
{
if(power%2 ==0 )
{
num = num * num;
power = power/2;
}
else
{
reminder = reminder * num;
num = num * num;
power = (power - 1)/2;
}
}
num = num * reminder;
// printf("The value of num is %f and result is %f\n", num, result);
return num;
}
A sorted link list contains a node as a structure of int and char *, As we read the next word, we will traverse the link list if we got the exact word then it increment the int of the node otherwise we will create a node in which we will allocate a memory of word length and add it at a proper place.
Link List : Best case : 1. Wrost case : n.
BST : Best case 1, Worst case n, Average Case : logn.
A sorted link list contains a node as a structure of int and char *, As we read the next word, we will traverse the link list if we got the exact word then it increment the int of the node otherwise we will create a node in which we will allocate a memory of word length and add it at a proper place.
Link List : Best case : 1. Wrost case : n.
BST : Best case 1, Worst case n, Average Case : logn.
int htoi(char *str)
- shahidnitt September 06, 2010{
char baseCaps = 'A';
char baseSmall = 'a';
int result = 0;
int len = strlen(str);
for(int i=0;i<len;i++)
{
if( (str[i] >= '0') && (str[i] <= '9'))
{
result += ((int)str[i] - 47) * pow(16, len -i -1);
}
else if(str[i] >= 'A' && str[i] <= 'F')
{
result += ((int)str[i] - (int)baseCaps + 10)* pow(16, len - i -1);
}
else if(str[i] >= 'a' && str[i] <= 'f')
{
result += ((int)str[i] - (int)baseSmall + 10)* pow(16, len - i -1);
}
else
{
printf("Wrong Inputs \n");
return -1;
}
}
return result;
}