Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-07-04 Thread surender sanke
chk_bst doesnt works as its checking only for its immediate child's values.
i think inorder non decreasing sequence checking would require here which is
iteratively programmed

surender

On Thu, Jun 30, 2011 at 4:05 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 1.

 int chk_bst(node *root)
 {
  if(root)
  {
enqueue(root);
while(!isempty())
{
 p=dequeue();

 if(p-left)
 {
  if(!(p-left-data  p-data))
  return 0;
  enqueue(p-left);
 }

 if(p-right)
 {
  if(!(p-right-data = p-data))
  return 0;
  enqueue(p-right);
 }
}
   }
 return 1;
 }



 2.

 void mirror(node **root)
 {

 if(root)
 {
  enqueue(*root);
  while(!isempty())
  {
   *p = dequeue();
   if(*p-left)
   enqueue(*p-left);
   if(*p-right)
   enqueue(*p-right);
   swap(*p-left,*p-right);
  }
 }
 }


 On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement it
 iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-07-04 Thread Apoorve Mohan
@surender: in the while loop all the nodes are being checked...please tell
me where u r stuck???

On Mon, Jul 4, 2011 at 2:13 PM, surender sanke surend...@gmail.com wrote:

 chk_bst doesnt works as its checking only for its immediate child's values.
 i think inorder non decreasing sequence checking would require here which
 is iteratively programmed

 surender

 On Thu, Jun 30, 2011 at 4:05 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 1.

 int chk_bst(node *root)
 {
  if(root)
  {
enqueue(root);
while(!isempty())
{
 p=dequeue();

 if(p-left)
 {
  if(!(p-left-data  p-data))
  return 0;
  enqueue(p-left);
 }

 if(p-right)
 {
  if(!(p-right-data = p-data))
  return 0;
  enqueue(p-right);
 }
}
   }
 return 1;
 }



 2.

 void mirror(node **root)
 {

 if(root)
 {
  enqueue(*root);
  while(!isempty())
  {
   *p = dequeue();
   if(*p-left)
   enqueue(*p-left);
   if(*p-right)
   enqueue(*p-right);
   swap(*p-left,*p-right);
  }
 }
 }


 On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement
 it iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
regards

Apoorve Mohan

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-07-04 Thread surender sanke
seems its failing for
 3
 2  5
  1   4  N  N

Surender

On Mon, Jul 4, 2011 at 3:12 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 @surender: in the while loop all the nodes are being checked...please tell
 me where u r stuck???


 On Mon, Jul 4, 2011 at 2:13 PM, surender sanke surend...@gmail.comwrote:

 chk_bst doesnt works as its checking only for its immediate child's
 values.
 i think inorder non decreasing sequence checking would require here which
 is iteratively programmed

 surender

 On Thu, Jun 30, 2011 at 4:05 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 1.

 int chk_bst(node *root)
 {
  if(root)
  {
enqueue(root);
while(!isempty())
{
 p=dequeue();

 if(p-left)
 {
  if(!(p-left-data  p-data))
  return 0;
  enqueue(p-left);
 }

 if(p-right)
 {
  if(!(p-right-data = p-data))
  return 0;
  enqueue(p-right);
 }
}
   }
 return 1;
 }



 2.

 void mirror(node **root)
 {

 if(root)
 {
  enqueue(*root);
  while(!isempty())
  {
   *p = dequeue();
   if(*p-left)
   enqueue(*p-left);
   if(*p-right)
   enqueue(*p-right);
   swap(*p-left,*p-right);
  }
 }
 }


 On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement
 it iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-07-04 Thread Apoorve Mohan
@surender: ok man...got it...thanks :)

On Mon, Jul 4, 2011 at 3:28 PM, surender sanke surend...@gmail.com wrote:

 seems its failing for
  3
  2  5
   1   4  N  N

 Surender

 On Mon, Jul 4, 2011 at 3:12 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 @surender: in the while loop all the nodes are being checked...please tell
 me where u r stuck???


 On Mon, Jul 4, 2011 at 2:13 PM, surender sanke surend...@gmail.comwrote:

 chk_bst doesnt works as its checking only for its immediate child's
 values.
  i think inorder non decreasing sequence checking would require here
 which is iteratively programmed

 surender

 On Thu, Jun 30, 2011 at 4:05 PM, Apoorve Mohan 
 apoorvemo...@gmail.comwrote:

 1.

 int chk_bst(node *root)
 {
  if(root)
  {
enqueue(root);
while(!isempty())
{
 p=dequeue();

 if(p-left)
 {
  if(!(p-left-data  p-data))
  return 0;
  enqueue(p-left);
 }

 if(p-right)
 {
  if(!(p-right-data = p-data))
  return 0;
  enqueue(p-right);
 }
}
   }
 return 1;
 }



 2.

 void mirror(node **root)
 {

 if(root)
 {
  enqueue(*root);
  while(!isempty())
  {
   *p = dequeue();
   if(*p-left)
   enqueue(*p-left);
   if(*p-right)
   enqueue(*p-right);
   swap(*p-left,*p-right);
  }
 }
 }


 On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement
 it iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
regards

Apoorve Mohan

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-07-04 Thread surender sanke
think it might work.. its a variant of in order iterative version.. checking
each time previous value from current node's data

bool isBST(tree *t)
{
  if(!t)
   return true;
  bool done = false;
  int last_value = INT_MIN;
  Stack s;
  while(!done)
  {
 if(t)
 {
  s.push(t);
  t=t-left;
 }
 else if(!s.empty())
 {
  t = s.pop();
  if(last_value  t-data)
   return false;
  else
   last_value = t-data;
  t=t-right;
 }
 else
  done = true;
  }
  return true;
}

surender

On Mon, Jul 4, 2011 at 3:34 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 @surender: ok man...got it...thanks :)


 On Mon, Jul 4, 2011 at 3:28 PM, surender sanke surend...@gmail.comwrote:

 seems its failing for
  3
  2  5
   1   4  N  N

 Surender

 On Mon, Jul 4, 2011 at 3:12 PM, Apoorve Mohan apoorvemo...@gmail.comwrote:

 @surender: in the while loop all the nodes are being checked...please
 tell me where u r stuck???


 On Mon, Jul 4, 2011 at 2:13 PM, surender sanke surend...@gmail.comwrote:

 chk_bst doesnt works as its checking only for its immediate child's
 values.
  i think inorder non decreasing sequence checking would require here
 which is iteratively programmed

 surender

 On Thu, Jun 30, 2011 at 4:05 PM, Apoorve Mohan 
 apoorvemo...@gmail.comwrote:

 1.

 int chk_bst(node *root)
 {
  if(root)
  {
enqueue(root);
while(!isempty())
{
 p=dequeue();

 if(p-left)
 {
  if(!(p-left-data  p-data))
  return 0;
  enqueue(p-left);
 }

 if(p-right)
 {
  if(!(p-right-data = p-data))
  return 0;
  enqueue(p-right);
 }
}
   }
 return 1;
 }



 2.

 void mirror(node **root)
 {

 if(root)
 {
  enqueue(*root);
  while(!isempty())
  {
   *p = dequeue();
   if(*p-left)
   enqueue(*p-left);
   if(*p-right)
   enqueue(*p-right);
   swap(*p-left,*p-right);
  }
 }
 }


 On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to
 implement it iteratively and hense implemented its recursive version 
 only.

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 regards

 Apoorve Mohan

  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Amazon Telephonic Interview Questions

2011-06-30 Thread Gene
For 1. you need an in-order traversal. During the visit check to see
that each successive key is non-decreasing.

For 2. you need a post order traversal. During the visit swap the left
and right child pointers.

Now you can look up the standard algorithms for  these traversals.



On Jun 30, 3:34 am, vikas mehta...@gmail.com wrote:
 1. write iterative program to find if a binary tree is binary search tree or
 not.

 2. write iterative program to convert a binary tree into its mirror image.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Amazon Telephonic Interview Questions

2011-06-30 Thread vikas
for 1 i did implement exactly the same algorithms.
 
and for 2 i donot know why but at that time i was not able to implement it 
iteratively and hense implemented its recursive version only.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-06-30 Thread Anantha Krishnan
1. check here  http://ideone.com/VjjqN
*int isBSTiterative(Tnode *root) {*
*int prev = -9;*
*Tnode *node = NULL;*
*while (root) {*
*push(root);*
*if (root-left == NULL) {*
**
*do {*
*node = pop();*
*if (node-data  prev)*
*return 0;*
*prev = node-data;*
*root = node-right;*
*} while (root == NULL  isempty() != 1);*
*} else {*
*root = root-left;*
*}*
*}*
*return 1;*
*}*

2.Check here http://ideone.com/yufa2

*Tnode *BTMirrorIterative(Tnode *root) {*
*Tnode *node = NULL, *tmp = NULL;*
*
*
*if (root == NULL)*
*return root;*
*while (root) {*
*push(root);*
*if (root-left != NULL)*
*root = root-left;*
*else if (root-right != NULL)*
*root = root-right;*
*else {*
*do {*
*node = pop();*
*tmp = node-left;*
*node-left = node-right;*
*node-right = tmp;*
*
*
*if (head == NULL)*
*return root;*
*root = head-data;*
*} while (root-right == node || root-right == NULL);*
*if (root-right != NULL)*
*root = root-right;*
*}*
*}*
*return NULL;*
*}*

Thanks  Regards
Anantha Krishnan

On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement it
 iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Amazon Telephonic Interview Questions

2011-06-30 Thread Apoorve Mohan
1.

int chk_bst(node *root)
{
 if(root)
 {
   enqueue(root);
   while(!isempty())
   {
p=dequeue();

if(p-left)
{
 if(!(p-left-data  p-data))
 return 0;
 enqueue(p-left);
}

if(p-right)
{
 if(!(p-right-data = p-data))
 return 0;
 enqueue(p-right);
}
   }
  }
return 1;
}



2.

void mirror(node **root)
{

if(root)
{
 enqueue(*root);
 while(!isempty())
 {
  *p = dequeue();
  if(*p-left)
  enqueue(*p-left);
  if(*p-right)
  enqueue(*p-right);
  swap(*p-left,*p-right);
 }
}
}


On Thu, Jun 30, 2011 at 2:12 PM, vikas mehta...@gmail.com wrote:

 for 1 i did implement exactly the same algorithms.

 and for 2 i donot know why but at that time i was not able to implement it
 iteratively and hense implemented its recursive version only.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/hJswdQGammMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
regards

Apoorve Mohan

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.