[algogeeks] Re: n ary tree symmetry

2010-06-30 Thread Gene
On Jun 29, 10:50 pm, sharad kumar  wrote:
> @gene
> i tried but unable to extend it to n ary tree...can u plzz extend it to n
> ary ...
> thnx in advance..

// check a for symmetry
boolean is1(a)
{
  if (a == null) return true;
for (i = 0, j = a->n_children - 1; i < j; i++, j--)
  if (!is2(a->child[i], a->child[j])
return false;
return i == j ? is1(a->child[i]) : true;
}

// check that pair a and b are mirror images
boolean is2(a, b)
{
  if (a == null && b == null) return true;
  if (a != null && b != null && a->key == b->key && a->n_children == b-
>n_children) {
for (i = 0, j = a->n_children - 1; j >= 0; i++, j--)
  if (!is2(a->child[i], b->child[j])
return false;
return true;
  }
  return false;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@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: n ary tree symmetry

2010-06-30 Thread sharad kumar
@gene
i tried but unable to extend it to n ary tree...can u plzz extend it to n
ary ...
thnx in advance..

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@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: n ary tree symmetry

2010-06-29 Thread Gene
On Jun 27, 7:32 am, sharad kumar  wrote:
> Algorithm to find if a tree is symmetric? (The tree is a generalized tree
> with as many child nodes as possible)

Here's how to do it with a binary tree.  Extension to an n-ary tree is
straightforward.

boolean is(a, b)
{
  if (a == null && b == null) return true;
  if (a != null && b != null && a->key == b->key)
return is(a->left, b->right) && is(a->right, b->left);
  return false;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algoge...@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.