//Tree definition
struct Node{
Node* Left;
Node* Right;
int Val;
};
typedef Node * tree;
/*Helper functions to find minimum and maximum*/
int MaxVal (int iNum1, int iNum2)
{
return iNum1 >= iNum2 ? iNum1 : iNum2;
}
int MinVal (int iNum1, int iNum2)
{
return iNum1 <= iNum2 ? iNum1 : iNum2;
}
/* Depth of trees */
int FindTreeDepth (tree t)
{
if (t == NULL)
return 0;
return 1 + MaxVal(FindTreeDepth(t->Left), FindTreeDepth(t->Right));
}
/* Minimum depth of a leaf */
int FindMinDepthOfLeaf (tree t)
{
if (t == NULL)
return 0;
if (t->Left == NULL)
if (t->Right == NULL)
return 1;
else
return 1 + FindMinDepthOfLeaf(t->Right);
else
if (t->Right == NULL)
return 1 + FindMinDepthOfLeaf(t->Left);
else
return 1 + MinVal(FindMinDepthOfLeaf(t->Left),
FindMinDepthOfLeaf(t->Right));
}
bool IsTreeBalanced (tree t)
{
//For tree to be balanced the depths of two arbitrary leaves must differ
//by at most 1
return FindTreeDepth(t) - FindMinDepthOfLeaf(t) < = 1;
}
Wednesday, December 31, 2008
Monday, December 15, 2008
Insert and Delete operations on Double Linked List
struct Node
{
Node* Prev;
Node* Next;
int data;
};
void InsertNode(Node** ppNode, int num)
{
if(*ppNode == NULL)
{
*ppNode = new Node();
(*ppNode)->data = num;
(*ppNode)->Next = NULL;
(*ppNode)->Prev = NULL;
return;
}
Node * pTemp = *ppNode;
while(pTemp->Next != NULL)
pTemp = pTemp->Next;
pTemp->Next = new Node();
pTemp->Next->data = num;
pTemp->Next->Prev = pTemp;
pTemp->Next->Next = NULL;
return;
}
void Display(Node *pNode)
{
while(pNode)
{
cout<<" "<data;
pNode = pNode->Next;
}
}
bool DeleteNode(Node** ppNode, int num)
{
bool bVal = false;
Node * pTemp = *ppNode;
//If the node is head then the head has to be reset
if((*ppNode)->data == num)
{
pTemp = pTemp->Next;
pTemp->Prev = NULL;
delete(*ppNode);
*ppNode = pTemp;
bVal = true;
}
else
{
while(pTemp)
{
if(pTemp->data == num)
{
pTemp->Prev->Next = pTemp->Next;
pTemp->Next->Prev = pTemp->Prev ;
delete pTemp;
bVal = true;
return bVal;
}
pTemp = pTemp->Next;
}
}
return bVal;
}
void ReverseList(Node** ppNode)
{
if((*ppNode == NULL)||((*ppNode)->Next == NULL))
return;
//Use 2 more pointers
Node* pastNode = *ppNode, *currentNode = NULL;
*ppNode = (*ppNode)->Next;
pastNode->Next = NULL;
while(*ppNode)
{
currentNode = *ppNode;
*ppNode = (*ppNode)->Next;
// Insert node at the front of
// the temporary list
currentNode->Next = pastNode;
currentNode->Prev = NULL;
pastNode->Prev = currentNode;
pastNode = currentNode;
}
// Update head with the temporary
// list
*ppNode = pastNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node * Head = NULL;
InsertNode(&Head,10);
InsertNode(&Head,20);
InsertNode(&Head,30);
InsertNode(&Head,40);
InsertNode(&Head,50);
InsertNode(&Head,55);
InsertNode(&Head,60);
Display(Head);
DeleteNode(&Head,10);
DeleteNode(&Head,55);
Display(Head);
ReverseList(&Head);
Display(Head);
return 0;
}
{
Node* Prev;
Node* Next;
int data;
};
void InsertNode(Node** ppNode, int num)
{
if(*ppNode == NULL)
{
*ppNode = new Node();
(*ppNode)->data = num;
(*ppNode)->Next = NULL;
(*ppNode)->Prev = NULL;
return;
}
Node * pTemp = *ppNode;
while(pTemp->Next != NULL)
pTemp = pTemp->Next;
pTemp->Next = new Node();
pTemp->Next->data = num;
pTemp->Next->Prev = pTemp;
pTemp->Next->Next = NULL;
return;
}
void Display(Node *pNode)
{
while(pNode)
{
cout<<" "<
pNode = pNode->Next;
}
}
bool DeleteNode(Node** ppNode, int num)
{
bool bVal = false;
Node * pTemp = *ppNode;
//If the node is head then the head has to be reset
if((*ppNode)->data == num)
{
pTemp = pTemp->Next;
pTemp->Prev = NULL;
delete(*ppNode);
*ppNode = pTemp;
bVal = true;
}
else
{
while(pTemp)
{
if(pTemp->data == num)
{
pTemp->Prev->Next = pTemp->Next;
pTemp->Next->Prev = pTemp->Prev ;
delete pTemp;
bVal = true;
return bVal;
}
pTemp = pTemp->Next;
}
}
return bVal;
}
void ReverseList(Node** ppNode)
{
if((*ppNode == NULL)||((*ppNode)->Next == NULL))
return;
//Use 2 more pointers
Node* pastNode = *ppNode, *currentNode = NULL;
*ppNode = (*ppNode)->Next;
pastNode->Next = NULL;
while(*ppNode)
{
currentNode = *ppNode;
*ppNode = (*ppNode)->Next;
// Insert node at the front of
// the temporary list
currentNode->Next = pastNode;
currentNode->Prev = NULL;
pastNode->Prev = currentNode;
pastNode = currentNode;
}
// Update head with the temporary
// list
*ppNode = pastNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
Node * Head = NULL;
InsertNode(&Head,10);
InsertNode(&Head,20);
InsertNode(&Head,30);
InsertNode(&Head,40);
InsertNode(&Head,50);
InsertNode(&Head,55);
InsertNode(&Head,60);
Display(Head);
DeleteNode(&Head,10);
DeleteNode(&Head,55);
Display(Head);
ReverseList(&Head);
Display(Head);
return 0;
}
Wednesday, December 10, 2008
Replace all occurance of %20 with a blank space.
#define MAX_LEN 256
/*******************************************************
INPUT: 1) character string with one or more %20 patterns
2) length of the string
OUTPUT: 0 - SUCCESS
-1 - INVALID INPUT
-2 - No need to do anything.
Test Data: 1)"%20"
2)"This%20is"
3)"This%20is%20%20a"
4)"This%20is%20%20%20"
5)"This is a"
*******************************************************/
int FindPattern( TCHAR* szStr, size_t &len)
{
//Return Values
int iRet = -2;
//if invalid Input
if((szStr == NULL)||(len <= 0))
return -1;
if(len <3)
return iRet;
TCHAR *pAhead = szStr;
TCHAR *pBack = szStr;
//Loop till you reach the end of the string
while( *pAhead)
{
//if the pattern is found replace it with ' ' and reposition the pointers
if( (*pAhead == _T('%'))&& (*(pAhead+1) == _T('2'))&&
(*(pAhead+2) == _T('0')))
{
*pBack = _T(' ');
pAhead += 3;
pBack +=1;
iRet = 0;
}
else//copy the data and increment the pointer
{
*pBack++ = *pAhead++;
}
}
*pBack = 0;
len = _tcslen(szStr);
return iRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR szPattern[MAX_LEN];
memset(szPattern,0,MAX_LEN);
_tcscpy_s(szPattern,MAX_LEN,_T("This is a beautiful world"));
size_t len = _tcslen(szPattern);
int retVal = FindPattern(szPattern, len);
if(retVal == 0)
{
_tprintf(_T("The \' \' replaced string for %%20 is %s"),szPattern);
}
else if(retVal == -2)
_tprintf(_T("The %s did not have any %%20 pattern"),szPattern);
else
_tprintf(_T("\nInvalid input\n"));
return retVal;
}
/*******************************************************
INPUT: 1) character string with one or more %20 patterns
2) length of the string
OUTPUT: 0 - SUCCESS
-1 - INVALID INPUT
-2 - No need to do anything.
Test Data: 1)"%20"
2)"This%20is"
3)"This%20is%20%20a"
4)"This%20is%20%20%20"
5)"This is a"
*******************************************************/
int FindPattern( TCHAR* szStr, size_t &len)
{
//Return Values
int iRet = -2;
//if invalid Input
if((szStr == NULL)||(len <= 0))
return -1;
if(len <3)
return iRet;
TCHAR *pAhead = szStr;
TCHAR *pBack = szStr;
//Loop till you reach the end of the string
while( *pAhead)
{
//if the pattern is found replace it with ' ' and reposition the pointers
if( (*pAhead == _T('%'))&& (*(pAhead+1) == _T('2'))&&
(*(pAhead+2) == _T('0')))
{
*pBack = _T(' ');
pAhead += 3;
pBack +=1;
iRet = 0;
}
else//copy the data and increment the pointer
{
*pBack++ = *pAhead++;
}
}
*pBack = 0;
len = _tcslen(szStr);
return iRet;
}
int _tmain(int argc, _TCHAR* argv[])
{
_TCHAR szPattern[MAX_LEN];
memset(szPattern,0,MAX_LEN);
_tcscpy_s(szPattern,MAX_LEN,_T("This is a beautiful world"));
size_t len = _tcslen(szPattern);
int retVal = FindPattern(szPattern, len);
if(retVal == 0)
{
_tprintf(_T("The \' \' replaced string for %%20 is %s"),szPattern);
}
else if(retVal == -2)
_tprintf(_T("The %s did not have any %%20 pattern"),szPattern);
else
_tprintf(_T("\nInvalid input\n"));
return retVal;
}
Wednesday, December 3, 2008
Compute the number of digit after . in floating point number.
if given 9.554 output=3
for 43.000 output=0
double no =3.44;
int count =0;
while(no!=((int)no))
{
count++;
no=no*10;
}
printf("%d",count);
for 43.000 output=0
double no =3.44;
int count =0;
while(no!=((int)no))
{
count++;
no=no*10;
}
printf("%d",count);
Breadth first traversal in binary tree
//BreadthFirst Traversal
void BreadthFirst(Node* pNode)
{
queue que;
while(pNode!= NULL)
{
cout<<" "<Val;
if(pNode->Left != NULL)
que.push(pNode->Left);
if(pNode->Right != NULL)
que.push(pNode->Right);
if(!que.empty())
{
pNode = que.front();
que.pop();
}
else
pNode = NULL;
}
}
void BreadthFirst(Node* pNode)
{
queue
while(pNode!= NULL)
{
cout<<" "<
if(pNode->Left != NULL)
que.push(pNode->Left);
if(pNode->Right != NULL)
que.push(pNode->Right);
if(!que.empty())
{
pNode = que.front();
que.pop();
}
else
pNode = NULL;
}
}
Non recursive insert operation in binary tree.
struct Node{
Node* Left;
Node* Right;
int Val;
};
void InsertNodeNonRecursive(Node** ppNode, int val)
{
if(ppNode == NULL)
return;
if(*ppNode == NULL)
{
*ppNode = new Node();
(*ppNode)->Val = val;
(*ppNode)->Left = (*ppNode)->Right = NULL;
}
else
{
Node *pNode = *ppNode;
Node *pParent = NULL;
while(pNode != NULL)
{
//need to store the parent as we hit the NULL using pNode
pParent = pNode;
if( val > pNode->Val)
pNode = pNode->Right;
else
pNode = pNode->Left;
}
if(val > pParent->Val)
{
pParent->Right = new Node();
pParent->Right->Val = val;
pParent->Right->Left = NULL;
pParent->Right->Right = NULL;
}
else
{
pParent->Left = new Node();
pParent->Left->Val = val;
pParent->Left->Left = pParent->Left->Right = NULL;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Node* Head1 = NULL;
InsertNodeNonRecursive(&Head1,6);
InsertNodeNonRecursive(&Head1,4);
InsertNodeNonRecursive(&Head1,5);
InsertNodeNonRecursive(&Head1,2);
InsertNodeNonRecursive(&Head1,3);
InsertNodeNonRecursive(&Head1,1);
InsertNodeNonRecursive(&Head1,8);
InsertNodeNonRecursive(&Head1,7);
InsertNodeNonRecursive(&Head1,9);
return 0;
}
Node* Left;
Node* Right;
int Val;
};
void InsertNodeNonRecursive(Node** ppNode, int val)
{
if(ppNode == NULL)
return;
if(*ppNode == NULL)
{
*ppNode = new Node();
(*ppNode)->Val = val;
(*ppNode)->Left = (*ppNode)->Right = NULL;
}
else
{
Node *pNode = *ppNode;
Node *pParent = NULL;
while(pNode != NULL)
{
//need to store the parent as we hit the NULL using pNode
pParent = pNode;
if( val > pNode->Val)
pNode = pNode->Right;
else
pNode = pNode->Left;
}
if(val > pParent->Val)
{
pParent->Right = new Node();
pParent->Right->Val = val;
pParent->Right->Left = NULL;
pParent->Right->Right = NULL;
}
else
{
pParent->Left = new Node();
pParent->Left->Val = val;
pParent->Left->Left = pParent->Left->Right = NULL;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Node* Head1 = NULL;
InsertNodeNonRecursive(&Head1,6);
InsertNodeNonRecursive(&Head1,4);
InsertNodeNonRecursive(&Head1,5);
InsertNodeNonRecursive(&Head1,2);
InsertNodeNonRecursive(&Head1,3);
InsertNodeNonRecursive(&Head1,1);
InsertNodeNonRecursive(&Head1,8);
InsertNodeNonRecursive(&Head1,7);
InsertNodeNonRecursive(&Head1,9);
return 0;
}
Find the next biggest number from the digits in the number.
If input is 8999 the next biggest number is 9899.
If input is 14562 the next biggest number is 14625.
using namespace std;
//SWAP 2 CHARACTERS
void swap(char* ch1, char* ch2)
{
char t;
t = *ch1;
*ch1 = *ch2;
*ch2 = t;
}
//USED IN QUICK SORT FOR SORTING AROUND THE PIVOT
int partition(char* ptr, int low, int high)
{
char key = ptr[low];
int i = low+1;
int j = high;
while(high > i && ptr[i] <= key)i++;
while(ptr[j] > key) j--;
if(i < j)
{
swap(ptr[i],ptr[j]);
}
else
{
swap(ptr[j],ptr[low]);
}
return j;
}
//RECURSSIVE QUICK SORT
void quicksort(char * ptr, int low, int high)
{
if( high > low)
{
int idx = partition(ptr,low,high);
quicksort(ptr,low,idx-1);
quicksort(ptr,idx+1,high);
}
}
//NEXT BIGGEST
void NextBiggest(char * val){
size_t len = strlen(val);
bool swapped = false;
// case : 9788 o/p:9887
//IF THERE IS ANY NUMBER IN OTHER PLACE, SMALLER THAN THE UNITth PLACE THE SWAP THEM
// AND YOU HAVE FOUND THE NEXT BIGGEST NUMBER
for(int i = len-2; i >= 1; i--)
{
if(val[len-1] > val[i]){
swap(val[len-1],val[i]);
swapped = true;
break;
}
}
//case : 145621 o/p:151246
//case 8997 o/p:9789
//IN CASES AS SHOWN ABOVE CHECK EVERY ALTERNATE DECIMAL PLACE VALUE, IF YOU FIND A SMALLER VALUE IN THE HIGHER DECIMAL PLACE SIMPLY SWAP THEM AND SORT THE INPUT FROM THAT POSITION.
if(swapped == false)
{
int i = 0;
for(i = len-2; i >=1; i--)
{
if(val[i] > val[i-1])
{
swap(val[i],val[i-1]);
swapped = true;
break;
}
}
if(swapped == true)
{
char* ptr = &val[i];
quicksort(ptr,0,(len - (ptr-val)-1));
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char numeric [256];
memset(numeric,0,256);
cin >> numeric;
NextBiggest(numeric);
cout < < "The next biggest val is :" < < numeric;
return 0;
}
If input is 14562 the next biggest number is 14625.
using namespace std;
//SWAP 2 CHARACTERS
void swap(char* ch1, char* ch2)
{
char t;
t = *ch1;
*ch1 = *ch2;
*ch2 = t;
}
//USED IN QUICK SORT FOR SORTING AROUND THE PIVOT
int partition(char* ptr, int low, int high)
{
char key = ptr[low];
int i = low+1;
int j = high;
while(high > i && ptr[i] <= key)i++;
while(ptr[j] > key) j--;
if(i < j)
{
swap(ptr[i],ptr[j]);
}
else
{
swap(ptr[j],ptr[low]);
}
return j;
}
//RECURSSIVE QUICK SORT
void quicksort(char * ptr, int low, int high)
{
if( high > low)
{
int idx = partition(ptr,low,high);
quicksort(ptr,low,idx-1);
quicksort(ptr,idx+1,high);
}
}
//NEXT BIGGEST
void NextBiggest(char * val){
size_t len = strlen(val);
bool swapped = false;
// case : 9788 o/p:9887
//IF THERE IS ANY NUMBER IN OTHER PLACE, SMALLER THAN THE UNITth PLACE THE SWAP THEM
// AND YOU HAVE FOUND THE NEXT BIGGEST NUMBER
for(int i = len-2; i >= 1; i--)
{
if(val[len-1] > val[i]){
swap(val[len-1],val[i]);
swapped = true;
break;
}
}
//case : 145621 o/p:151246
//case 8997 o/p:9789
//IN CASES AS SHOWN ABOVE CHECK EVERY ALTERNATE DECIMAL PLACE VALUE, IF YOU FIND A SMALLER VALUE IN THE HIGHER DECIMAL PLACE SIMPLY SWAP THEM AND SORT THE INPUT FROM THAT POSITION.
if(swapped == false)
{
int i = 0;
for(i = len-2; i >=1; i--)
{
if(val[i] > val[i-1])
{
swap(val[i],val[i-1]);
swapped = true;
break;
}
}
if(swapped == true)
{
char* ptr = &val[i];
quicksort(ptr,0,(len - (ptr-val)-1));
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char numeric [256];
memset(numeric,0,256);
cin >> numeric;
NextBiggest(numeric);
cout < < "The next biggest val is :" < < numeric;
return 0;
}
Subscribe to:
Posts (Atom)
.jpg)