0

I have these functions to determine if a binary tree is ordered or not.

(It is assumed that we already have implemented the treemanagement.c, which I have modified to host integers and not strings)

int ordtree(Treeptr p) {
  int a, b;
  return (p == NULL || fun(p, &a, &b));
}

int fun(Treeptr p, int * ap, int * bp) {
  int a, b;
  // Is 'p' a leaf?
  if (p->left == NULL && p->right == NULL) {
    *ap = p->value;
    return 1;
  }

  // Does 'p' have only a left child?
  if (p->right == NULL) {
    *bp = p->value;
    return (fun(p->left, &b, ap) && p->value > b);
  }

  // Does 'p' have only a right child?
  if (p->left == NULL) {
    *ap = p->value;
    return (fun(p->right, &a, bp) && p->value < a);
  }

  // 'p' has two children
  return (fun(p->right, &a, bp) && fun(p->left, &b, ap));
}

The problem is that this will not work for a perfect tree (all nodes have two children, because there is no value checking in the case of a perfect tree in my code!).

For example, this unordered tree will be evaluated as an ordered one!

     4
  2      6
1   8  5   7

A bigger problem is that this comes from a test and I am obliged to use this "code" and fill in the GAPs.

int fun(Treeptr p, .....GAP A.....)
{
  int a, b;
  if (p->left == NULL && .....GAP B.....) {
    *ap = p->value;
    .....GAP C.....
  }
  if (p->right == NULL) {
    *bp = p->value;
    return (fun(.....GAP D.....) && p->value > b);
  }
  if (.....GAP E.....) {
    .....GAP F.....
    return (fun(p->right, &a, bp) && GAP .....G.....);
  }
  return (.....GAP H.....);
}

Any guidance?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • why do you need so many parameters? just compare the keys if in order should be enough. – Jason Hu Oct 03 '14 at 19:26
  • That's what confused me too @HuStmpHrrr!! Because this is a question of a test, and I **must** use the skeleton I provide in the last piece of code I have in my question. – gsamaras Oct 03 '14 at 19:28
  • i don't know what the code is trying to do neither. `ap` and `bp` are not symmetric. about the perfect tree problem. why you don't add more conditions? `bvalue && p->value – Jason Hu Oct 03 '14 at 19:48
  • @HuStmpHrrr I feel that the play the role of min and max value so far. Good idea. You mean to place them in the last return? – gsamaras Oct 03 '14 at 19:50
  • @HuStmpHrrr how to do this without destructing the given skeleton? – gsamaras Oct 03 '14 at 19:54
  • i feel like `ap` is the max of left subtree and `bp` is the min of right subtree` or the other way around. – Jason Hu Oct 03 '14 at 19:58
  • if so, in the leaf case, `*ap` and `*bp` have to both be set to `p->value`. – Jason Hu Oct 03 '14 at 19:59

1 Answers1

1

this is the best code i can do.

int fun(Treeptr p, int * ap, int * bp) {
  int a, b;
  // Is 'p' a leaf?
  if (p->left == NULL && p->right == NULL) {
    *ap = p->value;
    *bp = p->value; return 1; //gap c
  }

  // Does 'p' have only a left child?
  if (p->right == NULL) {
    *bp = p->value;
    return (fun(p->left, ap, &b) && p->value > b); //gap d
  }

  // Does 'p' have only a right child?
  if (p->left == NULL) {
    *ap = p->value;
    return (fun(p->right, &a, bp) && p->value < a); // gap g
  }

  // 'p' has two children
  return (fun(p->right, &a, bp) && fun(p->left, ap, &b) && a > p->value && p->value > b); // gap h
}
Jason Hu
  • 6,239
  • 1
  • 20
  • 41
  • Thanks for the attempt! However, this won't work. Take as an example the tree I have in my question and replace node 8 with 3. The function you provide will evaluate this tree to unordered, while it is ordered. :/ – gsamaras Oct 03 '14 at 20:08
  • @G.Samaras oh, i reversed the comparison signs. now it should work. – Jason Hu Oct 03 '14 at 20:13
  • I show the signs and I suspected that, but I wanted to test the answer too. I am not only accepting this, but I am going to upvote anything else I find good in your profile! THANK YOU! – gsamaras Oct 03 '14 at 20:17